🔨 update CLIYesOrNoReader to include default value

This commit is contained in:
Aykhan Shahsuvarov 2024-09-03 02:05:50 +04:00
parent 86e4f74f10
commit b7ac3bc549
2 changed files with 21 additions and 3 deletions

View File

@ -75,11 +75,27 @@ func CLIConfigReader() (*config.CLIConfig, error) {
return cliConfig, nil
}
func CLIYesOrNoReader(message string) bool {
// CLIYesOrNoReader reads a yes or no answer from the command line.
// It prompts the user with the given message and default value,
// and returns true if the user answers "y" or "Y", and false otherwise.
// If there is an error while reading the input, it returns false.
// If the user simply presses enter without providing any input,
// it returns the default value specified by the `dft` parameter.
func CLIYesOrNoReader(message string, dft bool) bool {
var answer string
fmt.Printf("%s [y/N]: ", message)
defaultMessage := "Y/n"
if !dft {
defaultMessage = "y/N"
}
fmt.Printf("%s [%s]: ", message, defaultMessage)
if _, err := fmt.Scanln(&answer); err != nil {
if err.Error() == "unexpected newline" {
return dft
}
return false
}
if answer == "" {
return dft
}
return answer == "y" || answer == "Y"
}

View File

@ -272,7 +272,9 @@ func getClientDoFunc(
}
activeProxyClientsCount := len(activeProxyClients)
var yesOrNoMessage string
var yesOrNoDefault bool
if activeProxyClientsCount == 0 {
yesOrNoDefault = false
yesOrNoMessage = utils.Colored(
utils.Colors.Red,
"No active proxies found. Do you want to continue?",
@ -287,7 +289,7 @@ func getClientDoFunc(
)
}
fmt.Println()
proceed := readers.CLIYesOrNoReader(yesOrNoMessage)
proceed := readers.CLIYesOrNoReader(yesOrNoMessage, yesOrNoDefault)
if !proceed {
utils.PrintAndExit("Exiting...")
}