Skip to content

Commit

Permalink
feat: interactive file picker if input is not present
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Jan 29, 2024
1 parent c0f0e48 commit 3a96154
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/catppuccin/go v0.2.0 // indirect
github.com/charmbracelet/bubbles v0.17.2-0.20240125161725-fc18779a6ae9 // indirect
github.com/charmbracelet/bubbles v0.17.2-0.20240129221336-07e7bd4ee418 // indirect
github.com/charmbracelet/bubbletea v0.25.0 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
Expand All @@ -28,7 +29,7 @@ require (
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rivo/uniseg v0.4.6 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ github.com/beevik/etree v1.3.0 h1:hQTc+pylzIKDb23yYprodCWWTt+ojFfUZyzU09a/hmU=
github.com/beevik/etree v1.3.0/go.mod h1:aiPf89g/1k3AShMVAzriilpcE4R/Vuor90y83zVZWFc=
github.com/catppuccin/go v0.2.0 h1:ktBeIrIP42b/8FGiScP9sgrWOss3lw0Z5SktRoithGA=
github.com/catppuccin/go v0.2.0/go.mod h1:8IHJuMGaUUjQM82qBrGNBv7LFq6JI3NnQCF6MOlZjpc=
github.com/charmbracelet/bubbles v0.17.2-0.20240125161725-fc18779a6ae9 h1:DgM7KYTYQqfv7FmUFsSMgjpzsQgZPToBKANhrbgEGmg=
github.com/charmbracelet/bubbles v0.17.2-0.20240125161725-fc18779a6ae9/go.mod h1:bFPs/pcBrPDNRHK9DOOctOwvdSjRtdA5iFmhdQ/QT7w=
github.com/charmbracelet/bubbles v0.17.2-0.20240129221336-07e7bd4ee418 h1:uvKilAnaaZbbqtI3KR8KXdEy2eqqm7F0lr1bmlPpUyE=
github.com/charmbracelet/bubbles v0.17.2-0.20240129221336-07e7bd4ee418/go.mod h1:bFPs/pcBrPDNRHK9DOOctOwvdSjRtdA5iFmhdQ/QT7w=
github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt5dywy4TcM=
github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg=
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
Expand All @@ -30,6 +30,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0=
github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
Expand All @@ -55,8 +57,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.6 h1:Sovz9sDSwbOz9tgUy8JpT+KgCkPYJEN/oYzlJiYTNLg=
github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand Down
38 changes: 23 additions & 15 deletions interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"path/filepath"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -30,16 +31,32 @@ func runForm(config *Config) (*Config, error) {

theme := huh.ThemeCharm()
theme.FieldSeparator = lipgloss.NewStyle()
theme.Blurred.Title.Width(14).Foreground(lipgloss.Color("7"))
theme.Focused.Title.Width(14).Foreground(green).Bold(true)
theme.Blurred.Description.Foreground(lipgloss.Color("0"))
theme.Focused.Description.Foreground(lipgloss.Color("7"))
theme.Blurred.Title = theme.Blurred.Title.Copy().Width(14).Foreground(lipgloss.Color("7"))
theme.Focused.Title = theme.Focused.Title.Copy().Width(14).Foreground(green).Bold(true)
theme.Focused.Title = theme.Focused.Title.Copy().Width(14).Foreground(green).Bold(true)
theme.Blurred.Description = theme.Blurred.Description.Copy().Foreground(lipgloss.Color("0"))
theme.Focused.Description = theme.Focused.Description.Copy().Foreground(lipgloss.Color("7"))
theme.Blurred.BlurredButton = lipgloss.NewStyle().Foreground(lipgloss.Color("8")).PaddingRight(1)
theme.Blurred.FocusedButton = lipgloss.NewStyle().Foreground(lipgloss.Color("7")).PaddingRight(1)
theme.Focused.BlurredButton = lipgloss.NewStyle().Foreground(lipgloss.Color("8")).PaddingRight(1)
theme.Focused.FocusedButton = lipgloss.NewStyle().Foreground(lipgloss.Color("15")).PaddingRight(1)
theme.Focused.NoteTitle.Margin(1, 0)
theme.Blurred.NoteTitle.Margin(1, 0)
theme.Focused.NoteTitle = theme.Focused.NoteTitle.Copy().Margin(1, 0)
theme.Blurred.NoteTitle = theme.Blurred.NoteTitle.Copy().Margin(1, 0)

if config.Input == "" {
_ = huh.NewForm(
huh.NewGroup(
huh.NewNote().Title("Capture file"),
huh.NewFilePicker().
Height(10).
Value(&config.Input),
),
).WithTheme(theme).Run()

base, ext := filepath.Base(config.Input), filepath.Ext(config.Input)
config.Output = strings.TrimSuffix(base, ext) + ".svg"
}

theme.Focused.Base.
Border(lipgloss.Border{Left: "> "}, false).
BorderLeft(true).
Expand Down Expand Up @@ -182,11 +199,6 @@ func runForm(config *Config) (*Config, error) {
),
).WithTheme(theme).WithHeight(10)

// Let's first select a input file if not specified
if config.Input == "" {
config.Input = pickFile()
}

err := f.Run()

config.Padding = parsePadding(padding)
Expand Down Expand Up @@ -266,7 +278,3 @@ func parsePadding(v string) []int {
}

var parseMargin = parsePadding

func pickFile() string {
return "main.go"
}

0 comments on commit 3a96154

Please sign in to comment.