Added persistence

This commit is contained in:
Eduard Urbach 2023-08-18 13:17:46 +02:00
parent 78b9f1f2e7
commit 23d6ad2ab9
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
3 changed files with 63 additions and 17 deletions

View File

@ -25,11 +25,8 @@ func New(app *tview.Application) *tview.TextView {
func refresh(app *tview.Application, view *tview.TextView) { func refresh(app *tview.Application, view *tview.TextView) {
tick := time.NewTicker(interval) tick := time.NewTicker(interval)
for { for range tick.C {
select {
case <-tick.C:
view.SetText(time.Now().Format(format)) view.SetText(time.Now().Format(format))
app.Draw() app.Draw()
} }
} }
}

View File

@ -1,6 +1,9 @@
package editor package editor
import ( import (
"os"
"path/filepath"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/rivo/tview" "github.com/rivo/tview"
) )
@ -9,6 +12,50 @@ func New(app *tview.Application) *tview.TextArea {
view := tview.NewTextArea() view := tview.NewTextArea()
view.SetBackgroundColor(tcell.ColorDefault) view.SetBackgroundColor(tcell.ColorDefault)
view.SetTextStyle(tcell.StyleDefault) 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 return view
} }
func markDirty(view *tview.TextArea, dirty bool) {
if dirty {
view.SetBackgroundColor(tcell.ColorRed)
} else {
view.SetBackgroundColor(tcell.ColorDefault)
}
}

22
main.go
View File

@ -3,7 +3,7 @@ package main
import ( import (
"git.akyoto.dev/cli/dash/clock" "git.akyoto.dev/cli/dash/clock"
"git.akyoto.dev/cli/dash/editor" "git.akyoto.dev/cli/dash/editor"
"git.akyoto.dev/cli/dash/empty" //"git.akyoto.dev/cli/dash/empty"
"git.akyoto.dev/cli/dash/osinfo" "git.akyoto.dev/cli/dash/osinfo"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/rivo/tview" "github.com/rivo/tview"
@ -15,8 +15,8 @@ func main() {
header := clock.New(app) header := clock.New(app)
main := editor.New(app) main := editor.New(app)
footer := osinfo.New(app) footer := osinfo.New(app)
left := empty.New(app) //left := empty.New(app)
right := empty.New(app) //right := empty.New(app)
grid := tview.NewGrid() grid := tview.NewGrid()
grid.SetRows(3, 0, 3) grid.SetRows(3, 0, 3)
@ -25,18 +25,20 @@ func main() {
grid.SetBackgroundColor(tcell.ColorDefault) grid.SetBackgroundColor(tcell.ColorDefault)
grid.AddItem(header, 0, 0, 1, 3, 0, 0, false) 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) grid.AddItem(footer, 2, 0, 1, 3, 0, 0, false)
// Layout for screens narrower than 100 cells (menu and side bar are hidden). // 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(left, 0, 0, 0, 0, 0, 0, false)
grid.AddItem(main, 1, 0, 1, 3, 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(right, 0, 0, 0, 0, 0, 0, false)
// Layout for screens wider than 100 cells. // Layout for screens wider than 80 cells.
grid.AddItem(left, 1, 0, 1, 1, 0, 100, false) //grid.AddItem(left, 1, 0, 1, 1, 0, 80, false)
grid.AddItem(main, 1, 1, 1, 1, 0, 100, false) //grid.AddItem(main, 1, 1, 1, 1, 0, 80, false)
grid.AddItem(right, 1, 2, 1, 1, 0, 100, false) //grid.AddItem(right, 1, 2, 1, 1, 0, 80, false)
app.EnableMouse(true)
app.SetRoot(grid, true) app.SetRoot(grid, true)
app.SetFocus(main) app.SetFocus(main)