Bläddra i källkod

refactor: remove ui pkg

ayntgl 4 år sedan
förälder
incheckning
b8d09403e7
6 ändrade filer med 63 tillägg och 79 borttagningar
  1. 5 6
      main.go
  2. 58 0
      ui.go
  3. 0 16
      ui/channels_tree.go
  4. 0 22
      ui/login_form.go
  5. 0 19
      ui/message_input_field.go
  6. 0 16
      ui/messages_view.go

+ 5 - 6
main.go

@@ -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 - 0
ui.go

@@ -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
+}

+ 0 - 16
ui/channels_tree.go

@@ -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
-}

+ 0 - 22
ui/login_form.go

@@ -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
-}

+ 0 - 19
ui/message_input_field.go

@@ -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
-}

+ 0 - 16
ui/messages_view.go

@@ -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
-}