add shims for backups

trunk
nbsp 2025-01-08 05:37:45 +02:00
parent db58cb27f7
commit db53b21005
No known key found for this signature in database
GPG Key ID: 7184AC1C9835CE48
2 changed files with 124 additions and 2 deletions

121
app/backups.go 100644
View File

@ -0,0 +1,121 @@
package app
import (
"io/fs"
"os"
"strconv"
"time"
"git.sr.ht/~rockorager/vaxis"
"git.tilde.town/nbsp/neofeels/ttbp"
"git.tilde.town/nbsp/neofeels/ui"
"github.com/dustin/go-humanize"
)
type Backups struct {
title string
list ui.List
help string
description string
backups []fs.DirEntry
}
func NewBackups() *Backups {
os.MkdirAll(ttbp.PathUserBackups, 0700)
backups, _ := os.ReadDir(ttbp.PathUserBackups)
list := []string{}
for _, backup := range backups {
timestamp, err := time.Parse("feels-backup-20060102-150405.tar.gz", backup.Name())
if err != nil {
continue
}
list = append(list, humanize.Time(timestamp.Local()))
}
return &Backups{
title,
ui.NewList(list),
"↑↓/kj move ↵ enter q return",
"choose a backup to restore",
backups,
}
}
func (backups *Backups) 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)
case "Down", "j", "Ctrl+n":
backups.list.Down()
case "Up", "k", "Ctrl+p":
backups.list.Up()
case "End", "Shift+g":
backups.list.End()
case "Home", "g":
backups.list.Home()
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
i, _ := strconv.Atoi(key.String())
backups.list.SetIndex(i)
case "q", "h", "Left":
ui.ViewChange <- NewManagement()
case "Enter", "l", "Right":
if len(backups.list.Items()) > 0 {
backups.LoadBackup(state)
}
}
processed = true
}
backups.Draw(state)
return
}
func (backups *Backups) Draw(state *ui.State) {
win := state.Window()
win.New(win.Width/2-10, win.Height/2-8, 20, 5).Print(vaxis.Segment{Text: backups.title})
win.New(win.Width/2-13, win.Height/2-2, 26, 1).Print(vaxis.Segment{Text: backups.description})
backups.list.Draw(vaxis.Window{
Vx: win.Vx,
Parent: nil,
Column: win.Width/2 - 14,
Row: win.Height / 2,
Width: 28,
Height: 10,
})
if len(backups.list.Items()) == 0 {
win.New(win.Width/2-8, win.Height/2+1, 16, 1).Print(vaxis.Segment{Text: "no backups found"})
}
win.New(win.Width/2-14, win.Height/2+11, 28, 1).Print(vaxis.Segment{Text: backups.help})
}
func (backups *Backups) LoadBackup(state *ui.State) {
backups.Draw(state)
win := state.Window()
win.New(win.Width/2-14, win.Height/2+backups.list.Index(), 28, 1).Print(vaxis.Segment{
Text: " backup restored ",
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:
backups.Draw(state)
win.New(win.Width/2-14, win.Height/2+backups.list.Index(), 28, 1).Print(vaxis.Segment{
Text: " backup restored ",
Style: vaxis.Style{
Foreground: vaxis.IndexColor(2),
Attribute: vaxis.AttrReverse,
},
})
state.Render()
case vaxis.Key:
if ev.(vaxis.Key).EventType == vaxis.EventPress {
break loop
}
}
}
}

View File

@ -27,7 +27,7 @@ func NewManagement() *Management {
"read over feels",
"modify feels publishing",
"backup your feels",
"import a feels backup", // TODO
"import a feels backup",
"bury some feels",
"delete feels by day",
"purge all feels",
@ -64,7 +64,8 @@ func (management *Management) Event(state *ui.State, event vaxis.Event) (process
ui.ViewChange <- NewPublishing()
case 2:
management.SaveBackup(state)
// case 3:
case 3:
ui.ViewChange <- NewBackups()
case 4:
ui.ViewChange <- NewBury()
case 5: