X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/d4c152a4dba0eec7c8ecd13732900909f51b1c97..c519856703dbb94c58ec070a103c8df72ab98b19:/hotline/ui.go?ds=sidebyside diff --git a/hotline/ui.go b/hotline/ui.go index b9e45dd..8b0a8c5 100644 --- a/hotline/ui.go +++ b/hotline/ui.go @@ -1,11 +1,11 @@ package hotline import ( + "bufio" "fmt" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" "gopkg.in/yaml.v3" - "io" "io/ioutil" "os" "strconv" @@ -145,6 +145,7 @@ func (ui *UI) renderSettingsForm() *tview.Flex { return err == 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 { @@ -154,6 +155,7 @@ func (ui *UI) renderSettingsForm() *tview.Flex { 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.EnableBell = settingsForm.GetFormItem(3).(*tview.Checkbox).IsChecked() out, err := yaml.Marshal(&ui.HLClient.pref) if err != nil { @@ -197,29 +199,42 @@ func (ui *UI) joinServer(addr, login, password string) error { } go func() { - for { - err := ui.HLClient.ReadLoop() + // 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 { - ui.HLClient.Logger.Errorw("read error", "err", err) - - if err == io.EOF { - 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") - - return + break + } + if err := ui.HLClient.HandleTransaction(&t); err != nil { + ui.HLClient.Logger.Errorw("Error handling transaction", "err", err) } } + + 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") + }() return nil @@ -324,6 +339,7 @@ func (ui *UI) renderServerUI() *tview.Flex { if buttonIndex == 1 { _ = ui.HLClient.Disconnect() ui.Pages.RemovePage(pageServerUI) + ui.Pages.SwitchToPage("home") } else { ui.Pages.HidePage("modal") }