Skip to content

Commit

Permalink
feat(list): adds list launchd process running feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mJehanno committed Mar 23, 2022
1 parent c776195 commit 78d91e6
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"workbench.colorCustomizations": {
"activityBar.activeBackground": "#88d7ea",
"activityBar.activeBorder": "#de41bf",
"activityBar.background": "#88d7ea",
"activityBar.foreground": "#15202b",
"activityBar.inactiveForeground": "#15202b99",
"activityBarBadge.background": "#de41bf",
"activityBarBadge.foreground": "#e7e7e7",
"sash.hoverBorder": "#88d7ea",
"statusBar.background": "#5dc9e2",
"statusBar.foreground": "#15202b",
"statusBarItem.hoverBackground": "#32bbda",
"statusBarItem.remoteBackground": "#5dc9e2",
"statusBarItem.remoteForeground": "#15202b",
"titleBar.activeBackground": "#5dc9e2",
"titleBar.activeForeground": "#15202b",
"titleBar.inactiveBackground": "#5dc9e299",
"titleBar.inactiveForeground": "#15202b99"
},
"peacock.color": "#5dc9e2"
}
50 changes: 50 additions & 0 deletions cmds/launchd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cmds

import (
"bytes"
"log"
"os/exec"
"strconv"
"strings"

tea "github.com/charmbracelet/bubbletea"
"github.com/wecraftforfun/launch-tui/models"
)

func List() tea.Msg {
cmd := exec.Command("launchctl", "list")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()

if err != nil {
log.Fatal(err)
}
s := out.String()
list := strings.Split(s, "\n")
processes := []models.Process{}
for i, v := range list {
if i == 0 || i == len(list)-1 {
continue
}
s := strings.Split(v, "\t")
pid := strings.Trim(s[0], " ")
status, _ := strconv.Atoi(strings.Trim(s[1], " "))
processes = append(processes, models.Process{
Pid: pid,
Status: status,
Label: strings.Trim(s[2], " "),
})
}
return models.UpdateListMessage{
List: processes,
}
}

func Start() {}

func Load() {}

func Unload() {}

func Stop() {}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module github.com/wecraftforfun/launch-tui

go 1.16

require (
github.com/charmbracelet/bubbles v0.10.3
github.com/charmbracelet/bubbletea v0.20.0
)
46 changes: 46 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/charmbracelet/bubbles v0.10.3 h1:fKarbRaObLn/DCsZO4Y3vKCwRUzynQD9L+gGev1E/ho=
github.com/charmbracelet/bubbles v0.10.3/go.mod h1:jOA+DUF1rjZm7gZHcNyIVW+YrBPALKfpGVdJu8UiJsA=
github.com/charmbracelet/bubbletea v0.19.3/go.mod h1:VuXF2pToRxDUHcBUcPmCRUHRvFATM4Ckb/ql1rBl3KA=
github.com/charmbracelet/bubbletea v0.20.0 h1:/b8LEPgCbNr7WWZ2LuE/BV1/r4t5PyYJtDb+J3vpwxc=
github.com/charmbracelet/bubbletea v0.20.0/go.mod h1:zpkze1Rioo4rJELjRyGlm9T2YNou1Fm4LIJQSa5QMEM=
github.com/charmbracelet/harmonica v0.1.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
github.com/charmbracelet/lipgloss v0.4.0 h1:768h64EFkGUr8V5yAKV7/Ta0NiVceiPaV+PphaW1K9g=
github.com/charmbracelet/lipgloss v0.4.0/go.mod h1:vmdkHvce7UzX6xkyf4cca8WlwdQ5RQr8fzta+xl7BOM=
github.com/containerd/console v1.0.2/go.mod h1:ytZPjGgY2oeTkAONYafi2kSj0aYggsf8acV1PGKCbzQ=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.9.0/go.mod h1:R/LzAKf+suGs4IsO95y7+7DpFHO0KABgnZqtlyx2mBw=
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 h1:QANkGiGr39l1EESqrE0gZw0/AJNYzIvoGLhIoVYtluI=
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed h1:Ei4bQjjpYUsS4efOUz+5Nz++IVkHk87n2zBA0NxBWc0=
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
17 changes: 17 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
"os"

tea "github.com/charmbracelet/bubbletea"
"github.com/wecraftforfun/launch-tui/tui"
)

func main() {
p := tea.NewProgram(tui.InitialModel(), tea.WithAltScreen())
if err := p.Start(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
}
5 changes: 5 additions & 0 deletions models/messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package models

type UpdateListMessage struct {
List []Process
}
20 changes: 20 additions & 0 deletions models/process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package models

import "fmt"

type Process struct {
Pid string
Status int
Label string
}

func (p Process) FilterValue() string {
return p.Label
}
func (p Process) Title() string {
return p.Label
}

func (p Process) Description() string {
return fmt.Sprintf("Running with PID %v, and Status %v.", p.Pid, p.Status)
}
58 changes: 58 additions & 0 deletions tui/appmodel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package tui

import (
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/wecraftforfun/launch-tui/cmds"
"github.com/wecraftforfun/launch-tui/models"
)

type AppModel struct {
keys keyMap
help help.Model
list list.Model
}

func InitialModel() AppModel {
m := AppModel{
keys: newListKeyMap(),
list: list.New(nil, NewListDelegate(), 1300, 20),
}

m.list.SetShowHelp(false)
m.list.Title = "LaunchD Terminal User Interface"
return m
}

func (m AppModel) Init() tea.Cmd {
// Just return `nil`, which means "no I/O right now, please."
return cmds.List
}

func (m AppModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case models.UpdateListMessage:
for _, v := range msg.List {
m.list.InsertItem(len(m.list.Items()), list.Item(v))
}
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keys.up):
m.list.CursorUp()
case key.Matches(msg, m.keys.down):
m.list.CursorDown()
case key.Matches(msg, m.keys.quit):
return tea.Model(m), tea.Quit
}
}
return tea.Model(m), nil
}

func (m AppModel) View() string {
s := ""
s += m.list.View()
s += "\n" + m.help.View(m.keys)
return s
}
19 changes: 19 additions & 0 deletions tui/delegate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tui

import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
)

func NewListDelegate() list.DefaultDelegate {
delegate := list.NewDefaultDelegate()
delegate.FullHelpFunc = func() [][]key.Binding {
return nil
}
delegate.ShortHelpFunc = func() []key.Binding {
return nil
}

delegate.ShowDescription = true
return delegate
}
49 changes: 49 additions & 0 deletions tui/keymap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package tui

import "github.com/charmbracelet/bubbles/key"

type keyMap struct {
up key.Binding
down key.Binding
insertItem key.Binding
deleteItem key.Binding
cancel key.Binding
quit key.Binding
}

func (k keyMap) ShortHelp() []key.Binding {
return []key.Binding{k.insertItem, k.deleteItem, k.up, k.down, k.quit}
}

// FullHelp returns keybindings for the expanded help view. It's part of the
// key.Map interface.
func (k keyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.insertItem, k.deleteItem, k.up, k.down, k.quit},
}
}

func newListKeyMap() keyMap {
return keyMap{
insertItem: key.NewBinding(
key.WithKeys("a"),
key.WithHelp("a", "Add a new Agent/Daemon"),
),
deleteItem: key.NewBinding(
key.WithKeys("d"),
key.WithHelp("d", "Delete current Agent/Daemon"),
),
quit: key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc", "Quit app"),
),
up: key.NewBinding(
key.WithKeys("up"),
key.WithHelp("up", "Move up"),
),
down: key.NewBinding(
key.WithKeys("down"),
key.WithHelp("down", "Move down"),
),
}
}

0 comments on commit 78d91e6

Please sign in to comment.