tui better

This commit is contained in:
2026-08-27 17:57:23 +01:00
parent 71961f68da
commit 73c2009139
5 changed files with 230 additions and 123 deletions
+154 -102
View File
@@ -7,6 +7,7 @@ import (
"charm.land/bubbles/v2/textarea"
"charm.land/bubbles/v2/textinput"
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
"dirmd/internal/config"
)
@@ -57,29 +58,30 @@ func NewForm() FormModel {
inputDir := textinput.New()
inputDir.Placeholder = "."
inputDir.SetValue(".")
inputDir.SetWidth(40)
inputDir.SetWidth(60)
outputFile := textinput.New()
outputFile.Placeholder = "./output.md"
outputFile.SetValue("./output.md")
outputFile.SetWidth(40)
outputFile.Placeholder = "./output_dirmd/index.md"
outputFile.SetValue("./output_dirmd/index.md")
outputFile.SetWidth(60)
drivePath := textinput.New()
drivePath.Placeholder = "/my-files/md/"
drivePath.SetValue("")
drivePath.SetWidth(40)
drivePath.SetWidth(60)
maxDepth := textinput.New()
maxDepth.SetValue("20")
maxDepth.SetWidth(5)
maxDepth.SetWidth(10)
maxSize := textinput.New()
maxSize.SetValue("512")
maxSize.SetWidth(5)
maxSize.SetWidth(10)
ignoreArea := textarea.New()
ignoreArea.Placeholder = "node_modules\n.venv"
ignoreArea.SetHeight(3)
ignoreArea.Placeholder = "One pattern per line..."
ignoreArea.SetHeight(5)
ignoreArea.ShowLineNumbers = false
ignoreArea.CharLimit = 0
m := FormModel{
inputDir: inputDir,
@@ -114,17 +116,29 @@ func (m *FormModel) SetDark(isDark bool) {
}
func (m *FormModel) applyStyles() {
tiStyles := textinput.DefaultStyles(m.isDark)
tiStyles := getDefaultInputStyles(m.isDark)
m.inputDir.SetStyles(tiStyles)
m.outputFile.SetStyles(tiStyles)
m.drivePath.SetStyles(tiStyles)
m.maxDepth.SetStyles(tiStyles)
m.maxSize.SetStyles(tiStyles)
taStyles := textarea.DefaultStyles(m.isDark)
taStyles := getDefaultTextareaStyles(m.isDark)
m.ignoreArea.SetStyles(taStyles)
}
func (m *FormModel) shouldSkipField(idx int) bool {
return idx == fieldDrivePath && !m.protonDrive.Checked
}
func isTextInputField(idx int) bool {
switch idx {
case fieldInputDir, fieldOutputFile, fieldDrivePath, fieldMaxDepth, fieldMaxSize, fieldIgnorePatterns:
return true
}
return false
}
func (m *FormModel) focusActiveField() tea.Cmd {
m.inputDir.Blur()
m.outputFile.Blur()
@@ -157,57 +171,96 @@ func (m FormModel) Update(msg tea.Msg) (FormModel, tea.Cmd) {
var cmds []tea.Cmd
if k, ok := msg.(tea.KeyPressMsg); ok {
switch {
case isNavigateDown(k):
m.focusIndex++
if m.focusIndex >= fieldCount {
m.focusIndex = 0
if isTextInputField(m.focusIndex) {
if isNavigateDown(k) || isNavigateUp(k) {
if isNavigateDown(k) {
m.focusIndex++
if m.focusIndex >= fieldCount {
m.focusIndex = 0
}
for m.shouldSkipField(m.focusIndex) {
m.focusIndex++
if m.focusIndex >= fieldCount {
m.focusIndex = 0
}
}
} else {
m.focusIndex--
if m.focusIndex < 0 {
m.focusIndex = fieldCount - 1
}
for m.shouldSkipField(m.focusIndex) {
m.focusIndex--
if m.focusIndex < 0 {
m.focusIndex = fieldCount - 1
}
}
}
cmd := m.focusActiveField()
if cmd != nil {
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}
if m.focusIndex == fieldDrivePath && !m.protonDrive.Checked {
m.focusIndex = fieldProtonDrive
}
cmd := m.focusActiveField()
if cmd != nil {
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
case isNavigateUp(k):
m.focusIndex--
if m.focusIndex < 0 {
m.focusIndex = fieldCount - 1
}
if m.focusIndex == fieldDrivePath && !m.protonDrive.Checked {
m.focusIndex = fieldProtonDrive
}
cmd := m.focusActiveField()
if cmd != nil {
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
case isActivate(k):
switch m.focusIndex {
case fieldAppend:
m.appendMode.Toggle()
case fieldForce:
m.force.Toggle()
case fieldVerticalSlices:
m.verticalSlices.Toggle()
case fieldSkipFrontend:
m.skipFrontend.Toggle()
case fieldFrontendOnly:
m.frontendOnly.Toggle()
case fieldOmitTest:
m.omitTest.Toggle()
case fieldOmitMd:
m.omitMd.Toggle()
case fieldProtonDrive:
m.protonDrive.Toggle()
case fieldGenerateBtn:
} else {
switch {
case isNavigateDown(k):
m.focusIndex++
if m.focusIndex >= fieldCount {
m.focusIndex = 0
}
for m.shouldSkipField(m.focusIndex) {
m.focusIndex++
if m.focusIndex >= fieldCount {
m.focusIndex = 0
}
}
cmd := m.focusActiveField()
if cmd != nil {
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
case isNavigateUp(k):
m.focusIndex--
if m.focusIndex < 0 {
m.focusIndex = fieldCount - 1
}
for m.shouldSkipField(m.focusIndex) {
m.focusIndex--
if m.focusIndex < 0 {
m.focusIndex = fieldCount - 1
}
}
cmd := m.focusActiveField()
if cmd != nil {
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
case isActivate(k):
switch m.focusIndex {
case fieldAppend:
m.appendMode.Toggle()
case fieldForce:
m.force.Toggle()
case fieldVerticalSlices:
m.verticalSlices.Toggle()
case fieldSkipFrontend:
m.skipFrontend.Toggle()
case fieldFrontendOnly:
m.frontendOnly.Toggle()
case fieldOmitTest:
m.omitTest.Toggle()
case fieldOmitMd:
m.omitMd.Toggle()
case fieldProtonDrive:
m.protonDrive.Toggle()
case fieldGenerateBtn:
return m, nil
case fieldCancelBtn:
return m, tea.Quit
}
return m, nil
case fieldCancelBtn:
return m, tea.Quit
}
return m, nil
}
}
@@ -242,6 +295,18 @@ func (m FormModel) Update(msg tea.Msg) (FormModel, tea.Cmd) {
}
func (m FormModel) View() string {
usableWidth := m.width - 30
if usableWidth < 50 {
usableWidth = 50
}
if usableWidth > 80 {
usableWidth = 80
}
m.inputDir.SetWidth(usableWidth)
m.outputFile.SetWidth(usableWidth)
m.drivePath.SetWidth(usableWidth)
m.ignoreArea.SetWidth(usableWidth)
var b strings.Builder
b.WriteString(titleStyle.Render("dirmd"))
@@ -270,64 +335,61 @@ func (m FormModel) View() string {
b.WriteString("\n")
if m.protonDrive.Checked {
b.WriteString(" ")
b.WriteString(m.renderTextField("Remote path", m.drivePath, m.focusIndex == fieldDrivePath))
b.WriteString("\n")
}
b.WriteString("\n")
depthLabel := labelStyle.Render("Max depth:")
if m.focusIndex == fieldMaxDepth {
depthLabel = focusedStyle.Bold(true).Render("Max depth:")
}
sizeLabel := labelStyle.Render("Max size (KB):")
if m.focusIndex == fieldMaxSize {
sizeLabel = focusedStyle.Bold(true).Render("Max size (KB):")
}
b.WriteString(fmt.Sprintf("%s %s %s %s",
depthLabel, m.maxDepth.View(),
sizeLabel, m.maxSize.View(),
))
b.WriteString(m.renderTextField("Max depth", m.maxDepth, m.focusIndex == fieldMaxDepth))
b.WriteString("\n")
b.WriteString(m.renderTextField("Max size (KB)", m.maxSize, m.focusIndex == fieldMaxSize))
b.WriteString("\n\n")
ignoreLabel := labelStyle.Render("Ignore patterns:")
if m.focusIndex == fieldIgnorePatterns {
ignoreLabel = focusedStyle.Bold(true).Render("Ignore patterns:")
ignoreFocused := m.focusIndex == fieldIgnorePatterns
if ignoreFocused {
b.WriteString(focusedStyle.Render("▶ Ignore patterns:"))
} else {
b.WriteString(" " + labelStyle.Render("Ignore patterns:"))
}
b.WriteString(ignoreLabel)
b.WriteString("\n")
b.WriteString(m.ignoreArea.View())
b.WriteString("\n\n")
btnStyle := labelStyle
if m.focusIndex == fieldGenerateBtn {
btnStyle = focusedStyle.Bold(true)
btnFocused := m.focusIndex == fieldGenerateBtn
if btnFocused {
b.WriteString(focusedStyle.Render("▶ [ Generate ]"))
} else {
b.WriteString(" " + labelStyle.Render("[ Generate ]"))
}
b.WriteString(btnStyle.Render("[ Generate ]"))
b.WriteString(" ")
cancelStyle := labelStyle
if m.focusIndex == fieldCancelBtn {
cancelStyle = focusedStyle.Bold(true)
b.WriteString(" ")
cancelFocused := m.focusIndex == fieldCancelBtn
if cancelFocused {
b.WriteString(focusedStyle.Render("▶ [ Cancel ]"))
} else {
b.WriteString(" " + labelStyle.Render("[ Cancel ]"))
}
b.WriteString(cancelStyle.Render("[ Cancel ]"))
if m.err != "" {
b.WriteString("\n\n")
b.WriteString(errorStyle.Render(m.err))
}
b.WriteString("\n")
b.WriteString(footerStyle.Render("Tab/j/k: navigate Space: toggle Enter: submit Esc: quit"))
b.WriteString("\n\n")
b.WriteString(footerStyle.Render("Tab/↑↓: navigate Space: toggle Enter: submit Esc: quit"))
return appBorder.Render(b.String())
content := appBorder.Render(b.String())
if m.width > 0 && m.height > 0 {
return lipgloss.Place(m.width, m.height, lipgloss.Center, lipgloss.Center, content)
}
return content
}
func (m FormModel) renderTextField(label string, input textinput.Model, focused bool) string {
style := labelStyle
if focused {
style = focusedStyle
return focusedStyle.Render("▶ "+label+":") + " " + input.View()
}
return fmt.Sprintf("%s %s", style.Render(label+":"), input.View())
return " " + labelStyle.Render(label+":") + " " + input.View()
}
func (m FormModel) ToConfig() (*config.Config, error) {
@@ -406,16 +468,6 @@ func (m *FormModel) SetError(msg string) {
func (m *FormModel) SetSize(w, h int) {
m.width = w
m.height = h
usable := w - 30
if usable < 20 {
usable = 20
}
if usable > 60 {
usable = 60
}
m.inputDir.SetWidth(usable)
m.outputFile.SetWidth(usable)
m.drivePath.SetWidth(usable)
}
func parseIntDefault(s string, def int) (int, error) {