X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/854a92fc2755ace61c405df335ddf69b02a3d932..22c5767f56b55b0327d9ea75a964abf95c684794:/hotline/ui.go diff --git a/hotline/ui.go b/hotline/ui.go index 709137e..c005fb4 100644 --- a/hotline/ui.go +++ b/hotline/ui.go @@ -1,7 +1,6 @@ package hotline import ( - "bufio" "fmt" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" @@ -49,8 +48,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 +87,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 +104,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,25 +136,27 @@ 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 } @@ -192,47 +193,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,8 +230,8 @@ 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) } @@ -261,7 +240,7 @@ func (ui *UI) renderJoinServerForm(name, server, login, password, backPage strin if err != nil { panic(err) } - // pref := ui.HLClient.pref + // Pref := ui.HLClient.Pref }). AddButton("Cancel", func() { ui.Pages.SwitchToPage(backPage) @@ -358,14 +337,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 +376,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 {