yay/callbacks.go

102 lines
1.8 KiB
Go
Raw Normal View History

package main
import (
"bufio"
"fmt"
"os"
"strconv"
2019-04-23 17:53:20 +02:00
alpm "github.com/Jguer/go-alpm"
)
func questionCallback(question alpm.QuestionAny) {
2018-07-31 10:42:17 +02:00
if qi, err := question.QuestionInstallIgnorepkg(); err == nil {
qi.SetInstall(true)
}
qp, err := question.QuestionSelectProvider()
2018-07-31 10:42:17 +02:00
if err != nil {
return
}
if hideMenus {
return
}
2018-07-31 10:42:17 +02:00
size := 0
qp.Providers(alpmHandle).ForEach(func(pkg alpm.Package) error {
size++
return nil
})
fmt.Print(bold(cyan(":: ")))
str := bold(fmt.Sprintf(bold("There are %d providers available for %s:"), size, qp.Dep()))
size = 1
var db string
qp.Providers(alpmHandle).ForEach(func(pkg alpm.Package) error {
2019-02-04 17:56:02 +01:00
thisDB := pkg.DB().Name()
2018-07-31 10:42:17 +02:00
2019-02-04 17:56:02 +01:00
if db != thisDB {
db = thisDB
2018-07-31 10:42:17 +02:00
str += bold(cyan("\n:: ")) + bold("Repository "+db+"\n ")
}
str += fmt.Sprintf("%d) %s ", size, pkg.Name())
size++
return nil
})
fmt.Println(str)
for {
fmt.Print("\nEnter a number (default=1): ")
if config.NoConfirm {
fmt.Println()
break
}
2018-07-31 10:42:17 +02:00
reader := bufio.NewReader(os.Stdin)
numberBuf, overflow, err := reader.ReadLine()
if err != nil {
fmt.Fprintln(os.Stderr, err)
2018-07-31 10:42:17 +02:00
break
}
if overflow {
fmt.Fprintln(os.Stderr, "Input too long")
2018-07-31 10:42:17 +02:00
continue
}
if string(numberBuf) == "" {
break
}
num, err := strconv.Atoi(string(numberBuf))
if err != nil {
fmt.Fprintf(os.Stderr, "%s invalid number: %s\n", red("error:"), string(numberBuf))
2018-07-31 10:42:17 +02:00
continue
}
if num < 1 || num > size {
fmt.Fprintf(os.Stderr, "%s invalid value: %d is not between %d and %d\n", red("error:"), num, 1, size)
2018-07-31 10:42:17 +02:00
continue
}
qp.SetUseIndex(num - 1)
break
}
}
2018-07-26 14:35:19 +02:00
func logCallback(level alpm.LogLevel, str string) {
switch level {
case alpm.LogWarning:
fmt.Print(bold(yellow(smallArrow)), " ", str)
case alpm.LogError:
fmt.Print(bold(red(smallArrow)), " ", str)
}
}