Prechádzať zdrojové kódy

Implemented "setTitle" widget, deprecates createInCollection widget

arianagiroux 1 týždeň pred
rodič
commit
86eb3fee3f
1 zmenil súbory, kde vykonal 27 pridanie a 25 odobranie
  1. 27 25
      tui.go

+ 27 - 25
tui.go

@@ -119,15 +119,15 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 	case loadPath:
 		m.Path = string(msg)
 		return m, m.load
-	case createInCollectionMsg:
-		wg := createInCollection{Path: string(msg)}
+	case setTitleMsg:
+		wg := setTitle{Path: string(msg)}
 		i := textinput.New()
 		i.Placeholder = "a short title"
 		i.Focus()
 		i.CharLimit = 80
 		i.Width = 30
 		wg.input = i
-		return m, func() tea.Msg { return wg }
+		cmds = append(cmds, func() tea.Msg { return wg })
 	case string:
 		m.content = msg
 		m.viewport.SetContent(m.content)
@@ -146,7 +146,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 	case IssueCollection:
 		m.widget, cmd = w.update(msg)
 		cmds = append(cmds, cmd)
-	case createInCollection:
+	case setTitle:
 		m.widget, cmd = w.update(msg)
 		cmds = append(cmds, cmd)
 	}
@@ -214,7 +214,7 @@ func (m Model) header() string {
 	left := "tissues v0.0"
 	left = borderStyle.Render(left)
 
-	line := strings.Repeat("─", max(0, m.viewport.Width()-lipgloss.Width(left)))//                                   -lipgloss.Width(right)))
+	line := strings.Repeat("─", max(0, m.viewport.Width()-lipgloss.Width(left)))
 	line = lipgloss.NewStyle().Foreground(lipgloss.Color("8")).Render(line)
 	return lipgloss.JoinHorizontal(lipgloss.Center, line, left)
 }
@@ -668,8 +668,8 @@ func (i Issue) keyhelp() string {
 // ----------------------------------------------------------------------------
 
 type ( // Type definitions for use in tea.Msg life cycle for IssueCollection widget.
-	loadPath              string // thrown when user selects a path to load.
-	createInCollectionMsg string //thrown when user opts to create new issue in collection
+	loadPath    string // thrown when user selects a path to load.
+	setTitleMsg string //thrown when user opts to create new issue in collection
 )
 
 // enforce widget interface compliance
@@ -707,7 +707,7 @@ func (ic IssueCollection) update(msg tea.Msg) (widget, tea.Cmd) {
 
 func (ic IssueCollection) sendLoad() tea.Msg { return loadPath(ic.Path) }
 
-func (ic IssueCollection) newIssueInCollection() tea.Msg { return createInCollectionMsg(ic.Path) }
+func (ic IssueCollection) newIssueInCollection() tea.Msg { return setTitleMsg(ic.Path) }
 
 func (ic IssueCollection) edit() tea.Msg { return newEditWidget(ic.Path) }
 
@@ -745,17 +745,21 @@ func (ic IssueCollection) keyhelp() string {
 	return output
 }
 
-// -------- createInCollection widget definitions -----------------------------
+// -------- setTitle widget definitions ---------------------------------------
 // ----------------------------------------------------------------------------
 
-type createInCollection struct {
-	Path  string          // base path for the new issue
-	name  string          // the name input by the user
+type setTitle struct {
+	Path  string // base path for the new issue
+	name  string
 	input textinput.Model // the input widget
 }
 
-func (w createInCollection) update(msg tea.Msg) (widget, tea.Cmd) {
+func (w setTitle) update(msg tea.Msg) (widget, tea.Cmd) {
 	var cmds []tea.Cmd
+	i, cmd := w.input.Update(msg)
+	w.input = i
+	w.name = i.Value()
+	cmds = append(cmds, cmd)
 
 	switch msg := msg.(type) {
 	case tea.KeyMsg:
@@ -765,24 +769,22 @@ func (w createInCollection) update(msg tea.Msg) (widget, tea.Cmd) {
 		}
 		cmds = append(cmds, w.render)
 	}
-
-	var inputCmd tea.Cmd
-	w.input, inputCmd = w.input.Update(msg)
-	cmds = append(cmds, inputCmd)
-
-	w.name = w.input.Value()
-
 	return w, tea.Batch(cmds...)
 }
 
-func (w createInCollection) create() tea.Msg {
+func (w setTitle) create() tea.Msg {
 	w.Path = filepath.Join(w.Path, parseHumanToPath(w.name))
 	return newEditWidget(w.Path)
 }
 
-func (w createInCollection) render() tea.Msg {
-	return borderStyle.Render(fmt.Sprintf("Creating new issue in %s\ntitle: %s",
-		w.Path, w.input.View()))
+func (w setTitle) render() tea.Msg {
+	var header string
+	if len(w.Path) == 0 {
+		header = "Setting title for issue..."
+	} else {
+		header = fmt.Sprintf("Setting title for issue in %s", w.Path)
+	}
+	return borderStyle.Render(fmt.Sprintf("%s\ntitle: %s", header, w.input.View()))
 }
 
-func (w createInCollection) keyhelp() string { return "enter: submit\t\tctrl+c: quit" }
+func (w setTitle) keyhelp() string { return "enter: submit" }