neofeels/app/management.go

210 lines
5.3 KiB
Go
Raw Normal View History

2025-01-05 20:58:43 +00:00
package app
import (
2025-01-05 22:53:05 +00:00
"fmt"
2025-01-08 01:43:27 +00:00
"os"
2025-01-08 02:21:56 +00:00
"os/exec"
2025-01-08 01:43:27 +00:00
"os/user"
2025-01-08 02:21:56 +00:00
"path"
2025-01-05 23:06:35 +00:00
"strconv"
2025-01-08 02:21:56 +00:00
"time"
2025-01-05 22:53:05 +00:00
2025-01-05 20:58:43 +00:00
"git.sr.ht/~rockorager/vaxis"
2025-01-08 01:43:27 +00:00
"git.tilde.town/nbsp/neofeels/ttbp"
2025-01-05 20:58:43 +00:00
"git.tilde.town/nbsp/neofeels/ui"
)
type Management struct {
title string
2025-01-05 23:06:35 +00:00
list ui.List
2025-01-05 20:58:43 +00:00
help string
}
func NewManagement() *Management {
return &Management{
title,
2025-01-05 23:06:35 +00:00
ui.NewList([]string{
2025-01-08 01:43:27 +00:00
"read over feels",
2025-01-05 23:11:39 +00:00
"modify feels publishing", // TODO
"backup your feels", // TODO
"import a feels backup", // TODO
2025-01-08 02:06:14 +00:00
"bury some feels",
2025-01-08 01:55:51 +00:00
"delete feels by day",
2025-01-08 01:43:27 +00:00
"purge all feels",
"wipe feels account",
2025-01-05 20:58:43 +00:00
}),
"↑↓/kj move ↵ enter q return",
}
}
func (management *Management) Event(state *ui.State, event vaxis.Event) (processed bool) {
if key, ok := event.(vaxis.Key); ok && key.EventType == vaxis.EventPress {
switch key.String() {
case "Ctrl+c", "Ctrl+d":
close(ui.Quit)
2025-01-07 22:44:43 +00:00
case "Down", "j", "Ctrl+n":
2025-01-05 20:58:43 +00:00
management.list.Down()
2025-01-07 22:44:43 +00:00
case "Up", "k", "Ctrl+p":
2025-01-05 20:58:43 +00:00
management.list.Up()
case "End", "Shift+g":
management.list.End()
case "Home", "g":
management.list.Home()
2025-01-05 23:06:35 +00:00
case "0", "1", "2", "3", "4", "5", "6", "7":
i, _ := strconv.Atoi(key.String())
management.list.SetIndex(i)
2025-01-05 20:58:43 +00:00
case "q", "h", "Left":
ui.ViewChange <- NewMainMenu()
case "Enter", "l", "Right":
switch management.list.Index() {
2025-01-08 01:43:27 +00:00
case 0:
user, _ := user.Current()
ui.ViewChange <- NewUserPage(user.Username, true)
2025-01-08 02:21:56 +00:00
// case 1:
case 2:
management.SaveBackup(state)
// case 3:
2025-01-08 02:06:14 +00:00
case 4:
ui.ViewChange <- NewBury()
2025-01-08 01:55:51 +00:00
case 5:
ui.ViewChange <- NewDelete()
2025-01-05 22:53:05 +00:00
case 6:
2025-01-08 01:43:27 +00:00
management.Confirmation(state, purgeFeels, "feels purged")
2025-01-05 22:53:05 +00:00
case 7:
2025-01-08 01:43:27 +00:00
management.Confirmation(state, wipeAccount, "feels account wiped")
2025-01-05 20:58:43 +00:00
}
}
processed = true
}
2025-01-05 22:53:05 +00:00
management.Draw(state)
return
}
func (management *Management) Draw(state *ui.State) {
2025-01-05 20:58:43 +00:00
win := state.Window()
win.New(win.Width/2-10, win.Height/2-8, 20, 5).Print(vaxis.Segment{Text: management.title})
management.list.Draw(vaxis.Window{
Vx: win.Vx,
Parent: nil,
Column: win.Width/2 - 14,
Row: win.Height/2 - 2,
Width: 28,
Height: 10,
})
2025-01-05 22:53:05 +00:00
win.New(win.Width/2-15, win.Height/2+7, 30, 1).Print(vaxis.Segment{Text: management.help})
}
2025-01-08 01:43:27 +00:00
func purgeFeels() {
ttbp.Unpublish()
os.RemoveAll(ttbp.PathUserEntries)
os.MkdirAll(ttbp.PathUserEntries, 0700)
}
func wipeAccount() {
// TODO: edit users.txt
ttbp.Unpublish()
os.RemoveAll(ttbp.PathUserFeels)
close(ui.Quit)
}
2025-01-08 02:21:56 +00:00
func (management *Management) SaveBackup(state *ui.State) {
os.MkdirAll(ttbp.PathUserBackups, 0700)
filename := time.Now().Format("feels-backup-20060102-150405.tar.gz")
exec.Command("tar", "-C", ttbp.PathUserFeels, "-czf", filename, "entries").Run()
exec.Command("tar", "-C", ttbp.PathUserFeels, "-czf", path.Join(ttbp.PathUserBackups, filename), "entries").Run()
management.Draw(state)
win := state.Window()
win.New(win.Width/2-14, win.Height/2-2+management.list.Index(), 28, 1).Print(vaxis.Segment{
Text: " backup saved ",
Style: vaxis.Style{
Foreground: vaxis.IndexColor(2),
Attribute: vaxis.AttrReverse,
},
})
state.Render()
loop:
for ev := range state.Events() {
switch ev.(type) {
case vaxis.Resize, vaxis.Redraw:
management.Draw(state)
win.New(win.Width/2-14, win.Height/2-2+management.list.Index(), 28, 1).Print(vaxis.Segment{
Text: " backup saved ",
Style: vaxis.Style{
Foreground: vaxis.IndexColor(2),
Attribute: vaxis.AttrReverse,
},
})
state.Render()
case vaxis.Key:
if ev.(vaxis.Key).EventType == vaxis.EventPress {
break loop
}
}
}
}
2025-01-05 22:53:05 +00:00
// TODO: abstract this to our own List type
// TODO: figure out a less janky way instead of nesting event channels
func (management *Management) Confirmation(state *ui.State, action func(), message string) {
management.Draw(state)
win := state.Window()
win.New(win.Width/2-14, win.Height/2-2+management.list.Index(), 28, 1).Print(vaxis.Segment{
Text: " are you sure? ",
Style: vaxis.Style{
Foreground: vaxis.IndexColor(1),
Attribute: vaxis.AttrReverse,
},
})
state.Render()
loop:
for ev := range state.Events() {
switch ev.(type) {
case vaxis.Resize, vaxis.Redraw:
management.Draw(state)
win.New(win.Width/2-14, win.Height/2-2+management.list.Index(), 28, 1).Print(vaxis.Segment{
Text: " are you sure? ",
Style: vaxis.Style{
Foreground: vaxis.IndexColor(1),
Attribute: vaxis.AttrReverse,
},
})
state.Render()
case vaxis.Key:
switch ev.(vaxis.Key).String() {
case "Enter", "l", "Right":
action()
management.Draw(state)
win.New(win.Width/2-14, win.Height/2-2+management.list.Index(), 28, 1).Print(vaxis.Segment{
Text: fmt.Sprintf(" %-26s", message),
Style: vaxis.Style{
Foreground: vaxis.IndexColor(1),
Attribute: vaxis.AttrReverse,
},
})
state.Render()
for ev := range state.Events() {
switch ev.(type) {
case vaxis.Resize, vaxis.Redraw:
management.Draw(state)
win.New(win.Width/2-14, win.Height/2-2+management.list.Index(), 28, 1).Print(vaxis.Segment{
Text: fmt.Sprintf(" %26s", message),
Style: vaxis.Style{
Foreground: vaxis.IndexColor(1),
Attribute: vaxis.AttrReverse,
},
})
state.Render()
case vaxis.Key:
if ev.(vaxis.Key).EventType == vaxis.EventPress {
break loop
}
}
}
}
break loop
}
}
2025-01-05 20:58:43 +00:00
}