Skip to content

Commit

Permalink
feat: Include Confirmation dialogue if a file is about to be overwritten
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
pbj committed Apr 26, 2024
1 parent 80803eb commit a26d077
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/beevik/etree"
in "github.com/charmbracelet/freeze/input"
"github.com/charmbracelet/freeze/svg"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/charmbracelet/x/exp/term/ansi"
Expand Down Expand Up @@ -133,6 +134,34 @@ func main() {
config.Output = defaultOutputFilename
}

// Check if file already exists
if _, err := os.Stat(config.Output); err == nil {
var confirm bool
var newOutputFilename string

confirmOverwriteForm :=
huh.NewConfirm().
Title(fmt.Sprintf("'%s' already exists. Would you like to overwrite this file?", config.Output)).
Value(&confirm)

newOutputFileNameForm := huh.NewInput().
Title("Enter new output filename to use").
Value(&newOutputFilename)

err = confirmOverwriteForm.Run()
if err != nil {
printErrorFatal("could not retrive overwrite confirmation", err)
}

if !confirm {
err := newOutputFileNameForm.Run()
if err != nil {
printErrorFatal("could not retrieve new output filename to use", err)
}
config.Output = newOutputFilename
}
}

scale = 1
if autoHeight && autoWidth && strings.HasSuffix(config.Output, ".png") {
scale = 4
Expand Down

0 comments on commit a26d077

Please sign in to comment.