]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/ui.go
Add support for account login rename
[rbdr/mobius] / hotline / ui.go
index 45c452cbbeaea6d2aec2788e0c7e60573b8daa63..d9372d1937d0beb61ec000f93861cc291a7dd9c0 100644 (file)
@@ -1,6 +1,7 @@
 package hotline
 
 import (
+       "context"
        "fmt"
        "github.com/gdamore/tcell/v2"
        "github.com/rivo/tview"
@@ -102,12 +103,7 @@ func (ui *UI) showBookmarks() *tview.List {
        return list
 }
 
-func (ui *UI) getTrackerList() *tview.List {
-       listing, err := GetListing(ui.HLClient.Pref.Tracker)
-       if err != nil {
-               // TODO
-       }
-
+func (ui *UI) getTrackerList(servers []ServerRecord) *tview.List {
        list := tview.NewList()
        list.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
                if event.Key() == tcell.KeyEsc {
@@ -117,14 +113,13 @@ func (ui *UI) getTrackerList() *tview.List {
        })
        list.Box.SetBorder(true).SetTitle("| Servers |")
 
-       shortcut := 97 // rune for "a"
-       for i, srv := range listing {
-               addr := srv.Addr()
-               srvName := srv.Name
+       const shortcut = 97 // rune for "a"
+       for i, _ := range servers {
+               srv := servers[i]
                list.AddItem(string(srv.Name), string(srv.Description), rune(shortcut+i), func() {
                        ui.Pages.RemovePage("joinServer")
 
-                       newJS := ui.renderJoinServerForm(string(srvName), addr, GuestAccount, "", trackerListPage, false, true)
+                       newJS := ui.renderJoinServerForm(string(srv.Name), srv.Addr(), GuestAccount, "", trackerListPage, false, true)
 
                        ui.Pages.AddPage("joinServer", newJS, true, true)
                        ui.Pages.ShowPage("joinServer")
@@ -197,7 +192,7 @@ func (ui *UI) joinServer(addr, login, password string) error {
        }
 
        go func() {
-               if err := ui.HLClient.HandleTransactions(); err != nil {
+               if err := ui.HLClient.HandleTransactions(context.TODO()); err != nil {
                        ui.Pages.SwitchToPage("home")
                }
 
@@ -219,17 +214,17 @@ func (ui *UI) joinServer(addr, login, password string) error {
 func (ui *UI) renderJoinServerForm(name, server, login, password, backPage string, save, defaultConnect bool) *tview.Flex {
        joinServerForm := tview.NewForm()
        joinServerForm.
-               //      AddInputField("Name", server, 0, func(textToCheck string, lastChar rune) bool {
-               //      return false
-               // }, nil).
                AddInputField("Server", server, 0, nil, nil).
                AddInputField("Login", login, 0, nil, nil).
                AddPasswordField("Password", password, 0, '*', nil).
                AddCheckbox("Save", save, func(checked bool) {
-                       ui.HLClient.Logger.Infow("saving bookmark")
-                       // TODO: Implement bookmark saving
+                       ui.HLClient.Pref.AddBookmark(
+                               joinServerForm.GetFormItem(0).(*tview.InputField).GetText(),
+                               joinServerForm.GetFormItem(0).(*tview.InputField).GetText(),
+                               joinServerForm.GetFormItem(1).(*tview.InputField).GetText(),
+                               joinServerForm.GetFormItem(2).(*tview.InputField).GetText(),
+                       )
 
-                       ui.HLClient.Pref.AddBookmark(joinServerForm.GetFormItem(0).(*tview.InputField).GetText(), joinServerForm.GetFormItem(0).(*tview.InputField).GetText(), joinServerForm.GetFormItem(1).(*tview.InputField).GetText(), joinServerForm.GetFormItem(2).(*tview.InputField).GetText())
                        out, err := yaml.Marshal(ui.HLClient.Pref)
                        if err != nil {
                                panic(err)
@@ -239,7 +234,6 @@ func (ui *UI) renderJoinServerForm(name, server, login, password, backPage strin
                        if err != nil {
                                panic(err)
                        }
-                       //              Pref := ui.HLClient.Pref
                }).
                AddButton("Cancel", func() {
                        ui.Pages.SwitchToPage(backPage)
@@ -258,7 +252,7 @@ func (ui *UI) renderJoinServerForm(name, server, login, password, backPage strin
                        ui.HLClient.serverName = name
 
                        if err != nil {
-                               ui.HLClient.Logger.Errorw("login error", "err", err)
+                               ui.HLClient.Logger.Error("login error", "err", err)
                                loginErrModal := tview.NewModal().
                                        AddButtons([]string{"Oh no"}).
                                        SetText(err.Error()).
@@ -337,20 +331,19 @@ func (ui *UI) renderServerUI() *tview.Flex {
                // List files
                if event.Key() == tcell.KeyCtrlF {
                        if err := ui.HLClient.Send(*NewTransaction(TranGetFileNameList, nil)); err != nil {
-                               ui.HLClient.Logger.Errorw("err", "err", err)
+                               ui.HLClient.Logger.Error("err", "err", err)
                        }
                }
 
                // Show News
                if event.Key() == tcell.KeyCtrlN {
                        if err := ui.HLClient.Send(*NewTransaction(TranGetMsgs, nil)); err != nil {
-                               ui.HLClient.Logger.Errorw("err", "err", err)
+                               ui.HLClient.Logger.Error("err", "err", err)
                        }
                }
 
                // Post news
                if event.Key() == tcell.KeyCtrlP {
-
                        newsFlex := tview.NewFlex()
                        newsFlex.SetBorderPadding(0, 0, 1, 1)
                        newsPostTextArea := tview.NewTextView()
@@ -380,7 +373,7 @@ func (ui *UI) renderServerUI() *tview.Flex {
                                                ),
                                        )
                                        if err != nil {
-                                               ui.HLClient.Logger.Errorw("Error posting news", "err", err)
+                                               ui.HLClient.Logger.Error("Error posting news", "err", err)
                                                // TODO: display errModal to user
                                        }
                                        ui.Pages.RemovePage("newsInput")
@@ -413,7 +406,7 @@ func (ui *UI) renderServerUI() *tview.Flex {
                                                        newsPostTextArea.SetText(curTxt)
                                                }
                                        default:
-                                               _, _ = fmt.Fprintf(newsPostTextArea, string(event.Rune()))
+                                               _, _ = fmt.Fprint(newsPostTextArea, string(event.Rune()))
                                        }
                                }
 
@@ -470,7 +463,20 @@ func (ui *UI) Start() {
                        ui.Pages.AddAndSwitchToPage("bookmarks", ui.showBookmarks(), true)
                }).
                AddItem("Browse Tracker", "", 't', func() {
-                       ui.trackerList = ui.getTrackerList()
+                       listing, err := GetListing(ui.HLClient.Pref.Tracker)
+                       if err != nil {
+                               errMsg := fmt.Sprintf("Error fetching tracker results:\n%v", err)
+                               errModal := tview.NewModal()
+                               errModal.SetText(errMsg)
+                               errModal.AddButtons([]string{"Cancel"})
+                               errModal.SetDoneFunc(func(buttonIndex int, buttonLabel string) {
+                                       ui.Pages.RemovePage("errModal")
+                               })
+                               ui.Pages.RemovePage("joinServer")
+                               ui.Pages.AddPage("errModal", errModal, false, true)
+                               return
+                       }
+                       ui.trackerList = ui.getTrackerList(listing)
                        ui.Pages.AddAndSwitchToPage("trackerList", ui.trackerList, true)
                }).
                AddItem("Settings", "", 's', func() {
@@ -485,7 +491,7 @@ func (ui *UI) Start() {
        // App level input capture
        ui.App.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
                if event.Key() == tcell.KeyCtrlC {
-                       ui.HLClient.Logger.Infow("Exiting")
+                       ui.HLClient.Logger.Info("Exiting")
                        ui.App.Stop()
                        os.Exit(0)
                }