Skip to content

Commit

Permalink
Refactor continueTask()
Browse files Browse the repository at this point in the history
The main reason behind this is for future localisation. yes and no can
be set to the localized equivalent and it should just work.

This Refactor also changes the function in ways which make it much less
confusing.

The param is no longer reversed and changed to a boolean. Before you had
to pass in Yy to get a default of no and vice versa.

The function now retuens false for no and true for yes. Before it would
return true if the default option was choosen.
  • Loading branch information
Morganamilo committed Jul 23, 2018
1 parent a3564ec commit e28f4f3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
4 changes: 2 additions & 2 deletions clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ func syncClean(parser *arguments) error {
fmt.Println()
fmt.Printf("Build directory: %s\n", config.BuildDir)

if continueTask(question, "nN") {
if continueTask(question, true) {
err = cleanAUR(keepInstalled, keepCurrent, removeAll)
}

if err != nil || removeAll {
return err
}

if continueTask("Do you want to remove ALL untracked AUR files?", "nN") {
if continueTask("Do you want to remove ALL untracked AUR files?", true) {
err = cleanUntracked()
}

Expand Down
30 changes: 16 additions & 14 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,31 +241,33 @@ func editor() (string, []string) {

// ContinueTask prompts if user wants to continue task.
//If NoConfirm is set the action will continue without user input.
func continueTask(s string, def string) (cont bool) {
func continueTask(s string, cont bool) bool {
if config.NoConfirm {
return true
return cont
}

var response string
var postFix string
yes := "yes"
no := "no"
y := string([]rune(yes)[0])
n := string([]rune(no)[0])

if def == "nN" {
postFix = " [Y/n] "
if cont {
postFix = fmt.Sprintf(" [%s/%s] ", strings.ToUpper(y), n)
} else {
postFix = " [y/N] "
postFix = fmt.Sprintf(" [%s/%s] ", y, strings.ToUpper(n))
}

var response string
fmt.Print(bold(green(arrow)+" "+s), bold(postFix))

n, err := fmt.Scanln(&response)
if err != nil || n == 0 {
return true
}

if response == string(def[0]) || response == string(def[1]) {
return false
len, err := fmt.Scanln(&response)
if err != nil || len == 0 {
return cont
}

return true
response = strings.ToLower(response)
return response == yes || response == y
}

func getInput(defaultValue string) (string, error) {
Expand Down
8 changes: 4 additions & 4 deletions install.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func install(parser *arguments) error {
removeMake = true
} else if config.RemoveMake == "no" {
removeMake = false
} else if !continueTask("Remove make dependencies after install?", "yY") {
} else if continueTask("Remove make dependencies after install?", false) {
removeMake = true
}
}
Expand Down Expand Up @@ -213,7 +213,7 @@ func install(parser *arguments) error {
oldValue := config.NoConfirm
config.NoConfirm = false
fmt.Println()
if !continueTask(bold(green("Proceed with install?")), "nN") {
if !continueTask(bold(green("Proceed with install?")), true) {
return fmt.Errorf("Aborting due to user")
}
config.NoConfirm = oldValue
Expand Down Expand Up @@ -249,7 +249,7 @@ func install(parser *arguments) error {
oldValue := config.NoConfirm
config.NoConfirm = false
fmt.Println()
if !continueTask(bold(green("Proceed with install?")), "nN") {
if !continueTask(bold(green("Proceed with install?")), true) {
return fmt.Errorf("Aborting due to user")
}
config.NoConfirm = oldValue
Expand Down Expand Up @@ -441,7 +441,7 @@ nextpkg:

fmt.Println()

if !continueTask("Try to build them anyway?", "nN") {
if !continueTask("Try to build them anyway?", true) {
return nil, fmt.Errorf("Aborting due to user")
}
}
Expand Down
2 changes: 1 addition & 1 deletion keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func checkPgpKeys(pkgs []*rpc.Pkg, bases map[string][]*rpc.Pkg, srcinfos map[str
fmt.Println()
fmt.Println(str)

if continueTask(bold(green("Import?")), "nN") {
if continueTask(bold(green("Import?")), true) {
return importKeys(problematic.toSlice())
}

Expand Down

0 comments on commit e28f4f3

Please sign in to comment.