Skip to content

Commit a26d077

Browse files
author
pbj
committed
feat: Include Confirmation dialogue if a file is about to be overwritten
- Added in a `huh.NewConfirm` and `huh.NewInput` fields, that checks if the output file already exists within the given directory - If the user selects to `overwrite` the file, execution continues as is - If the user selects to `not overwrite` the file, an `input` field is provided to select a new output filename
1 parent 80803eb commit a26d077

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/beevik/etree"
1818
in "github.com/charmbracelet/freeze/input"
1919
"github.com/charmbracelet/freeze/svg"
20+
"github.com/charmbracelet/huh"
2021
"github.com/charmbracelet/lipgloss"
2122
"github.com/charmbracelet/log"
2223
"github.com/charmbracelet/x/exp/term/ansi"
@@ -133,6 +134,34 @@ func main() {
133134
config.Output = defaultOutputFilename
134135
}
135136

137+
// Check if file already exists
138+
if _, err := os.Stat(config.Output); err == nil {
139+
var confirm bool
140+
var newOutputFilename string
141+
142+
confirmOverwriteForm :=
143+
huh.NewConfirm().
144+
Title(fmt.Sprintf("'%s' already exists. Would you like to overwrite this file?", config.Output)).
145+
Value(&confirm)
146+
147+
newOutputFileNameForm := huh.NewInput().
148+
Title("Enter new output filename to use").
149+
Value(&newOutputFilename)
150+
151+
err = confirmOverwriteForm.Run()
152+
if err != nil {
153+
printErrorFatal("could not retrive overwrite confirmation", err)
154+
}
155+
156+
if !confirm {
157+
err := newOutputFileNameForm.Run()
158+
if err != nil {
159+
printErrorFatal("could not retrieve new output filename to use", err)
160+
}
161+
config.Output = newOutputFilename
162+
}
163+
}
164+
136165
scale = 1
137166
if autoHeight && autoWidth && strings.HasSuffix(config.Output, ".png") {
138167
scale = 4

0 commit comments

Comments
 (0)