package app import ( "fmt" "os" "os/user" "path" "strconv" "git.sr.ht/~rockorager/vaxis" "git.tilde.town/nbsp/neofeels/ttbp" "git.tilde.town/nbsp/neofeels/ui" ) type Delete struct { title string list ui.List help string posts []ttbp.Post } func NewDelete() *Delete { user, _ := user.Current() posts := ttbp.GetPostsForUser(user.Username) list := []string{} for _, post := range posts { list = append(list, fmt.Sprintf( "%s (%d words)", post.Date.Format("2006-01-02"), post.Words, )) } return &Delete{ title, ui.NewList(list), "↑↓/kj move ↵ enter q return", posts, } } func (delete *Delete) 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": delete.list.Down() case "Up", "k", "Ctrl+p": delete.list.Up() case "End", "Shift+g": delete.list.End() case "Home", "g": delete.list.Home() case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9": i, _ := strconv.Atoi(key.String()) delete.list.SetIndex(i) case "q", "h", "Left": ui.ViewChange <- NewManagement() case "Enter", "l", "Right": if len(delete.list.Items()) > 0 { delete.Confirmation(state, func() { os.Remove(path.Join(ttbp.PathUserEntries, delete.posts[delete.list.Index()].Date.Format("20060102.txt"))) }, "feels deleted") } } processed = true } delete.Draw(state) return } func (delete *Delete) Draw(state *ui.State) { win := state.Window() win.New(win.Width/2-10, win.Height/2-8, 20, 5).Print(vaxis.Segment{Text: delete.title}) delete.list.Draw(vaxis.Window{ Vx: win.Vx, Parent: nil, Column: win.Width/2 - 14, Row: win.Height/2 - 2, Width: 28, Height: 10, }) win.New(win.Width/2-15, win.Height/2+9, 30, 1).Print(vaxis.Segment{Text: delete.help}) } func (delete *Delete) Confirmation(state *ui.State, action func(), message string) { delete.Draw(state) win := state.Window() win.New(win.Width/2-14, win.Height/2-2+delete.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: delete.Draw(state) win.New(win.Width/2-14, win.Height/2-2+delete.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() delete.Draw(state) win.New(win.Width/2-14, win.Height/2-2+delete.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: delete.Draw(state) win.New(win.Width/2-14, win.Height/2-2+delete.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 { ui.ViewChange <- NewDelete() break loop } } } } break loop } } }