Просмотр исходного кода

Reworks Model.Update to more sensibly structure keypress reaction

arianagiroux 3 недель назад
Родитель
Сommit
17cb955f5d
1 измененных файлов с 51 добавлено и 32 удалено
  1. 51 32
      tui.go

+ 51 - 32
tui.go

@@ -67,47 +67,66 @@ func (m Model) Init() tea.Cmd { return m.load }
 
 // Handles quit logic and viewport scroll and size updates
 func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
+	// widget specifc keyhandling
+	switch m.widget.(type) {
+	case issueCreate:
+		if msg, ok := msg.(tea.KeyMsg); ok {
+			switch msg.String() {
+			case "enter": // TODO create, write, and render an issue on press enter
+				return m, tea.Quit
+			}
+		}
+	default:
+		if msg, ok := msg.(tea.KeyMsg); ok {
+			switch msg.String() {
+			case "q":
+				return m, tea.Quit
+			case "j":
+				if collection, ok := m.widget.(IssueCollection); ok {
+					if collection.selection+1 < len(collection.Collection) {
+						collection.selection = collection.selection + 1
+					} else {
+						collection.selection = 0
+					}
+					m.widget = collection
+					return m, collection.view
+				}
+				return m, nil
+			case "k":
+				// do something only if widget is collection
+				if collection, ok := m.widget.(IssueCollection); ok {
+					if collection.selection != 0 {
+						collection.selection = collection.selection - 1
+					} else {
+						collection.selection = len(collection.Collection) - 1
+					}
+					m.widget = collection
+					return m, collection.view
+				}
+				return m, nil
+			case "enter":
+				if _, ok := m.widget.(IssueCollection); ok {
+					m.Path = m.widget.(IssueCollection).Collection[m.widget.(IssueCollection).selection].Path
+					return m, m.load
+				}
+				return m, nil
+			}
+		}
+	}
+
 	switch msg := msg.(type) {
 	case tea.KeyMsg:
 		switch msg.String() {
-		case "q":
+		case "ctrl+c":
 			return m, tea.Quit
-		case "j":
-			if collection, ok := m.widget.(IssueCollection); ok {
-				if collection.selection+1 < len(collection.Collection) {
-					collection.selection = collection.selection + 1
-				} else {
-					collection.selection = 0
-				}
-				m.widget = collection
-				return m, collection.view
-			}
-			return m, nil
-		case "k":
-			// do something only if widget is collection
-			if collection, ok := m.widget.(IssueCollection); ok {
-				if collection.selection != 0 {
-					collection.selection = collection.selection - 1
-				} else {
-					collection.selection = len(collection.Collection) - 1
-				}
-				m.widget = collection
-				return m, collection.view
-			}
-			return m, nil
-		case "enter":
-			if _, ok := m.widget.(IssueCollection); ok {
-				m.Path = m.widget.(IssueCollection).Collection[m.widget.(IssueCollection).selection].Path
-				return m, m.load
-			}
-			return m, nil
 		}
 	case widget:
 		switch T := msg.(type) {
-		case Issue:
+		default:
 			m.widget = T
 			return m, T.view
-		case IssueCollection:
+		case issueCreate:
+			T = T.init(Issue{Path: m.Path})
 			m.widget = T
 			return m, T.view
 		}