package hotline
import (
+ "bufio"
"fmt"
- "github.com/davecgh/go-spew/spew"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
- "gopkg.in/yaml.v2"
- "io"
+ "gopkg.in/yaml.v3"
"io/ioutil"
"os"
"strconv"
func (ui *UI) getTrackerList() *tview.List {
listing, err := GetListing(ui.HLClient.pref.Tracker)
if err != nil {
- spew.Dump(err)
+ // TODO
}
list := tview.NewList()
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 {
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 {
}
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
if buttonIndex == 1 {
_ = ui.HLClient.Disconnect()
ui.Pages.RemovePage(pageServerUI)
+ ui.Pages.SwitchToPage("home")
} else {
ui.Pages.HidePage("modal")
}