]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/ui.go
Replace deprecated iotuil usage
[rbdr/mobius] / hotline / ui.go
index 709137e6de6d7c2a33b0279df90345a23d373a69..45c452cbbeaea6d2aec2788e0c7e60573b8daa63 100644 (file)
@@ -1,12 +1,10 @@
 package hotline
 
 import (
-       "bufio"
        "fmt"
        "github.com/gdamore/tcell/v2"
        "github.com/rivo/tview"
        "gopkg.in/yaml.v3"
-       "io/ioutil"
        "os"
        "strconv"
        "strings"
@@ -49,8 +47,8 @@ func NewUI(c *Client) *UI {
                        }
 
                        _ = c.Send(
-                               *NewTransaction(tranChatSend, nil,
-                                       NewField(fieldData, []byte(chatInput.GetText())),
+                               *NewTransaction(TranChatSend, nil,
+                                       NewField(FieldData, []byte(chatInput.GetText())),
                                ),
                        )
                        chatInput.SetText("") // clear the input field after chat send
@@ -88,7 +86,7 @@ func (ui *UI) showBookmarks() *tview.List {
        list.Box.SetBorder(true).SetTitle("| Bookmarks |")
 
        shortcut := 97 // rune for "a"
-       for i, srv := range ui.HLClient.pref.Bookmarks {
+       for i, srv := range ui.HLClient.Pref.Bookmarks {
                addr := srv.Addr
                login := srv.Login
                pass := srv.Password
@@ -105,7 +103,7 @@ func (ui *UI) showBookmarks() *tview.List {
 }
 
 func (ui *UI) getTrackerList() *tview.List {
-       listing, err := GetListing(ui.HLClient.pref.Tracker)
+       listing, err := GetListing(ui.HLClient.Pref.Tracker)
        if err != nil {
                // TODO
        }
@@ -137,30 +135,32 @@ func (ui *UI) getTrackerList() *tview.List {
 }
 
 func (ui *UI) renderSettingsForm() *tview.Flex {
-       iconStr := strconv.Itoa(ui.HLClient.pref.IconID)
+       iconStr := strconv.Itoa(ui.HLClient.Pref.IconID)
        settingsForm := tview.NewForm()
-       settingsForm.AddInputField("Your Name", ui.HLClient.pref.Username, 0, nil, nil)
+       settingsForm.AddInputField("Your Name", ui.HLClient.Pref.Username, 0, nil, nil)
        settingsForm.AddInputField("IconID", iconStr, 0, func(idStr string, _ rune) bool {
                _, err := strconv.Atoi(idStr)
                return err == nil
        }, nil)
-       settingsForm.AddInputField("Tracker", ui.HLClient.pref.Tracker, 0, nil, nil)
+       settingsForm.AddInputField("Tracker", ui.HLClient.Pref.Tracker, 0, nil, nil)
+       settingsForm.AddCheckbox("Enable Terminal Bell", ui.HLClient.Pref.EnableBell, nil)
        settingsForm.AddButton("Save", func() {
                usernameInput := settingsForm.GetFormItem(0).(*tview.InputField).GetText()
                if len(usernameInput) == 0 {
                        usernameInput = "unnamed"
                }
-               ui.HLClient.pref.Username = usernameInput
+               ui.HLClient.Pref.Username = usernameInput
                iconStr = settingsForm.GetFormItem(1).(*tview.InputField).GetText()
-               ui.HLClient.pref.IconID, _ = strconv.Atoi(iconStr)
-               ui.HLClient.pref.Tracker = settingsForm.GetFormItem(2).(*tview.InputField).GetText()
+               ui.HLClient.Pref.IconID, _ = strconv.Atoi(iconStr)
+               ui.HLClient.Pref.Tracker = settingsForm.GetFormItem(2).(*tview.InputField).GetText()
+               ui.HLClient.Pref.EnableBell = settingsForm.GetFormItem(3).(*tview.Checkbox).IsChecked()
 
-               out, err := yaml.Marshal(&ui.HLClient.pref)
+               out, err := yaml.Marshal(&ui.HLClient.Pref)
                if err != nil {
                        // TODO: handle err
                }
                // TODO: handle err
-               err = ioutil.WriteFile(ui.HLClient.cfgPath, out, 0666)
+               err = os.WriteFile(ui.HLClient.cfgPath, out, 0666)
                if err != nil {
                        println(ui.HLClient.cfgPath)
                        panic(err)
@@ -192,47 +192,25 @@ func (ui *UI) joinServer(addr, login, password string) error {
        if len(strings.Split(addr, ":")) == 1 {
                addr += ":5500"
        }
-       if err := ui.HLClient.JoinServer(addr, login, password); err != nil {
+       if err := ui.HLClient.Connect(addr, login, password); err != nil {
                return fmt.Errorf("Error joining server: %v\n", err)
        }
 
        go func() {
-               // Create a new scanner for parsing incoming bytes into transaction tokens
-               scanner := bufio.NewScanner(ui.HLClient.Connection)
-               scanner.Split(transactionScanner)
-
-               // Scan for new transactions and handle them as they come in.
-               for scanner.Scan() {
-                       // Make a new []byte slice and copy the scanner bytes to it.  This is critical to avoid a data race as the
-                       // scanner re-uses the buffer for subsequent scans.
-                       buf := make([]byte, len(scanner.Bytes()))
-                       copy(buf, scanner.Bytes())
-
-                       var t Transaction
-                       _, err := t.Write(buf)
-                       if err != nil {
-                               break
-                       }
-                       if err := ui.HLClient.HandleTransaction(&t); err != nil {
-                               ui.HLClient.Logger.Errorw("Error handling transaction", "err", err)
-                       }
+               if err := ui.HLClient.HandleTransactions(); err != nil {
+                       ui.Pages.SwitchToPage("home")
                }
 
-               if scanner.Err() == nil {
-                       loginErrModal := tview.NewModal().
-                               AddButtons([]string{"Ok"}).
-                               SetText("The server connection has closed.").
-                               SetDoneFunc(func(buttonIndex int, buttonLabel string) {
-                                       ui.Pages.SwitchToPage("home")
-                               })
-                       loginErrModal.Box.SetTitle("Server Connection Error")
-
-                       ui.Pages.AddPage("loginErr", loginErrModal, false, true)
-                       ui.App.Draw()
-                       return
-               }
-               ui.Pages.SwitchToPage("home")
+               loginErrModal := tview.NewModal().
+                       AddButtons([]string{"Ok"}).
+                       SetText("The server connection has closed.").
+                       SetDoneFunc(func(buttonIndex int, buttonLabel string) {
+                               ui.Pages.SwitchToPage("home")
+                       })
+               loginErrModal.Box.SetTitle("Server Connection Error")
 
+               ui.Pages.AddPage("loginErr", loginErrModal, false, true)
+               ui.App.Draw()
        }()
 
        return nil
@@ -251,17 +229,17 @@ func (ui *UI) renderJoinServerForm(name, server, login, password, backPage strin
                        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())
-                       out, err := yaml.Marshal(ui.HLClient.pref)
+                       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)
                        }
 
-                       err = ioutil.WriteFile(ui.HLClient.cfgPath, out, 0666)
+                       err = os.WriteFile(ui.HLClient.cfgPath, out, 0666)
                        if err != nil {
                                panic(err)
                        }
-                       //              pref := ui.HLClient.pref
+                       //              Pref := ui.HLClient.Pref
                }).
                AddButton("Cancel", func() {
                        ui.Pages.SwitchToPage(backPage)
@@ -358,14 +336,14 @@ func (ui *UI) renderServerUI() *tview.Flex {
 
                // List files
                if event.Key() == tcell.KeyCtrlF {
-                       if err := ui.HLClient.Send(*NewTransaction(tranGetFileNameList, nil)); err != nil {
+                       if err := ui.HLClient.Send(*NewTransaction(TranGetFileNameList, nil)); err != nil {
                                ui.HLClient.Logger.Errorw("err", "err", err)
                        }
                }
 
                // Show News
                if event.Key() == tcell.KeyCtrlN {
-                       if err := ui.HLClient.Send(*NewTransaction(tranGetMsgs, nil)); err != nil {
+                       if err := ui.HLClient.Send(*NewTransaction(TranGetMsgs, nil)); err != nil {
                                ui.HLClient.Logger.Errorw("err", "err", err)
                        }
                }
@@ -397,8 +375,8 @@ func (ui *UI) renderServerUI() *tview.Flex {
                                                return event
                                        }
                                        err := ui.HLClient.Send(
-                                               *NewTransaction(tranOldPostNews, nil,
-                                                       NewField(fieldData, []byte(newsText)),
+                                               *NewTransaction(TranOldPostNews, nil,
+                                                       NewField(FieldData, []byte(newsText)),
                                                ),
                                        )
                                        if err != nil {