Browse Source

Cleaned up the output and organization of tui a little

arianagiroux 3 weeks ago
parent
commit
b03db393f1
2 changed files with 55 additions and 49 deletions
  1. 49 44
      tui.go
  2. 6 5
      tui_test.go

+ 49 - 44
tui.go

@@ -15,6 +15,40 @@ import (
 	"github.com/charmbracelet/lipgloss"
 )
 
+// [lipgloss] style definitions, stores the currently displayed "widget"
+var (
+	titleStyle = lipgloss.NewStyle().
+			Bold(true).
+			Underline(true)
+
+	statusStyle = lipgloss.NewStyle().
+			Faint(true).
+			Italic(true)
+
+	variadicTitleStyle = lipgloss.NewStyle().
+				Align(lipgloss.Left).
+				Italic(true)
+
+	variadicDataStyle = lipgloss.NewStyle().
+				Width(40).
+				BorderStyle(lipgloss.ASCIIBorder())
+
+	borderStyle = lipgloss.NewStyle().
+			Padding(1, 2).
+			Margin(1).
+			BorderStyle(lipgloss.NormalBorder())
+
+	indexStyle = lipgloss.NewStyle().
+			Italic(true)
+
+	pointerStyle = lipgloss.NewStyle().
+			Faint(true)
+
+	collectionStyleLeft = lipgloss.NewStyle().
+				Align(lipgloss.Left)
+)
+
+// interface for renderable structs
 type widget interface {
 	view() tea.Msg
 }
@@ -27,6 +61,7 @@ type Model struct {
 	// viewport viewport.Model
 }
 
+// The bubbletea init function
 func (m Model) Init() tea.Cmd { return m.load }
 
 // Handles quit logic and viewport scroll and size updates
@@ -36,7 +71,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 		switch msg.String() {
 		case "q":
 			return m, tea.Quit
-		case "k":
+		case "j":
 			if collection, ok := m.widget.(IssueCollection); ok {
 				if collection.selection+1 < len(collection.Collection) {
 					collection.selection = collection.selection + 1
@@ -47,7 +82,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 				return m, collection.view
 			}
 			return m, nil
-		case "j":
+		case "k":
 			// do something only if widget is collection
 			if collection, ok := m.widget.(IssueCollection); ok {
 				if collection.selection != 0 {
@@ -80,6 +115,18 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 	return m, nil
 }
 
+// Handles top level view functionality
+func (m Model) View() string {
+	var output string
+	if len(m.content) == 0 {
+		return "loading..."
+	} else {
+		output = output + m.content
+	}
+	output = output + "\nj/k: down/up\tenter: select\tq: quit"
+	return output
+}
+
 // Handles load logic
 func (m Model) load() tea.Msg {
 	if IsIssue(m.Path) {
@@ -105,39 +152,6 @@ func (m Model) load() tea.Msg {
 	return nil
 }
 
-// [lipgloss] style definitions
-var (
-	titleStyle = lipgloss.NewStyle().
-			Bold(true).
-			Underline(true)
-
-	statusStyle = lipgloss.NewStyle().
-			Faint(true).
-			Italic(true)
-
-	variadicTitleStyle = lipgloss.NewStyle().
-				Align(lipgloss.Left).
-				Italic(true)
-
-	variadicDataStyle = lipgloss.NewStyle().
-				Width(40).
-				BorderStyle(lipgloss.ASCIIBorder())
-
-	borderStyle = lipgloss.NewStyle().
-			Padding(1, 2).
-			Margin(1).
-			BorderStyle(lipgloss.NormalBorder())
-
-	indexStyle = lipgloss.NewStyle().
-			Italic(true)
-
-	pointerStyle = lipgloss.NewStyle().
-			Faint(true)
-
-	collectionStyleLeft = lipgloss.NewStyle().
-				Align(lipgloss.Left)
-)
-
 // Handles all view logic for Issue structs
 func (i Issue) view() tea.Msg {
 	var output string
@@ -197,14 +211,5 @@ func (ic IssueCollection) view() tea.Msg {
 	}
 
 	output = output + collectionStyleLeft.Render(left)
-	output = output + "\nj/k: down/up\tenter: select\tq: quit"
 	return output
 }
-
-// Wraps [issue.Model.renderIssue] in a viewport
-func (m Model) View() string {
-	if len(m.content) == 0 {
-		return "loading..."
-	}
-	return m.content
-}

+ 6 - 5
tui_test.go

@@ -34,10 +34,10 @@ func Test_Model_Update_quit_on_keymsg(t *testing.T) {
 	}
 }
 
-func Test_Model_Update_scroll_on_k(t *testing.T) {
+func Test_Model_Update_scroll_up(t *testing.T) {
 	ic, _ := IssueCollection.NewFromPath(IssueCollection{}, "tests/bugs/")
 	testModel := Model{widget: ic}
-	testKey := tea.Key{Type: tea.KeyRunes, Runes: []rune{'k'}}
+	testKey := tea.Key{Type: tea.KeyRunes, Runes: []rune{'j'}}
 	testMsg := tea.KeyMsg(testKey)
 
 	model, cmd := testModel.Update(testMsg)
@@ -60,10 +60,10 @@ func Test_Model_Update_scroll_on_k(t *testing.T) {
 	}
 }
 
-func Test_Model_Update_scroll_on_j(t *testing.T) {
+func Test_Model_Update_scroll_down(t *testing.T) {
 	ic, _ := IssueCollection.NewFromPath(IssueCollection{}, "tests/bugs/")
 	testModel := Model{widget: ic}
-	testKey := tea.Key{Type: tea.KeyRunes, Runes: []rune{'j'}}
+	testKey := tea.Key{Type: tea.KeyRunes, Runes: []rune{'k'}}
 	testMsg := tea.KeyMsg(testKey)
 
 	model, cmd := testModel.Update(testMsg)
@@ -190,7 +190,8 @@ func Test_Model_View(t *testing.T) {
 		assert.Fail(t, "should not return cmd")
 	}
 
-	assert.Equal(t, testIssue.view().(string), model.(Model).View())
+	testView := testIssue.view().(string) + "\nj/k: down/up\tenter: select\tq: quit"
+	assert.Equal(t, testView, model.(Model).View())
 
 	render2 := Model{}.View()
 	assert.Equal(t, "loading...", render2)