-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgui.go
167 lines (143 loc) · 3.45 KB
/
gui.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"strings"
)
// LaunchGUI starts the user interface
func LaunchGUI(db *DB) {
fmt.Printf("getting basepath...")
basepath, err := db.GetOption("basepath")
checkErr(err)
fmt.Printf("OK\n")
fmt.Printf("getting file count...")
filesInDB, err := db.GetCount("SELECT id FROM files LIMIT 1")
if err != nil {
filesInDB = 0
}
fmt.Printf("OK\n")
fmt.Printf("getting total filesize...")
ts, err := db.GetCount("SELECT sum(filesize) FROM files")
if err != nil {
ts = 0
}
totalSize := ByteSize(ts)
fmt.Printf("OK\n")
fmt.Printf("getting deleted files count...")
deletedFiles, err := db.GetCount("SELECT count(id) FROM files WHERE file_found = '0'")
if err != nil {
deletedFiles = 0
}
fmt.Printf("OK\n")
fmt.Printf("getting changed files count...")
changedFiles, err := db.GetCount("SELECT count(id) FROM files WHERE checksum_ok = '0'")
if err != nil {
changedFiles = 0
}
fmt.Printf("OK\n")
clearScreen()
fmt.Printf("Checksummer %v - filesystem intelligence", VERSION)
fmt.Println("")
fmt.Println("basepath is:", basepath)
fmt.Println("total size: ", totalSize)
fmt.Println("")
fmt.Println("=== Collection ===")
fmt.Println("[cf] collect files")
if filesInDB > 0 {
fmt.Println("[cd] check files in database")
fmt.Println("[mc] make checksums")
fmt.Println("[rc] reindex & check all files")
fmt.Println("[crc] continue reindex & checking")
}
fmt.Println("")
fmt.Println("=== Analysis ===")
if filesInDB > 0 {
fmt.Println("[s] search files")
fmt.Println("[r] rank by filesize")
fmt.Println("[m] recently modified files")
fmt.Println("[ld] list duplicate files")
}
if deletedFiles > 0 {
fmt.Printf("[d] show %v deleted files\n", deletedFiles)
fmt.Println("[pd] prune deleted files")
}
if changedFiles > 0 {
fmt.Printf("[ch] show %v changed files\n", changedFiles)
fmt.Println("[pc] prune changed files")
}
fmt.Println("")
fmt.Println("[cb] change basepath")
fmt.Println("[q] exit")
fmt.Println("")
reader := bufio.NewReader(os.Stdin)
fmt.Print("Select: ")
choice, _ := reader.ReadString('\n')
choice = strings.Trim(choice, "\n")
switch choice {
case "cf":
db.CollectFiles()
case "cd":
db.CheckFilesDB()
case "cb":
db.ChangeBasepath()
case "mc":
db.MakeChecksums()
case "rc":
db.ReindexCheck(false)
case "crc":
db.ReindexCheck(true)
case "r":
db.RankFilesize()
case "s":
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter searchterm: ")
term, _ := reader.ReadString('\n')
term = strings.Trim(term, "\n")
db.Search(term)
case "m":
db.RankModified()
case "ld":
db.ListDuplicates()
case "d":
db.ShowDeleted()
case "pd":
db.PruneDeleted()
case "ch":
db.ShowChanged()
case "pc":
db.PruneChanged()
case "q":
return
}
LaunchGUI(db)
}
func clearScreen() {
fmt.Print("\033[H\033[2J")
}
func pager(str string) {
// nasty bug forces me to scroll at the end (+G)
// otherwise, less may hang
// this hack will be replaced by the termui interface without less
cmd := exec.Command("less", "+G")
// create a pipe (blocking)
r, stdin := io.Pipe()
// Set your i/o's
cmd.Stdin = r
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Create a blocking chan, Run the pager and unblock once it is finished
c := make(chan struct{})
go func() {
defer close(c)
cmd.Run()
}()
// Pass anything to your pipe
fmt.Fprintf(stdin, str)
// Close stdin (result in pager to exit)
stdin.Close()
// Wait for the pager to be finished
<-c
}