add homepage

This commit is contained in:
aoife cassidy 2025-03-23 17:36:00 +02:00
parent 376e93ec30
commit 4319b37063
No known key found for this signature in database
GPG Key ID: 7184AC1C9835CE48
3 changed files with 44 additions and 1 deletions

View File

@ -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) {

15
app/homepage.txt Normal file
View File

@ -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

View File

@ -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) {