From 9eac46a2aaf3a7f43672de528447a1145b3dd25f Mon Sep 17 00:00:00 2001 From: nbsp Date: Sun, 5 Jan 2025 21:42:53 +0200 Subject: [PATCH] posting feels --- README.md | 2 +- app/credits.go | 10 +++------- app/menu.go | 46 ++++++++++++++++++++++++++++++++++++++++++---- app/posted.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 app/posted.go diff --git a/README.md b/README.md index 211258c..3406dac 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ make - [x] credits - [ ] feels browser - [ ] configuration editor -- [ ] feels publishing +- [x] feels publishing - [ ] graffiti - [ ] feedback - [ ] documentation with manpages diff --git a/app/credits.go b/app/credits.go index 2ccd16a..213c087 100644 --- a/app/credits.go +++ b/app/credits.go @@ -13,11 +13,7 @@ type Credits struct { func NewCredits() *Credits { return &Credits{ - ` ___ __ - / _/__ ___ / /__ - / _/ -_) -_) (_-< -/_/ \__/\__/_/___/ - neofeels 0.1.0`, + title, `ttbp written for tilde.town by ~endorphant in python neofeels written by ~nbsp in go @@ -25,7 +21,7 @@ tips for development of ttbp are accepted at https://liberapay.com/modgethanc/ tips for development of neofeels are accepted at https://liberapay.com/nbsp/ kind words are also extremely appreciated :)`, - "q exit", + "q return", } } @@ -42,6 +38,6 @@ func (credits *Credits) Event(state *ui.State, event vaxis.Event) (processed boo win := state.Window() win.New(win.Width/2-10, win.Height/2-8, 20, 5).Print(vaxis.Segment{Text: credits.title}) win.New(win.Width/2-40, win.Height/2-2, 80, 7).Print(vaxis.Segment{Text: credits.credits}) - win.New(win.Width/2-3, win.Height/2+6, 6, 1).Print(vaxis.Segment{Text: credits.help}) + win.New(win.Width/2-4, win.Height/2+6, 8, 1).Print(vaxis.Segment{Text: credits.help}) return } diff --git a/app/menu.go b/app/menu.go index be184fc..44e1009 100644 --- a/app/menu.go +++ b/app/menu.go @@ -3,6 +3,8 @@ package app import ( "os" "os/exec" + "path" + "time" "git.sr.ht/~rockorager/vaxis" "git.sr.ht/~rockorager/vaxis/widgets/list" @@ -16,13 +18,15 @@ type MainMenu struct { help string } -func NewMainMenu() *MainMenu { - return &MainMenu{ - ` ___ __ +const title = ` ___ __ / _/__ ___ / /__ / _/ -_) -_) (_-< /_/ \__/\__/_/___/ - neofeels 0.1.0`, + neofeels 0.1.0` + +func NewMainMenu() *MainMenu { + return &MainMenu{ + title, list.New([]string{ " record some feels ", " manage your feels ", @@ -54,6 +58,8 @@ func (menu *MainMenu) Event(state *ui.State, event vaxis.Event) (processed bool) menu.list.Home() case "Enter": switch menu.list.Index() { + case 0: + newFeels(state) case 8: ui.ViewChange <- NewCredits() case 9: @@ -106,3 +112,35 @@ func showManpage(state *ui.State) { } } } + +func newFeels(state *ui.State) { + state.HideCursor() + vt := term.New() + vt.TERM = os.Getenv("TERM") + vt.Attach(state.PostEvent()) + vt.Focus() + err := vt.Start(exec.Command(os.Getenv("EDITOR"), path.Join(os.Getenv("HOME"), ".ttbp/entries", time.Now().Format("20060102")+".txt"))) + if err != nil { + panic(err) + } + defer vt.Close() + + for ev := range state.Events() { + switch ev.(type) { + case term.EventClosed: + state.HideCursor() + state.Window().Clear() + ui.ViewChange <- NewPosted() + return + case vaxis.Redraw: + vt.Draw(state.Window()) + state.Render() + continue + } + + // for some reason vaxis doubles all events for Press/Release so this just ignores releases + if key, ok := ev.(vaxis.Key); ok && key.EventType == vaxis.EventPress { + vt.Update(ev) + } + } +} diff --git a/app/posted.go b/app/posted.go new file mode 100644 index 0000000..c8f3788 --- /dev/null +++ b/app/posted.go @@ -0,0 +1,50 @@ +package app + +import ( + "os" + "path" + "time" + + "git.sr.ht/~rockorager/vaxis" + "git.tilde.town/nbsp/neofeels/ui" +) + +type Posted struct { + title string + content string + help string +} + +func NewPosted() *Posted { + var content string + info, err := os.Stat(path.Join(os.Getenv("HOME"), ".ttbp/entries", time.Now().Format("20060102")+".txt")) + if os.IsNotExist(err) || info.IsDir() || info.Size() == 0 { + content = `your post is empty and was not published. +see you next time!` + } else { + content = `your post has been successfully published. +thanks for sharing your feels!` + } + return &Posted{ + title, + content, + "q return", + } +} + +func (posted *Posted) 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 "Enter", "q": + ui.ViewChange <- NewMainMenu() + } + processed = true + } + win := state.Window() + win.New(win.Width/2-10, win.Height/2-8, 20, 5).Print(vaxis.Segment{Text: posted.title}) + win.New(win.Width/2-21, win.Height/2-2, 43, 2).Print(vaxis.Segment{Text: posted.content}) + win.New(win.Width/2-4, win.Height/2+1, 8, 1).Print(vaxis.Segment{Text: posted.help}) + return +}