From f772bf0bdde2f7b4bbdd62ccb616f600a54619e8 Mon Sep 17 00:00:00 2001 From: aoife cassidy Date: Sat, 22 Mar 2025 13:47:16 +0200 Subject: [PATCH] initial commit --- .gitignore | 1 + app/app.go | 42 +++++++++++++++++++++ go.mod | 15 ++++++++ go.sum | 49 +++++++++++++++++++++++++ main.go | 26 +++++++++++++ ui/list.go | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++ ui/ui.go | 92 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 330 insertions(+) create mode 100644 .gitignore create mode 100644 app/app.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 ui/list.go create mode 100644 ui/ui.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0437ffd --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +directory diff --git a/app/app.go b/app/app.go new file mode 100644 index 0000000..efa48ae --- /dev/null +++ b/app/app.go @@ -0,0 +1,42 @@ +package app + +import ( + "os" + + "git.sr.ht/~rockorager/vaxis" + "git.tilde.town/nbsp/directory/ui" +) + +type App struct { + users ui.List +} + +func New() *App { + users := []string{"(home)"} + userDirs, _ := os.ReadDir("/home") + for _, user := range userDirs { + if user.IsDir() { + users = append(users, "~"+user.Name()) + } + } + return &App{ui.NewList(users, 0)} +} + +func (app *App) Event(state *ui.State, event vaxis.Event) { + if key, ok := event.(vaxis.Key); ok && key.EventType == vaxis.EventPress { + switch key.String() { + case "Ctrl+c", "Ctrl+d", "q": + close(ui.Quit) + case "Down", "j", "Ctrl+n": + app.users.Down() + case "Up", "k", "Ctrl+p": + app.users.Up() + case "End", "Shift+g": + app.users.End() + case "Home", "g": + app.users.Home() + } + } + + app.users.Draw(state.Window().New(0, 0, 20, state.Window().Height)) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..fe1bd0e --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module git.tilde.town/nbsp/directory + +go 1.23.5 + +require git.sr.ht/~rockorager/vaxis v0.13.0 + +require ( + github.com/containerd/console v1.0.3 // indirect + github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/mattn/go-sixel v0.0.5 // indirect + github.com/rivo/uniseg v0.4.4 // indirect + github.com/soniakeys/quant v1.0.0 // indirect + golang.org/x/image v0.9.0 // indirect + golang.org/x/sys v0.10.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..cec6c31 --- /dev/null +++ b/go.sum @@ -0,0 +1,49 @@ +git.sr.ht/~rockorager/vaxis v0.13.0 h1:+F3ze1t4X5x87QEsyn/b9b9pWXw9MDqoHynoR/PHqKg= +git.sr.ht/~rockorager/vaxis v0.13.0/go.mod h1:h94aKek3frIV1hJbdXjqnBqaLkbWXvV+UxAsQHg9bns= +github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw= +github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U= +github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= +github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mattn/go-sixel v0.0.5 h1:55w2FR5ncuhKhXrM5ly1eiqMQfZsnAHIpYNGZX03Cv8= +github.com/mattn/go-sixel v0.0.5/go.mod h1:h2Sss+DiUEHy0pUqcIB6PFXo5Cy8sTQEFr3a9/5ZLNw= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= +github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/soniakeys/quant v1.0.0 h1:N1um9ktjbkZVcywBVAAYpZYSHxEfJGzshHCxx/DaI0Y= +github.com/soniakeys/quant v1.0.0/go.mod h1:HI1k023QuVbD4H8i9YdfZP2munIHU4QpjsImz6Y6zds= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/image v0.9.0 h1:QrzfX26snvCM20hIhBwuHI/ThTg18b/+kcKdXHvnR+g= +golang.org/x/image v0.9.0/go.mod h1:jtrku+n79PfroUbvDdeUWMAI+heR786BofxrbiSF+J0= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= +golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/main.go b/main.go new file mode 100644 index 0000000..486913e --- /dev/null +++ b/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "git.tilde.town/nbsp/directory/app" + "git.tilde.town/nbsp/directory/ui" +) + +func main() { + state, err := ui.New(app.New()) + if err != nil { + panic(err) + } + defer state.Close() + +loop: + for { + select { + case event := <-ui.Events: + state.HandleEvent(event) + case newState := <-ui.ViewChange: + state.HandleViewChange(newState) + case <-ui.Quit: + break loop + } + } +} diff --git a/ui/list.go b/ui/list.go new file mode 100644 index 0000000..e5d1591 --- /dev/null +++ b/ui/list.go @@ -0,0 +1,105 @@ +/** + * vaxis/widget/list, edited to support wraparounds and number seeking + * soon to support confirmations, once i get around to that + */ +package ui + +import ( + "fmt" + "strconv" + + "git.sr.ht/~rockorager/vaxis" +) + +type List struct { + index int + items []string + offset int +} + +func NewList(items []string, index int) List { + return List{ + items: items, + index: index, + } +} + +func (m *List) Draw(win vaxis.Window) { + _, height := win.Size() + if m.index >= m.offset+height { + m.offset = m.index - height + 1 + } else if m.index < m.offset { + m.offset = m.index + } + + defaultStyle := vaxis.Style{} + selectedStyle := vaxis.Style{Attribute: vaxis.AttrReverse} + + index := m.index - m.offset + for i, subject := range m.items[m.offset:] { + var style vaxis.Style + if i == index { + style = selectedStyle + } else { + style = defaultStyle + } + win.Println(i, vaxis.Segment{Text: fmt.Sprintf("%-"+strconv.Itoa(win.Width)+"s", subject), Style: style}) + } + +} + +func (m *List) Down() { + if m.index == len(m.items)-1 { + m.index = 0 + } else { + m.index++ + } +} + +func (m *List) Up() { + if m.index == 0 { + m.index = len(m.items) - 1 + } else { + m.index-- + } +} + +func (m *List) Home() { + m.index = 0 +} + +func (m *List) End() { + m.index = len(m.items) - 1 +} + +func (m *List) PageDown(win vaxis.Window) { + _, height := win.Size() + m.index = min(len(m.items)-1, m.index+height) +} + +func (m *List) PageUp(win vaxis.Window) { + _, height := win.Size() + m.index = max(0, m.index-height) +} + +func (m *List) Items() []string { + return m.items +} + +func (m *List) SetItems(items []string) { + m.items = items + m.index = min(len(items)-1, m.index) +} + +func (m *List) SetItem(index int, item string) { + m.items[index] = item +} + +// Returns the index of the currently selected item. +func (m *List) Index() int { + return m.index +} + +func (m *List) SetIndex(index int) { + m.index = index +} diff --git a/ui/ui.go b/ui/ui.go new file mode 100644 index 0000000..2f3c95c --- /dev/null +++ b/ui/ui.go @@ -0,0 +1,92 @@ +package ui + +import ( + "git.sr.ht/~rockorager/vaxis" +) + +type Context struct { + Window vaxis.Window + x, y int +} + +type View interface { + Event(state *State, event vaxis.Event) + // MouseEvent(localX int, localY int, event vaxis.Event) +} + +var Events = make(chan vaxis.Event) +var Quit = make(chan struct{}) +var ViewChange = make(chan View, 1) + +type State struct { + content View + vx *vaxis.Vaxis +} + +func New(view View) (state State, err error) { + vx, err := vaxis.New(vaxis.Options{ + DisableMouse: true, + }) + if err != nil { + return + } + state = State{ + content: view, + vx: vx, + } + state.vx.SetTitle("neofeels") + + go func() { + for event := range state.vx.Events() { + state.vx.Window().Clear() + Events <- event + } + close(Events) + }() + + return +} + +func (state *State) HandleEvent(event vaxis.Event) { + state.content.Event(state, event) + state.vx.Render() +} + +func (state *State) HandleViewChange(view View) { + state.vx.Window().Clear() + state.content = view + state.content.Event(state, vaxis.Redraw{}) + state.vx.Render() +} + +func (state *State) Close() { + state.vx.Close() +} + +func (state *State) PostEvent() func(ev vaxis.Event) { + return state.vx.PostEvent +} + +func (state *State) Events() chan vaxis.Event { + return state.vx.Events() +} + +func (state *State) Render() { + state.vx.Render() +} + +func (state *State) HideCursor() { + state.vx.HideCursor() +} + +func (state *State) Window() vaxis.Window { + return state.vx.Window() +} + +func (state *State) Suspend() { + state.vx.Suspend() +} + +func (state *State) Resume() { + state.vx.Resume() +}