77 "strings"
88
99 "github.com/aiomayo/hdf/internal/config"
10+ "github.com/aiomayo/hdf/internal/ui"
11+ "github.com/charmbracelet/huh"
1012 "github.com/spf13/cobra"
1113)
1214
@@ -99,7 +101,7 @@ func formatShow(cfg *config.Config) string {
99101 if f .Kind == config .StringMap {
100102 fmt .Fprintf (& b , "%s\n " , formatted )
101103 } else {
102- fmt .Fprintf (& b , "%-17s = %-10s # %s\n " , f .Key , formatted , f .Desc )
104+ fmt .Fprintf (& b , "%-20s = %-10s # %s\n " , f .DisplayName () , formatted , f .Desc )
103105 }
104106 }
105107 }
@@ -110,19 +112,35 @@ func formatShow(cfg *config.Config) string {
110112func newConfigEditCmd () * cobra.Command {
111113 return & cobra.Command {
112114 Use : "edit" ,
113- Short : "Open config in editor" ,
115+ Short : "Open config editor" ,
114116 Args : cobra .NoArgs ,
115117 RunE : func (cmd * cobra.Command , args []string ) error {
116- editor := os . Getenv ( "VISUAL" )
117- if editor == "" {
118- editor = os . Getenv ( "EDITOR" )
118+ cfg , err := config . Load ( )
119+ if err != nil {
120+ return err
119121 }
122+
123+ editor := cfg .DefaultEditor
120124 if editor == "" {
121- editor = "vi"
125+ editor , err = pickEditor ()
126+ if err != nil {
127+ return err
128+ }
129+ cfg .DefaultEditor = editor
130+ if err := config .Save (cfg ); err != nil {
131+ return err
132+ }
122133 }
123134
124- if _ , err := config .Load (); err != nil {
125- return err
135+ if editor == "tui" {
136+ modified , err := ui .EditConfig (cfg )
137+ if err != nil {
138+ return err
139+ }
140+ if modified {
141+ return config .Save (cfg )
142+ }
143+ return nil
126144 }
127145
128146 c := exec .Command (editor , config .Path ())
@@ -134,6 +152,38 @@ func newConfigEditCmd() *cobra.Command {
134152 }
135153}
136154
155+ func pickEditor () (string , error ) {
156+ f := config .LookupField ("default_editor" )
157+
158+ var options []huh.Option [string ]
159+ for _ , opt := range f .Options {
160+ if opt == "" {
161+ continue
162+ }
163+ if opt == "tui" {
164+ options = append (options , huh .NewOption ("Built-in TUI" , opt ))
165+ continue
166+ }
167+ if _ , err := exec .LookPath (opt ); err == nil {
168+ options = append (options , huh .NewOption (opt , opt ))
169+ }
170+ }
171+
172+ var choice string
173+ form := huh .NewForm (
174+ huh .NewGroup (
175+ huh .NewSelect [string ]().
176+ Title ("Choose default config editor" ).
177+ Options (options ... ).
178+ Value (& choice ),
179+ ),
180+ )
181+ if err := form .Run (); err != nil {
182+ return "" , err
183+ }
184+ return choice , nil
185+ }
186+
137187func newConfigResetCmd () * cobra.Command {
138188 return & cobra.Command {
139189 Use : "reset" ,
0 commit comments