move some functions out of main into packages

- confirm functions into files package
- newpath prompter into promt package
This commit is contained in:
Lilian Jónsdóttir 2024-07-27 16:42:23 -07:00
parent 095f59babb
commit 7fade0ce4c
3 changed files with 130 additions and 138 deletions

View file

@ -6,6 +6,7 @@ import (
"log"
"os"
"github.com/charmbracelet/huh"
"golang.org/x/term"
)
@ -38,3 +39,25 @@ func AskRune(prompt, options string) byte {
return bytes.ToLower(b)[0]
}
func NewPath(path string) (string, bool) {
for {
answer := AskRune(fmt.Sprintf("file %s exists, overwrite, rename, or cancel?", path), "o/r/c")
switch answer {
case 'o', 'O':
return path, false
case 'r', 'R':
if err := huh.NewInput().
Title("input a new filename").
Value(&path).
Run(); err != nil {
return path, false
}
if _, e := os.Stat(path); e != nil {
return path, false
}
default:
return path, true
}
}
}