From 6b5bc6b0efb7c36b1440db969131eb81e148526e Mon Sep 17 00:00:00 2001 From: nbsp Date: Wed, 8 Jan 2025 05:13:48 +0200 Subject: [PATCH] nopub per post --- app/management.go | 9 +++-- app/menu.go | 2 +- app/publishing.go | 92 ++++++++++++++++++++++++++++++++++++++++++++++ assets/header.html | 4 +- ttbp/ttbp.go | 43 +++++++++++++++++++++- 5 files changed, 141 insertions(+), 9 deletions(-) create mode 100644 app/publishing.go diff --git a/app/management.go b/app/management.go index 6af8f40..130b0a2 100644 --- a/app/management.go +++ b/app/management.go @@ -25,9 +25,9 @@ func NewManagement() *Management { title, ui.NewList([]string{ "read over feels", - "modify feels publishing", // TODO - "backup your feels", // TODO - "import a feels backup", // TODO + "modify feels publishing", + "backup your feels", // TODO + "import a feels backup", // TODO "bury some feels", "delete feels by day", "purge all feels", @@ -60,7 +60,8 @@ func (management *Management) Event(state *ui.State, event vaxis.Event) (process case 0: user, _ := user.Current() ui.ViewChange <- NewUserPage(user.Username, true) - // case 1: + case 1: + ui.ViewChange <- NewPublishing() case 2: management.SaveBackup(state) // case 3: diff --git a/app/menu.go b/app/menu.go index d449462..acac71d 100644 --- a/app/menu.go +++ b/app/menu.go @@ -147,7 +147,7 @@ func newFeels(state *ui.State) { state.HideCursor() state.Window().Clear() ttbp.NewNopub(now) - ttbp.Publish(now) + ttbp.Publish() ui.ViewChange <- NewPosted() return case vaxis.Redraw: diff --git a/app/publishing.go b/app/publishing.go new file mode 100644 index 0000000..5b87a94 --- /dev/null +++ b/app/publishing.go @@ -0,0 +1,92 @@ +package app + +import ( + "fmt" + "os/user" + "strconv" + + "git.sr.ht/~rockorager/vaxis" + "git.tilde.town/nbsp/neofeels/ttbp" + "git.tilde.town/nbsp/neofeels/ui" +) + +type Publishing struct { + title string + list ui.List + help string + posts []ttbp.Post +} + +func NewPublishing() *Publishing { + user, _ := user.Current() + posts := ttbp.GetPostsForUser(user.Username) + list := []string{} + for _, post := range posts { + list = append(list, formatPublishing(post)) + } + + return &Publishing{ + title, + ui.NewList(list), + "↑↓/kj move ↵ enter q return", + posts, + } +} + +func formatPublishing(post ttbp.Post) string { + nopub := "" + if post.Nopub { + nopub = "(nopub)" + } + return fmt.Sprintf( + "%s %s", + post.Date.Format("2006-01-02"), + nopub, + ) +} + +func (publishing *Publishing) 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": + publishing.list.Down() + case "Up", "k", "Ctrl+p": + publishing.list.Up() + case "End", "Shift+g": + publishing.list.End() + case "Home", "g": + publishing.list.Home() + case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9": + i, _ := strconv.Atoi(key.String()) + publishing.list.SetIndex(i) + case "q", "h", "Left": + ttbp.Publish() + ui.ViewChange <- NewManagement() + case "Enter", "l", "Right": + if len(publishing.list.Items()) > 0 { + publishing.posts[publishing.list.Index()].Nopub = !publishing.posts[publishing.list.Index()].Nopub + ttbp.ToggleNopub(publishing.posts[publishing.list.Index()].Date) + publishing.list.SetItem(publishing.list.Index(), formatPublishing(publishing.posts[publishing.list.Index()])) + } + } + processed = true + } + publishing.Draw(state) + return +} + +func (publishing *Publishing) Draw(state *ui.State) { + win := state.Window() + win.New(win.Width/2-10, win.Height/2-8, 20, 5).Print(vaxis.Segment{Text: publishing.title}) + publishing.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: publishing.help}) +} diff --git a/assets/header.html b/assets/header.html index 02908f7..61b1b88 100644 --- a/assets/header.html +++ b/assets/header.html @@ -14,6 +14,4 @@ -
- - +
diff --git a/ttbp/ttbp.go b/ttbp/ttbp.go index acb63a0..3f3d847 100644 --- a/ttbp/ttbp.go +++ b/ttbp/ttbp.go @@ -261,7 +261,48 @@ func NewNopub(t time.Time) { writer.Flush() } -func Publish(t time.Time) { +func ToggleNopub(t time.Time) { + dateString := t.Format("20060102.txt") + nopubs, err := os.ReadFile(PathUserNopub) + if err != nil { + return + } + + lines := strings.Split(strings.TrimSpace(string(nopubs)), "\n") + + newLines := []string{} + exists := false + for _, line := range lines { + if line == dateString { + exists = true + } else { + newLines = append(newLines, line) + } + } + if !exists { + newLines = append(newLines, dateString) + } + + file, err := os.Create(PathUserNopub) + if err != nil { + return + } + defer file.Close() + + writer := bufio.NewWriter(file) + for _, line := range newLines { + if line == "" { + continue + } + _, err := writer.WriteString(line + "\n") + if err != nil { + return + } + } + writer.Flush() +} + +func Publish() { cfg, err := config.Read() if err != nil { return // TODO: expose this error to the user