From 4319b37063633a79f39aff25e10f3ba497443fcb Mon Sep 17 00:00:00 2001 From: aoife cassidy Date: Sun, 23 Mar 2025 17:36:00 +0200 Subject: [PATCH] add homepage --- app/app.go | 25 +++++++++++++++++++++++++ app/homepage.txt | 15 +++++++++++++++ ui/list.go | 5 ++++- 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 app/homepage.txt diff --git a/app/app.go b/app/app.go index fb54472..c2cd5ab 100644 --- a/app/app.go +++ b/app/app.go @@ -1,7 +1,9 @@ package app import ( + _ "embed" "os" + "os/exec" "strings" "git.sr.ht/~rockorager/vaxis" @@ -9,6 +11,9 @@ import ( "git.tilde.town/nbsp/directory/ui" ) +//go:embed homepage.txt +var homepage []byte + type App struct { allUsers []string users ui.List @@ -62,6 +67,7 @@ func (app *App) Event(state *ui.State, event vaxis.Event) { case "/": app.searching = true app.filter.SetContent("") + app.users.SetItems(app.allUsers) } } else { app.filter.Update(event) @@ -70,11 +76,14 @@ func (app *App) Event(state *ui.State, event vaxis.Event) { } } + // render sidebar win := state.Window() w, h := win.Size() if len(app.users.Items()) > 0 { app.users.Draw(win.New(0, 0, 20, h-1)) } + + // render search bar for x := 0; x < w; x++ { setCell(win, x, h-2, '─', vaxis.Style{}) } @@ -83,6 +92,22 @@ func (app *App) Event(state *ui.State, event vaxis.Event) { } else { win.New(0, h-1, w, 1).Print(vaxis.Segment{Text: "press / to search users", Style: vaxis.Style{Foreground: vaxis.IndexColor(8)}}) } + + // render page + if len(app.users.Items()) == 0 { + return + } + main := win.New(20, 0, w, h-2) + if app.users.Items()[app.users.Index()] == "(home)" { + // special homepage + aligned := win.New(main.Width/2, main.Height/2-8, 47, 15) + aligned.Print(vaxis.Segment{Text: string(homepage)}) + } else { + // user page + user := app.users.Items()[app.users.Index()][1:] + art, _ := exec.Command("figlet", "-f", "script", user).CombinedOutput() + main.Print(vaxis.Segment{Text: string(art)}) + } } func setCell(win vaxis.Window, x int, y int, r rune, st vaxis.Style) { diff --git a/app/homepage.txt b/app/homepage.txt new file mode 100644 index 0000000..1bc7ea7 --- /dev/null +++ b/app/homepage.txt @@ -0,0 +1,15 @@ + _ + o | | | +_|_ | | __| _ _|_ __ _ _ + | | |/ / | |/ | / \_| | |_/ |/ | + |_/|_/|__/\_/|_/|__/o|_/\__/ \/ \/ | |_/ + tilde.town directory explorer v0.1.0 + +use the list to the left to browse users, or +press / to search for specific users. + +press Enter on a user to enter their page, +where you can see information about them, and +what they're up to on town. + + happy browsing! <3 diff --git a/ui/list.go b/ui/list.go index 9ba8d95..86fd48a 100644 --- a/ui/list.go +++ b/ui/list.go @@ -74,6 +74,9 @@ func (m *List) Home() { func (m *List) End() { m.index = len(m.items) - 1 + if m.index < 0 { + m.index = 0 + } } func (m *List) PageDown(win vaxis.Window) { @@ -92,7 +95,7 @@ func (m *List) Items() []string { func (m *List) SetItems(items []string) { m.items = items - m.index = min(len(items)-1, m.index) + m.index = max(min(len(items)-1, m.index), 0) } func (m *List) SetItem(index int, item string) {