package main import ( "bufio" "fmt" "os" "strings" ) // isTerminal reports whether f is a character device (i.e. a TTY). func isTerminal(f *os.File) bool { info, err := f.Stat() if err != nil { return false } return info.Mode()&os.ModeCharDevice != 0 } // promptDir asks the user for the parent directory to scan, defaulting to // def when the input is empty. func promptDir(def string) (string, error) { fmt.Fprintf(os.Stderr, "Parent directory to scan [%s]: ", def) reader := bufio.NewReader(os.Stdin) line, err := reader.ReadString('\n') if err != nil { return "", err } line = strings.TrimSpace(line) if line == "" { return def, nil } return line, nil } // promptYesNo asks a y/N question on stderr and returns the answer. func promptYesNo(def bool, format string, args ...any) (bool, error) { suffix := "[y/N]" if def { suffix = "[Y/n]" } fmt.Fprintf(os.Stderr, format+" "+suffix+": ", args...) line, err := bufio.NewReader(os.Stdin).ReadString('\n') if err != nil { return false, err } switch strings.ToLower(strings.TrimSpace(line)) { case "y", "yes": return true, nil case "n", "no": return false, nil case "": return def, nil default: return false, nil } }