From 865c7b97893e4acbbda591e61954c9893c825579 Mon Sep 17 00:00:00 2001 From: nbsp Date: Wed, 8 Jan 2025 04:06:14 +0200 Subject: [PATCH] bury --- app/bury.go | 154 ++++++++++++++++++++++++++++++++++++++++++++++ app/management.go | 4 +- ttbp/ttbp.go | 1 + 3 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 app/bury.go diff --git a/app/bury.go b/app/bury.go new file mode 100644 index 0000000..b6225f5 --- /dev/null +++ b/app/bury.go @@ -0,0 +1,154 @@ +package app + +import ( + "fmt" + "os" + "os/user" + "path" + "strconv" + "time" + + "git.sr.ht/~rockorager/vaxis" + "git.tilde.town/nbsp/neofeels/ttbp" + "git.tilde.town/nbsp/neofeels/ui" +) + +type Bury struct { + title string + list ui.List + help string + posts []ttbp.Post +} + +func NewBury() *Bury { + 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 &Bury{ + title, + ui.NewList(list), + "↑↓/kj move ↵ enter q return", + posts, + } +} + +func (bury *Bury) 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": + bury.list.Down() + case "Up", "k", "Ctrl+p": + bury.list.Up() + case "End", "Shift+g": + bury.list.End() + case "Home", "g": + bury.list.Home() + case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9": + i, _ := strconv.Atoi(key.String()) + bury.list.SetIndex(i) + case "q", "h", "Left": + ui.ViewChange <- NewManagement() + case "Enter", "l", "Right": + if len(bury.list.Items()) > 0 { + bury.Confirmation(state, func() { + os.MkdirAll(ttbp.PathUserBuried, 0700) + newPath := path.Join(ttbp.PathUserBuried, bury.posts[bury.list.Index()].Date.Format("20060102-")+strconv.FormatInt(time.Now().Unix(), 10)+".txt") + os.Rename( + path.Join(ttbp.PathUserEntries, bury.posts[bury.list.Index()].Date.Format("20060102.txt")), + newPath, + ) + os.Chmod(newPath, 600) + }, "feels buried") + } + } + processed = true + } + bury.Draw(state) + return +} + +func (bury *Bury) Draw(state *ui.State) { + win := state.Window() + win.New(win.Width/2-10, win.Height/2-8, 20, 5).Print(vaxis.Segment{Text: bury.title}) + bury.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: bury.help}) +} + +func (delete *Bury) 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 <- NewBury() + break loop + } + } + } + } + break loop + } + } +} diff --git a/app/management.go b/app/management.go index 6782e27..6686a8d 100644 --- a/app/management.go +++ b/app/management.go @@ -25,7 +25,7 @@ func NewManagement() *Management { "modify feels publishing", // TODO "backup your feels", // TODO "import a feels backup", // TODO - "bury some feels", // TODO + "bury some feels", "delete feels by day", "purge all feels", "wipe feels account", @@ -57,6 +57,8 @@ func (management *Management) Event(state *ui.State, event vaxis.Event) (process case 0: user, _ := user.Current() ui.ViewChange <- NewUserPage(user.Username, true) + case 4: + ui.ViewChange <- NewBury() case 5: ui.ViewChange <- NewDelete() case 6: diff --git a/ttbp/ttbp.go b/ttbp/ttbp.go index 703b246..a60df79 100644 --- a/ttbp/ttbp.go +++ b/ttbp/ttbp.go @@ -29,6 +29,7 @@ var ( PathUserHTML = path.Join(PathUser, "public_html") PathUserConfig = path.Join(PathUserFeels, "config") PathUserEntries = path.Join(PathUserFeels, "entries") + PathUserBuried = path.Join(PathUserFeels, "buried") PathUserWWW = path.Join(PathUserFeels, "www") PathUserRc = path.Join(PathUserConfig, "ttbprc") PathUserNopub = path.Join(PathUserConfig, "nopub")