diskspace/commands.go

122 lines
2.4 KiB
Go
Raw Normal View History

2022-06-28 13:11:16 +02:00
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func help(args []string) {
fmt.Println()
fmt.Println(Translate("Commands:"))
fmt.Println(Translate("ls: list files"))
fmt.Println(Translate("cd: change directory"))
fmt.Println(Translate("rm: mark files for removal"))
fmt.Println(Translate("urm: unmark files for removal"))
fmt.Println(Translate("exit: delete marked files and exit"))
fmt.Println()
}
func ls(args []string) {
path := strings.Join(args, " ")
if path == "" {
path = "."
}
entries := getEntriesForPath(path)
if len(entries) == 0 {
showError("'%s' could not be found", path)
return
}
for _, entry := range entries {
fmt.Println()
fmt.Println(entry.StringRecursive(*RecursiveDirDepth))
2022-06-28 13:11:16 +02:00
}
}
func rm(args []string, removalMark bool) {
path := strings.Join(args, " ")
entries := getEntriesForPath(path)
if len(entries) == 0 {
showError("'%s' could not be found", path)
return
}
for _, entry := range entries {
entry.SetRemovalMark(removalMark)
}
}
func cd(args []string) {
path := "/"
if len(args) > 0 {
path = args[0]
}
entries := getEntriesForPath(path)
if len(entries) == 0 {
showError("'%s' could not be found", path)
return
} else if len(entries) > 1 {
b := new(strings.Builder)
for _, entry := range entries {
path := strings.TrimPrefix(entry.Path(), Current.Path()+"/")
path = strings.TrimPrefix(path, Root.Path())
b.WriteString(path)
b.WriteRune('\n')
}
showError("ambiguous path: %s", path)
fmt.Println(Translate("possible paths:\n%s", b.String()))
return
}
entry := entries[0]
if entry.IsDir() {
Current = entry
fmt.Println()
} else {
showError("'%s' is not a directory", entry.Name())
}
}
func exit() {
fmt.Println()
stats := new(RemovalStats)
Root.RemovalStats(stats)
if len(stats.Paths) == 0 {
s := bufio.NewScanner(os.Stdin)
s.Split(bufio.ScanRunes)
fmt.Print(Translate("exit? [%s/%s]: ", "y", "N"))
2022-06-28 13:11:16 +02:00
if s.Scan() {
text := strings.ToLower(s.Text())
if text == "y" || text == "yes" {
os.Exit(0)
}
}
return
}
fmt.Println(Translate("The following items will be removed:"))
fmt.Println(stats)
s := bufio.NewScanner(os.Stdin)
s.Split(bufio.ScanRunes)
fmt.Print(Translate("Delete files? [%s/%s/%s]: ", ColorRed.Sprint("y"), "n", "C"))
2022-06-28 13:11:16 +02:00
if s.Scan() {
text := strings.ToLower(s.Text())
if text == "y" || text == "yes" {
removeMarkedEntries(Root)
os.Exit(0)
} else if text == "n" || text == "no" {
os.Exit(0)
}
}
}