-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprompt.go
80 lines (69 loc) · 1.52 KB
/
prompt.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func handleDeletions(groups [][]string) {
reader := bufio.NewReader(os.Stdin)
for _, group := range groups {
if len(group) < 2 {
continue
}
fmt.Printf("\nDuplicate group (%d files):\n", len(group))
for i, path := range group {
fmt.Printf("[%d] %s\n", i+1, path)
}
for {
fmt.Print("Enter numbers to delete (space-separated, 'a' to abort): ")
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
if strings.ToLower(input) == "a" {
break
}
toDelete, err := parseDeleteInput(input, len(group))
if err != nil {
fmt.Printf("Error: %v\n", err)
continue
}
if len(toDelete) == len(group) {
fmt.Println("Error: Cannot delete all files in group")
continue
}
deleteFiles(group, toDelete)
break
}
}
}
func parseDeleteInput(input string, max int) ([]int, error) {
if input == "" {
return nil, nil
}
seen := make(map[int]bool)
var indices []int
for _, s := range strings.Split(input, " ") {
num, err := strconv.Atoi(s)
if err != nil || num < 1 || num > max {
return nil, fmt.Errorf("invalid number: %s", s)
}
if seen[num-1] {
continue
}
seen[num-1] = true
indices = append(indices, num-1)
}
return indices, nil
}
func deleteFiles(group []string, indices []int) {
for _, idx := range indices {
path := group[idx]
err := os.Remove(path)
if err != nil {
fmt.Printf("Failed to delete %s: %v\n", path, err)
} else {
fmt.Printf("Deleted: %s\n", path)
}
}
}