From 23d6ad2ab953f5988e6b88022c85a245c7c9717d Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 18 Aug 2023 13:17:46 +0200 Subject: [PATCH] Added persistence --- clock/clock.go | 9 +++------ editor/editor.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++- main.go | 22 ++++++++++++---------- 3 files changed, 63 insertions(+), 17 deletions(-) diff --git a/clock/clock.go b/clock/clock.go index a82e7c0..6c83c1c 100644 --- a/clock/clock.go +++ b/clock/clock.go @@ -25,11 +25,8 @@ func New(app *tview.Application) *tview.TextView { func refresh(app *tview.Application, view *tview.TextView) { tick := time.NewTicker(interval) - for { - select { - case <-tick.C: - view.SetText(time.Now().Format(format)) - app.Draw() - } + for range tick.C { + view.SetText(time.Now().Format(format)) + app.Draw() } } diff --git a/editor/editor.go b/editor/editor.go index d2d19c1..cd045ed 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -1,6 +1,9 @@ package editor import ( + "os" + "path/filepath" + "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) @@ -9,6 +12,50 @@ func New(app *tview.Application) *tview.TextArea { view := tview.NewTextArea() view.SetBackgroundColor(tcell.ColorDefault) view.SetTextStyle(tcell.StyleDefault) - view.SetText("Having been erased,\nThe document you're seeking\nMust now be retyped.", true) + view.SetBorderPadding(0, 0, 1, 1) + source := "Having been erased,\nThe document you're seeking\nMust now be retyped." + + home, err := os.UserHomeDir() + + if err != nil { + panic(err) + } + + data, err := os.ReadFile(filepath.Join(home, ".dash")) + + if err == nil { + source = string(data) + } + + view.SetText(source, true) + + view.SetChangedFunc(func() { + source = view.GetText() + markDirty(view, true) + }) + + view.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyCtrlS { + err := os.WriteFile(filepath.Join(home, ".dash"), []byte(source), 0644) + + if err != nil { + panic(err) + } + + markDirty(view, false) + return nil + } + + return event + }) + return view } + +func markDirty(view *tview.TextArea, dirty bool) { + if dirty { + view.SetBackgroundColor(tcell.ColorRed) + } else { + view.SetBackgroundColor(tcell.ColorDefault) + } +} diff --git a/main.go b/main.go index b15fced..07934c6 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ package main import ( "git.akyoto.dev/cli/dash/clock" "git.akyoto.dev/cli/dash/editor" - "git.akyoto.dev/cli/dash/empty" + //"git.akyoto.dev/cli/dash/empty" "git.akyoto.dev/cli/dash/osinfo" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" @@ -15,8 +15,8 @@ func main() { header := clock.New(app) main := editor.New(app) footer := osinfo.New(app) - left := empty.New(app) - right := empty.New(app) + //left := empty.New(app) + //right := empty.New(app) grid := tview.NewGrid() grid.SetRows(3, 0, 3) @@ -25,18 +25,20 @@ func main() { grid.SetBackgroundColor(tcell.ColorDefault) grid.AddItem(header, 0, 0, 1, 3, 0, 0, false) + grid.AddItem(main, 1, 1, 1, 1, 0, 0, false) grid.AddItem(footer, 2, 0, 1, 3, 0, 0, false) // Layout for screens narrower than 100 cells (menu and side bar are hidden). - grid.AddItem(left, 0, 0, 0, 0, 0, 0, false) - grid.AddItem(main, 1, 0, 1, 3, 0, 0, false) - grid.AddItem(right, 0, 0, 0, 0, 0, 0, false) + //grid.AddItem(left, 0, 0, 0, 0, 0, 0, false) + //grid.AddItem(main, 1, 0, 1, 3, 0, 0, false) + //grid.AddItem(right, 0, 0, 0, 0, 0, 0, false) - // Layout for screens wider than 100 cells. - grid.AddItem(left, 1, 0, 1, 1, 0, 100, false) - grid.AddItem(main, 1, 1, 1, 1, 0, 100, false) - grid.AddItem(right, 1, 2, 1, 1, 0, 100, false) + // Layout for screens wider than 80 cells. + //grid.AddItem(left, 1, 0, 1, 1, 0, 80, false) + //grid.AddItem(main, 1, 1, 1, 1, 0, 80, false) + //grid.AddItem(right, 1, 2, 1, 1, 0, 80, false) + app.EnableMouse(true) app.SetRoot(grid, true) app.SetFocus(main)