refactor: remove ui pkg

This commit is contained in:
ayntgl 2021-11-20 20:11:33 +04:00
parent c82acc2121
commit b8d09403e7
6 changed files with 63 additions and 79 deletions

11
main.go
View file

@ -2,7 +2,6 @@ package main
import (
"github.com/ayntgl/discordgo"
"github.com/ayntgl/discordo/ui"
"github.com/ayntgl/discordo/util"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
@ -45,17 +44,17 @@ func main() {
EnableMouse(conf.Mouse).
SetInputCapture(onAppInputCapture)
channelsTree = ui.NewChannelsTree()
channelsTree = newChannelsTree()
channelsTree.SetSelectedFunc(onChannelsTreeSelected)
messagesView = ui.NewMessagesView()
messagesView = newMessagesView()
messagesView.
SetChangedFunc(func() {
app.Draw()
}).
SetInputCapture(onMessagesViewInputCapture)
messageInputField = ui.NewMessageInputField()
messageInputField = newMessageInputField()
messageInputField.SetInputCapture(onMessageInputFieldInputCapture)
rightFlex := tview.NewFlex().
@ -83,7 +82,7 @@ func main() {
panic(err)
}
} else {
loginForm = ui.NewLoginForm(onLoginFormLoginButtonSelected, false)
loginForm = newLoginForm(onLoginFormLoginButtonSelected, false)
app.SetRoot(loginForm, true)
}
@ -120,7 +119,7 @@ func onLoginFormLoginButtonSelected() {
go keyring.Set(service, "token", lr.Token)
} else if lr.MFA {
// The account has MFA enabled, reattempt login with code and ticket.
loginForm = ui.NewLoginForm(func() {
loginForm = newLoginForm(func() {
code := loginForm.GetFormItem(0).(*tview.InputField).GetText()
if code == "" {
return

58
ui.go
View file

@ -31,6 +31,19 @@ func onAppInputCapture(e *tcell.EventKey) *tcell.EventKey {
return e
}
func newChannelsTree() *tview.TreeView {
treeView := tview.NewTreeView()
treeView.
SetTopLevel(1).
SetRoot(tview.NewTreeNode("")).
SetTitle("Channels").
SetTitleAlign(tview.AlignLeft).
SetBorder(true).
SetBorderPadding(0, 0, 1, 0)
return treeView
}
func onChannelsTreeSelected(n *tview.TreeNode) {
selectedChannel = nil
selectedMessage = 0
@ -128,6 +141,19 @@ func onChannelsTreeSelected(n *tview.TreeNode) {
}
}
func newMessagesView() *tview.TextView {
textView := tview.NewTextView()
textView.
SetRegions(true).
SetDynamicColors(true).
SetWordWrap(true).
SetBorder(true).
SetBorderPadding(0, 0, 1, 0).
SetTitleAlign(tview.AlignLeft)
return textView
}
func onMessagesViewInputCapture(e *tcell.EventKey) *tcell.EventKey {
if selectedChannel == nil {
return nil
@ -231,6 +257,19 @@ func onMessagesViewInputCapture(e *tcell.EventKey) *tcell.EventKey {
return e
}
func newMessageInputField() *tview.InputField {
inputField := tview.NewInputField()
inputField.
SetPlaceholder("Message...").
SetPlaceholderTextColor(tcell.ColorWhite).
SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor).
SetBorder(true).
SetBorderPadding(0, 0, 1, 0).
SetTitleAlign(tview.AlignLeft)
return inputField
}
func onMessageInputFieldInputCapture(e *tcell.EventKey) *tcell.EventKey {
switch e.Key() {
case tcell.KeyEnter:
@ -284,3 +323,22 @@ func onMessageInputFieldInputCapture(e *tcell.EventKey) *tcell.EventKey {
return e
}
func newLoginForm(onLoginFormLoginButtonSelected func(), mfa bool) *tview.Form {
w := tview.NewForm()
w.
AddButton("Login", onLoginFormLoginButtonSelected).
SetButtonsAlign(tview.AlignCenter).
SetBorder(true).
SetBorderPadding(0, 0, 1, 0)
if mfa {
w.AddPasswordField("Code", "", 0, 0, nil)
} else {
w.
AddInputField("Email", "", 0, nil, nil).
AddPasswordField("Password", "", 0, 0, nil)
}
return w
}

View file

@ -1,16 +0,0 @@
package ui
import "github.com/rivo/tview"
func NewChannelsTree() *tview.TreeView {
treeView := tview.NewTreeView()
treeView.
SetTopLevel(1).
SetRoot(tview.NewTreeNode("")).
SetTitle("Channels").
SetTitleAlign(tview.AlignLeft).
SetBorder(true).
SetBorderPadding(0, 0, 1, 0)
return treeView
}

View file

@ -1,22 +0,0 @@
package ui
import "github.com/rivo/tview"
func NewLoginForm(onLoginFormLoginButtonSelected func(), mfa bool) *tview.Form {
w := tview.NewForm()
w.
AddButton("Login", onLoginFormLoginButtonSelected).
SetButtonsAlign(tview.AlignCenter).
SetBorder(true).
SetBorderPadding(0, 0, 1, 0)
if mfa {
w.AddPasswordField("Code", "", 0, 0, nil)
} else {
w.
AddInputField("Email", "", 0, nil, nil).
AddPasswordField("Password", "", 0, 0, nil)
}
return w
}

View file

@ -1,19 +0,0 @@
package ui
import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
func NewMessageInputField() *tview.InputField {
inputField := tview.NewInputField()
inputField.
SetPlaceholder("Message...").
SetPlaceholderTextColor(tcell.ColorWhite).
SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor).
SetBorder(true).
SetBorderPadding(0, 0, 1, 0).
SetTitleAlign(tview.AlignLeft)
return inputField
}

View file

@ -1,16 +0,0 @@
package ui
import "github.com/rivo/tview"
func NewMessagesView() *tview.TextView {
textView := tview.NewTextView()
textView.
SetRegions(true).
SetDynamicColors(true).
SetWordWrap(true).
SetBorder(true).
SetBorderPadding(0, 0, 1, 0).
SetTitleAlign(tview.AlignLeft)
return textView
}