-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add a text input for the short commit description
- Loading branch information
Showing
6 changed files
with
138 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package tui_description_editor | ||
|
||
// like what Glow has, but without the markdown-stashing | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/charmbracelet/bubbles/textinput" | ||
tea "github.com/charmbracelet/bubbletea" | ||
_ "github.com/charmbracelet/glamour" | ||
"github.com/muesli/termenv" | ||
) | ||
|
||
type Model struct { | ||
prefix string | ||
prefixLen int | ||
Choice chan<- string | ||
input textinput.Model | ||
softMax int // TODO: make *int and use nil to eliminate countdown | ||
} | ||
|
||
func (m Model) Focus() { | ||
m.input.Focus() | ||
} | ||
|
||
func NewModel(choice chan string, prefix string, softLengthLimit int) Model { | ||
input := textinput.NewModel() | ||
input.Prompt = termenv.String(prefix).Faint().String() | ||
// input.Focus() // should be done by supervisor | ||
return Model{ | ||
prefixLen: len(prefix), | ||
softMax: softLengthLimit, | ||
Choice: choice, | ||
input: input, | ||
} | ||
} | ||
|
||
func viewCounter(m Model) string { | ||
current := m.prefixLen + len(m.input.Value()) | ||
paddedFormat := fmt.Sprintf("(%%%dd/%d)", len(fmt.Sprintf("%d", m.softMax)), m.softMax) | ||
view := fmt.Sprintf(paddedFormat, current) | ||
if current < m.softMax { | ||
return termenv.String(view).Faint().String() | ||
} else if current == m.softMax { | ||
return view // render in a warning color termenv.String(view). | ||
} else { | ||
return termenv.String(view).Underline().String() // render in an alert color | ||
} | ||
} | ||
|
||
func viewHelpBar(m Model) string { | ||
help := termenv.String("\nsubmit: enter; go back: shift+tab; cancel: ctrl+c"). | ||
Faint().String() + "" | ||
return help + viewCounter(m) | ||
} | ||
|
||
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { | ||
var cmd tea.Cmd | ||
|
||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.Type { | ||
case tea.KeyEnter: | ||
m.Choice <- m.input.Value() | ||
return m, tea.Quit | ||
case tea.KeyCtrlC, tea.KeyCtrlD: | ||
close(m.Choice) | ||
return m, tea.Quit | ||
default: | ||
m.input, cmd = textinput.Update(msg, m.input) | ||
m.input.Focus() | ||
return m, cmd | ||
} | ||
default: | ||
m.input, cmd = textinput.Update(msg, m.input) | ||
m.input.Focus() | ||
return m, cmd | ||
} | ||
} | ||
|
||
func (m Model) View() string { | ||
return textinput.View(m.input) + viewHelpBar(m) | ||
} | ||
|
||
func (m Model) Init() tea.Cmd { | ||
return textinput.Blink(m.input) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters