-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.go
101 lines (79 loc) · 1.8 KB
/
callbacks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"bufio"
"fmt"
"os"
"strconv"
alpm "github.com/Jguer/go-alpm"
)
func questionCallback(question alpm.QuestionAny) {
if qi, err := question.QuestionInstallIgnorepkg(); err == nil {
qi.SetInstall(true)
}
qp, err := question.QuestionSelectProvider()
if err != nil {
return
}
if hideMenus {
return
}
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 {
thisDB := pkg.DB().Name()
if db != thisDB {
db = thisDB
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
}
reader := bufio.NewReader(os.Stdin)
numberBuf, overflow, err := reader.ReadLine()
if err != nil {
fmt.Fprintln(os.Stderr, err)
break
}
if overflow {
fmt.Fprintln(os.Stderr, "Input too long")
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))
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)
continue
}
qp.SetUseIndex(num - 1)
break
}
}
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)
}
}