Compare commits
10 commits
03c09e1da6
...
1339d642dd
Author | SHA1 | Date | |
---|---|---|---|
Lilian Jónsdóttir | 1339d642dd | ||
Lilian Jónsdóttir | 07424ecd36 | ||
Lilian Jónsdóttir | 6c3abd8d98 | ||
Lilian Jónsdóttir | 34eef6a228 | ||
Lilian Jónsdóttir | 50f4dbda8a | ||
Lilian Jónsdóttir | d8ec25b36d | ||
Lilian Jónsdóttir | d6f066095b | ||
Lilian Jónsdóttir | 4a10d7794f | ||
Lilian Jónsdóttir | cc973694e5 | ||
Lilian Jónsdóttir | 44da4acbbd |
32
README.md
32
README.md
|
@ -23,7 +23,7 @@ Find files on the filesystem based on the filter flags and any filename args.
|
||||||
*--recursive*, *-r*
|
*--recursive*, *-r*
|
||||||
operate on files recursively
|
operate on files recursively
|
||||||
|
|
||||||
*--work-dir* dir, *-w* dir
|
*--work-dir* **dir**, *-w* **dir**
|
||||||
operate on files in this directory
|
operate on files in this directory
|
||||||
|
|
||||||
*--hidden*, *-h*
|
*--hidden*, *-h*
|
||||||
|
@ -38,7 +38,7 @@ Find files in the trash based on the filter flags and any filename args.
|
||||||
*--non-interactive*, *-n*
|
*--non-interactive*, *-n*
|
||||||
list files and quit
|
list files and quit
|
||||||
|
|
||||||
*--original-path* dir, *-O* dir
|
*--original-path* **dir**, *-O* **dir**
|
||||||
list files trashed from this directory
|
list files trashed from this directory
|
||||||
|
|
||||||
### restore / re
|
### restore / re
|
||||||
|
@ -50,8 +50,8 @@ Find files in the trash based on the filter flags and any filename args.
|
||||||
*--all*, *-a*
|
*--all*, *-a*
|
||||||
restore all files in trash
|
restore all files in trash
|
||||||
|
|
||||||
*--original-path* dir, *-O* dir
|
*--original-path* **dir**, *-O* **dir**
|
||||||
restore files trashed from this directory
|
*restore* files trashed from this directory
|
||||||
|
|
||||||
### clean / cl
|
### clean / cl
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ Find files in the trash based on the filter flags and any filename args.
|
||||||
*--all*, *-a*
|
*--all*, *-a*
|
||||||
clean all files in trash
|
clean all files in trash
|
||||||
|
|
||||||
*--original-path* dir, *-O* dir
|
*--original-path* **dir**, *-O* **dir**
|
||||||
remove files trashed from this directory
|
remove files trashed from this directory
|
||||||
|
|
||||||
## Flags
|
## Flags
|
||||||
|
@ -72,30 +72,30 @@ remove files trashed from this directory
|
||||||
*--confirm*, *-c*
|
*--confirm*, *-c*
|
||||||
ask for confirmation before executing any action
|
ask for confirmation before executing any action
|
||||||
|
|
||||||
*--log* level, *-l* level
|
*--log* **level**, *-l* **level**
|
||||||
set log level
|
set log level
|
||||||
|
|
||||||
### Filter flags (usable with all commands)
|
### Filter flags (usable with all commands)
|
||||||
|
|
||||||
*--match* pattern, *-m* pattern
|
*--match* **pattern**, *-m* **pattern**
|
||||||
operate on files matching regex pattern
|
operate on files matching regex pattern
|
||||||
|
|
||||||
*--glob* pattern, *-m* pattern
|
*--glob* **pattern**, *-m* **pattern**
|
||||||
operate on files matching glob
|
operate on files matching glob
|
||||||
|
|
||||||
*--not-match* pattern, *-M* pattern
|
*--not-match* **pattern**, *-M* **pattern**
|
||||||
operate on files not matching regex pattern
|
operate on files not matching regex pattern
|
||||||
|
|
||||||
*--not-glob* pattern, *-G* pattern
|
*--not-glob* **pattern**, *-G* **pattern**
|
||||||
operate on files not matching glob
|
operate on files not matching glob
|
||||||
|
|
||||||
*--on* date, *-O* date
|
*--on* **date**, *-O* **date**
|
||||||
operate on files modified on date
|
operate on files modified on date
|
||||||
|
|
||||||
*--before* date, *-B* date
|
*--before* **date**, *-B* **date**
|
||||||
operate on files modified before date
|
operate on files modified before date
|
||||||
|
|
||||||
*--after* date, *-A* date
|
*--after* **date**, *-A* **date**
|
||||||
operate on files modified after date
|
operate on files modified after date
|
||||||
|
|
||||||
*--files-only*, *-F*
|
*--files-only*, *-F*
|
||||||
|
@ -104,13 +104,13 @@ operate on files only
|
||||||
*--dirs-only*, *-D*
|
*--dirs-only*, *-D*
|
||||||
operate on directories only
|
operate on directories only
|
||||||
|
|
||||||
*--min-size* size, *-N* size
|
*--min-size* **size**, *-N* **size**
|
||||||
operate on files larger than size
|
operate on files larger than size
|
||||||
|
|
||||||
*--max-size* size, *-X* size
|
*--max-size* **size**, *-X* **size**
|
||||||
operate on files smaller than size
|
operate on files smaller than size
|
||||||
|
|
||||||
*--mode* mode, *-x* mode
|
*--mode* **mode**, *-x* **mode**
|
||||||
operate on files matching mode mode
|
operate on files matching mode mode
|
||||||
|
|
||||||
See also gt(1) or `gt --help`.
|
See also gt(1) or `gt --help`.
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -12,6 +12,7 @@ require (
|
||||||
github.com/dustin/go-humanize v1.0.1
|
github.com/dustin/go-humanize v1.0.1
|
||||||
github.com/ijt/go-anytime v1.9.2
|
github.com/ijt/go-anytime v1.9.2
|
||||||
github.com/lithammer/fuzzysearch v1.1.8
|
github.com/lithammer/fuzzysearch v1.1.8
|
||||||
|
github.com/moby/sys/mountinfo v0.7.2
|
||||||
github.com/urfave/cli/v2 v2.27.3
|
github.com/urfave/cli/v2 v2.27.3
|
||||||
gitlab.com/tymonx/go-formatter v1.5.1
|
gitlab.com/tymonx/go-formatter v1.5.1
|
||||||
golang.org/x/term v0.22.0
|
golang.org/x/term v0.22.0
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -58,6 +58,8 @@ github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6T
|
||||||
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||||
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
|
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
|
||||||
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
|
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
|
||||||
|
github.com/moby/sys/mountinfo v0.7.2 h1:1shs6aH5s4o5H2zQLn796ADW1wMrIwHsyJ2v9KouLrg=
|
||||||
|
github.com/moby/sys/mountinfo v0.7.2/go.mod h1:1YOa8w8Ih7uW0wALDUgT1dTTSBrZ+HiBLGws92L2RU4=
|
||||||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
|
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
|
||||||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
|
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
|
||||||
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
||||||
|
|
2
gt.1
2
gt.1
|
@ -5,7 +5,7 @@
|
||||||
.nh
|
.nh
|
||||||
.ad l
|
.ad l
|
||||||
.\" Begin generated content:
|
.\" Begin generated content:
|
||||||
.TH "gt" "1" "2024-07-31" "gt version v0.0.2" "User Commands"
|
.TH "gt" "1" "2024-07-31" "gt version v0.0.3" "User Commands"
|
||||||
.PP
|
.PP
|
||||||
.SH NAME
|
.SH NAME
|
||||||
.PP
|
.PP
|
||||||
|
|
6
gt.1.scd
6
gt.1.scd
|
@ -1,8 +1,8 @@
|
||||||
gt(1) ["gt version v0.0.2" ["User Commands"]]
|
gt(1) ["gt version v0.0.3" ["User Commands"]]
|
||||||
|
|
||||||
# NAME
|
# NAME
|
||||||
|
|
||||||
gt \- manual page for gt version 0.0.2
|
gt \- manual page for gt version 0.0.3
|
||||||
|
|
||||||
# DESCRIPTION
|
# DESCRIPTION
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ g(o)t(rash) is a simple, command line program to interface with the XDG Trash. F
|
||||||
|
|
||||||
## VERSION:
|
## VERSION:
|
||||||
|
|
||||||
0.0.2
|
0.0.3
|
||||||
|
|
||||||
## AUTHOR:
|
## AUTHOR:
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// Package dirs provides functions sanitize directory names.
|
// Package dirs provides functions to sanitize directory and file names.
|
||||||
package dirs
|
package dirs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -7,14 +7,20 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const sep = string(os.PathSeparator)
|
const (
|
||||||
|
sep = string(os.PathSeparator)
|
||||||
|
space = " "
|
||||||
|
spacep = "%20"
|
||||||
|
newline = "\n"
|
||||||
|
newlinep = "%0A"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
home = os.Getenv("HOME")
|
home = os.Getenv("HOME")
|
||||||
pwd, _ = os.Getwd()
|
pwd, _ = os.Getwd()
|
||||||
)
|
)
|
||||||
|
|
||||||
// UnExpand returns dir after expanding some directory shortcuts
|
// UnExpand returns dir after unexpanding some directory shortcuts
|
||||||
//
|
//
|
||||||
// $HOME -> ~
|
// $HOME -> ~
|
||||||
//
|
//
|
||||||
|
@ -35,7 +41,7 @@ func UnExpand(dir, workdir string) (outdir string) {
|
||||||
|
|
||||||
outdir = strings.Replace(outdir, home, "~", 1)
|
outdir = strings.Replace(outdir, home, "~", 1)
|
||||||
|
|
||||||
outdir = UnEscape(outdir)
|
outdir = PercentDecode(outdir)
|
||||||
|
|
||||||
if outdir == "" {
|
if outdir == "" {
|
||||||
outdir = "/"
|
outdir = "/"
|
||||||
|
@ -44,17 +50,25 @@ func UnExpand(dir, workdir string) (outdir string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func UnEscape(input string) string {
|
func PercentDecode(input string) (output string) {
|
||||||
return strings.ReplaceAll(input, "%20", " ")
|
output = strings.ReplaceAll(input, spacep, space)
|
||||||
}
|
output = strings.ReplaceAll(output, newlinep, newline)
|
||||||
|
|
||||||
func cleanDir(dir, pwd string) (out string) {
|
|
||||||
if strings.HasPrefix(dir, ".") {
|
|
||||||
out = filepath.Clean(dir)
|
|
||||||
} else if !strings.HasPrefix(dir, sep) {
|
|
||||||
out = filepath.Join(pwd, dir)
|
|
||||||
} else {
|
|
||||||
out = dir
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PercentEncode(input string) (output string) {
|
||||||
|
output = strings.ReplaceAll(input, space, spacep)
|
||||||
|
output = strings.ReplaceAll(output, newline, newlinep)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanDir(dir, pwd string) string {
|
||||||
|
if strings.HasPrefix(dir, ".") {
|
||||||
|
return filepath.Clean(dir)
|
||||||
|
} else if !strings.HasPrefix(dir, sep) {
|
||||||
|
return filepath.Join(pwd, dir)
|
||||||
|
}
|
||||||
|
return dir
|
||||||
|
}
|
||||||
|
|
154
internal/files/directorysizes.go
Normal file
154
internal/files/directorysizes.go
Normal file
|
@ -0,0 +1,154 @@
|
||||||
|
package files
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.burning.moe/celediel/gt/internal/dirs"
|
||||||
|
"github.com/charmbracelet/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
directorysizes = "directorysizes"
|
||||||
|
length int = 3
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
loadedDirSizes directorySizes
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
loadedDirSizes = readDirectorySizesFromFile()
|
||||||
|
}
|
||||||
|
|
||||||
|
type directorySize struct {
|
||||||
|
size int64
|
||||||
|
mtime int64
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
type directorySizes map[string]directorySize
|
||||||
|
|
||||||
|
func WriteDirectorySizes() {
|
||||||
|
loadedDirSizes = updateDirectorySizes(loadedDirSizes)
|
||||||
|
writeDirectorySizes(loadedDirSizes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func readDirectorySizesFromFile() directorySizes {
|
||||||
|
dirSizes := directorySizes{}
|
||||||
|
for _, trash := range getAllTrashes() {
|
||||||
|
dsf := filepath.Join(trash, directorysizes)
|
||||||
|
if _, err := os.Lstat(dsf); os.IsNotExist(err) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Open(dsf)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
var (
|
||||||
|
size int64
|
||||||
|
mtime int64
|
||||||
|
name string
|
||||||
|
)
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
split := strings.Split(line, " ")
|
||||||
|
if len(split) != length {
|
||||||
|
log.Errorf("malformed line '%s' in %s", line, dsf)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
size, err = strconv.ParseInt(split[0], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("size %s can't be int?", split[0])
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
mtime, err = strconv.ParseInt(split[1], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("mtime %s can't be int?", split[1])
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
name = dirs.PercentDecode(split[2])
|
||||||
|
|
||||||
|
dirSize := directorySize{
|
||||||
|
size: size,
|
||||||
|
mtime: mtime,
|
||||||
|
name: name,
|
||||||
|
}
|
||||||
|
dirSizes[name] = dirSize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dirSizes
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateDirectorySizes(ds directorySizes) directorySizes {
|
||||||
|
newDs := directorySizes{}
|
||||||
|
for k, v := range ds {
|
||||||
|
newDs[k] = v
|
||||||
|
}
|
||||||
|
for _, trash := range getAllTrashes() {
|
||||||
|
files, err := os.ReadDir(filepath.Join(trash, "files"))
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, file := range files {
|
||||||
|
if _, ok := loadedDirSizes[file.Name()]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
info, err := file.Info()
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !info.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
newDs[info.Name()] = directorySize{
|
||||||
|
size: calculateDirSize(filepath.Join(trash, "files", info.Name())),
|
||||||
|
mtime: info.ModTime().Unix(),
|
||||||
|
name: info.Name(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newDs
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeDirectorySizes(dirSizes directorySizes) {
|
||||||
|
// TODO: make this less bad
|
||||||
|
for _, trash := range getAllTrashes() {
|
||||||
|
var lines []string
|
||||||
|
out := filepath.Join(trash, directorysizes)
|
||||||
|
files, err := os.ReadDir(filepath.Join(trash, "files"))
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, file := range files {
|
||||||
|
if dirSize, ok := dirSizes[file.Name()]; ok {
|
||||||
|
lines = append(lines, fmt.Sprintf("%d %d ", dirSize.size, dirSize.mtime)+dirs.PercentEncode(file.Name()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile(out, []byte(strings.Join(lines, "\n")), noExecutePerm)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -26,12 +26,7 @@ func (f DiskFile) Path() string { return f.path }
|
||||||
func (f DiskFile) Date() time.Time { return f.modified }
|
func (f DiskFile) Date() time.Time { return f.modified }
|
||||||
func (f DiskFile) IsDir() bool { return f.isdir }
|
func (f DiskFile) IsDir() bool { return f.isdir }
|
||||||
func (f DiskFile) Mode() fs.FileMode { return f.mode }
|
func (f DiskFile) Mode() fs.FileMode { return f.mode }
|
||||||
func (f DiskFile) Filesize() int64 {
|
func (f DiskFile) Filesize() int64 { return f.filesize }
|
||||||
if f.isdir {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return f.filesize
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f DiskFile) String() string {
|
func (f DiskFile) String() string {
|
||||||
// this is unique enough because two files can't be named the same in the same directory
|
// this is unique enough because two files can't be named the same in the same directory
|
||||||
|
@ -53,21 +48,29 @@ func NewDisk(path string) (DiskFile, error) {
|
||||||
|
|
||||||
name := filepath.Base(abs)
|
name := filepath.Base(abs)
|
||||||
basePath := filepath.Dir(abs)
|
basePath := filepath.Dir(abs)
|
||||||
|
actualPath := filepath.Join(basePath, name)
|
||||||
|
|
||||||
|
var size int64
|
||||||
|
if info.IsDir() {
|
||||||
|
size = calculateDirSize(actualPath)
|
||||||
|
} else {
|
||||||
|
size = info.Size()
|
||||||
|
}
|
||||||
|
|
||||||
log.Debugf("%s (base:%s) (size:%s) (modified:%s) exists",
|
log.Debugf("%s (base:%s) (size:%s) (modified:%s) exists",
|
||||||
name, basePath, humanize.Bytes(uint64(info.Size())), info.ModTime())
|
name, basePath, humanize.Bytes(uint64(size)), info.ModTime())
|
||||||
|
|
||||||
return DiskFile{
|
return DiskFile{
|
||||||
name: name,
|
name: name,
|
||||||
path: filepath.Join(basePath, name),
|
path: actualPath,
|
||||||
filesize: info.Size(),
|
filesize: size,
|
||||||
modified: info.ModTime(),
|
modified: info.ModTime(),
|
||||||
isdir: info.IsDir(),
|
isdir: info.IsDir(),
|
||||||
mode: info.Mode(),
|
mode: info.Mode(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindDisk(dir string, recursive bool, fltr *filter.Filter) (Files, error) {
|
func FindDisk(dir string, recursive bool, fltr *filter.Filter) Files {
|
||||||
var files Files
|
var files Files
|
||||||
dir = filepath.Clean(dir)
|
dir = filepath.Clean(dir)
|
||||||
if dir == "." || dir == "" {
|
if dir == "." || dir == "" {
|
||||||
|
@ -91,7 +94,7 @@ func FindDisk(dir string, recursive bool, fltr *filter.Filter) (Files, error) {
|
||||||
files = append(files, readDir(dir, fltr)...)
|
files = append(files, readDir(dir, fltr)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return files, nil
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
// isInHiddenDir checks `path` and parent directories
|
// isInHiddenDir checks `path` and parent directories
|
||||||
|
@ -130,10 +133,17 @@ func walkDir(dir string, fltr *filter.Filter) Files {
|
||||||
name := dirEntry.Name()
|
name := dirEntry.Name()
|
||||||
info, _ := dirEntry.Info()
|
info, _ := dirEntry.Info()
|
||||||
if fltr.Match(info) {
|
if fltr.Match(info) {
|
||||||
|
var size int64
|
||||||
|
if info.IsDir() {
|
||||||
|
size = calculateDirSize(filepath.Join(actualPath, name))
|
||||||
|
} else {
|
||||||
|
size = info.Size()
|
||||||
|
}
|
||||||
|
|
||||||
files = append(files, DiskFile{
|
files = append(files, DiskFile{
|
||||||
path: actualPath,
|
path: actualPath,
|
||||||
name: name,
|
name: name,
|
||||||
filesize: info.Size(),
|
filesize: size,
|
||||||
modified: info.ModTime(),
|
modified: info.ModTime(),
|
||||||
isdir: info.IsDir(),
|
isdir: info.IsDir(),
|
||||||
mode: info.Mode(),
|
mode: info.Mode(),
|
||||||
|
@ -143,7 +153,7 @@ func walkDir(dir string, fltr *filter.Filter) Files {
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error walking directory %s: %s", dir, err)
|
log.Errorf("error walking directory %s: %s", dir, err)
|
||||||
return Files{}
|
return nil
|
||||||
}
|
}
|
||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
@ -152,7 +162,7 @@ func readDir(dir string, fltr *filter.Filter) Files {
|
||||||
var files Files
|
var files Files
|
||||||
fs, err := os.ReadDir(dir)
|
fs, err := os.ReadDir(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Files{}
|
return nil
|
||||||
}
|
}
|
||||||
for _, file := range fs {
|
for _, file := range fs {
|
||||||
name := file.Name()
|
name := file.Name()
|
||||||
|
@ -163,17 +173,28 @@ func readDir(dir string, fltr *filter.Filter) Files {
|
||||||
|
|
||||||
info, err := file.Info()
|
info, err := file.Info()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Files{}
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
path := filepath.Dir(filepath.Join(dir, name))
|
path := filepath.Dir(filepath.Join(dir, name))
|
||||||
|
actualPath, e := filepath.Abs(path)
|
||||||
|
if e != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if fltr.Match(info) {
|
if fltr.Match(info) {
|
||||||
|
var size int64
|
||||||
|
if info.IsDir() {
|
||||||
|
size = calculateDirSize(filepath.Join(actualPath, name))
|
||||||
|
} else {
|
||||||
|
size = info.Size()
|
||||||
|
}
|
||||||
|
|
||||||
files = append(files, DiskFile{
|
files = append(files, DiskFile{
|
||||||
name: name,
|
name: name,
|
||||||
path: filepath.Join(path, name),
|
path: filepath.Join(actualPath, name),
|
||||||
modified: info.ModTime(),
|
modified: info.ModTime(),
|
||||||
filesize: info.Size(),
|
filesize: size,
|
||||||
isdir: info.IsDir(),
|
isdir: info.IsDir(),
|
||||||
mode: info.Mode(),
|
mode: info.Mode(),
|
||||||
})
|
})
|
||||||
|
|
|
@ -3,11 +3,15 @@ package files
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"cmp"
|
"cmp"
|
||||||
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/charmbracelet/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type File interface {
|
type File interface {
|
||||||
|
@ -22,6 +26,35 @@ type File interface {
|
||||||
|
|
||||||
type Files []File
|
type Files []File
|
||||||
|
|
||||||
|
func (fls Files) String() string {
|
||||||
|
var out = strings.Builder{}
|
||||||
|
for _, file := range fls {
|
||||||
|
out.WriteString(fmt.Sprintf("%s\t%s\t%s\n",
|
||||||
|
file.Date().Format(time.RFC3339), file.Name(), file.Path(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
return out.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fls Files) TotalSize() int64 {
|
||||||
|
var size int64
|
||||||
|
|
||||||
|
for _, file := range fls {
|
||||||
|
if file.IsDir() {
|
||||||
|
if d, ok := loadedDirSizes[file.Name()]; ok {
|
||||||
|
log.Debugf("%s: got %d from directorysizes", file.Name(), d.size)
|
||||||
|
size += d.size
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
size += calculateDirSize(file.Path())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
size += file.Filesize()
|
||||||
|
}
|
||||||
|
|
||||||
|
return size
|
||||||
|
}
|
||||||
|
|
||||||
func SortByModified(a, b File) int {
|
func SortByModified(a, b File) int {
|
||||||
if a.Date().Before(b.Date()) {
|
if a.Date().Before(b.Date()) {
|
||||||
return 1
|
return 1
|
||||||
|
@ -41,15 +74,11 @@ func SortByModifiedReverse(a, b File) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
func SortBySize(a, b File) int {
|
func SortBySize(a, b File) int {
|
||||||
as := getSortingSize(a)
|
return cmp.Compare(a.Filesize(), b.Filesize())
|
||||||
bs := getSortingSize(b)
|
|
||||||
return cmp.Compare(as, bs)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func SortBySizeReverse(a, b File) int {
|
func SortBySizeReverse(a, b File) int {
|
||||||
as := getSortingSize(a)
|
return cmp.Compare(b.Filesize(), a.Filesize())
|
||||||
bs := getSortingSize(b)
|
|
||||||
return cmp.Compare(bs, as)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func SortByName(a, b File) int {
|
func SortByName(a, b File) int {
|
||||||
|
@ -112,9 +141,36 @@ func doNameSort(a, b File) int {
|
||||||
return cmp.Compare(aname, bname)
|
return cmp.Compare(aname, bname)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSortingSize(f File) int64 {
|
func calculateDirSize(path string) int64 {
|
||||||
if f.IsDir() {
|
var size int64
|
||||||
return -1
|
info, err := os.Lstat(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
return f.Filesize()
|
if !info.IsDir() {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
files, err := os.ReadDir(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
filePath := filepath.Join(path, file.Name())
|
||||||
|
info, err := os.Lstat(filePath)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
if info.IsDir() {
|
||||||
|
size += calculateDirSize(filePath)
|
||||||
|
} else {
|
||||||
|
size += info.Size()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return size
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,9 @@ import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -13,16 +15,20 @@ import (
|
||||||
"git.burning.moe/celediel/gt/internal/filter"
|
"git.burning.moe/celediel/gt/internal/filter"
|
||||||
"git.burning.moe/celediel/gt/internal/prompt"
|
"git.burning.moe/celediel/gt/internal/prompt"
|
||||||
|
|
||||||
|
"github.com/adrg/xdg"
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
"github.com/dustin/go-humanize"
|
"github.com/dustin/go-humanize"
|
||||||
|
"github.com/moby/sys/mountinfo"
|
||||||
"gitlab.com/tymonx/go-formatter/formatter"
|
"gitlab.com/tymonx/go-formatter/formatter"
|
||||||
"gopkg.in/ini.v1"
|
"gopkg.in/ini.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
executePerm = fs.FileMode(0755)
|
executePerm = fs.FileMode(0755)
|
||||||
|
noExecutePerm = fs.FileMode(0644)
|
||||||
noExecuteUserPerm = fs.FileMode(0600)
|
noExecuteUserPerm = fs.FileMode(0600)
|
||||||
randomStrLength int = 8
|
randomStrLength int = 8
|
||||||
|
trashName string = ".Trash"
|
||||||
trashInfoExt string = ".trashinfo"
|
trashInfoExt string = ".trashinfo"
|
||||||
trashInfoSec string = "Trash Info"
|
trashInfoSec string = "Trash Info"
|
||||||
trashInfoPath string = "Path"
|
trashInfoPath string = "Path"
|
||||||
|
@ -34,6 +40,8 @@ DeletionDate={date}
|
||||||
`
|
`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var homeTrash = filepath.Join(xdg.DataHome, "Trash")
|
||||||
|
|
||||||
type TrashInfo struct {
|
type TrashInfo struct {
|
||||||
name, ogpath string
|
name, ogpath string
|
||||||
path, trashinfo string
|
path, trashinfo string
|
||||||
|
@ -50,54 +58,139 @@ func (t TrashInfo) TrashInfo() string { return t.trashinfo }
|
||||||
func (t TrashInfo) Date() time.Time { return t.trashed }
|
func (t TrashInfo) Date() time.Time { return t.trashed }
|
||||||
func (t TrashInfo) IsDir() bool { return t.isdir }
|
func (t TrashInfo) IsDir() bool { return t.isdir }
|
||||||
func (t TrashInfo) Mode() fs.FileMode { return t.mode }
|
func (t TrashInfo) Mode() fs.FileMode { return t.mode }
|
||||||
func (t TrashInfo) Filesize() int64 {
|
func (t TrashInfo) Filesize() int64 { return t.filesize }
|
||||||
if t.isdir {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return t.filesize
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t TrashInfo) String() string {
|
func (t TrashInfo) String() string {
|
||||||
return t.name + t.path + t.ogpath + t.trashinfo
|
return t.name + t.path + t.ogpath + t.trashinfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) {
|
func FindInAllTrashes(ogdir string, fltr *filter.Filter) Files {
|
||||||
var files Files
|
var files Files
|
||||||
outerr := filepath.WalkDir(trashdir, func(path string, dirEntry fs.DirEntry, err error) error {
|
|
||||||
|
for _, trash := range getAllTrashes() {
|
||||||
|
fls, err := findTrash(trash, ogdir, fltr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("what happened?? what is %s?", err)
|
log.Errorf("error reading trash dir '%s': %s", trash, err)
|
||||||
return err
|
continue
|
||||||
|
}
|
||||||
|
files = append(files, fls...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ignore self, directories, and non trashinfo files
|
return files
|
||||||
if path == trashdir || dirEntry.IsDir() || filepath.Ext(path) != trashInfoExt {
|
}
|
||||||
|
|
||||||
|
func ConfirmRestore(confirm bool, fs Files) error {
|
||||||
|
if !confirm || prompt.YesNo(fmt.Sprintf("restore %d selected files?", len(fs))) {
|
||||||
|
log.Info("doing the thing")
|
||||||
|
restored, err := restore(fs)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("restored %d files before error %w", restored, err)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(os.Stdout, "restored %d files\n", restored)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(os.Stdout, "not doing anything\n")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ConfirmClean(confirm bool, fs Files) error {
|
||||||
|
if prompt.YesNo(fmt.Sprintf("remove %d selected files permanently from the trash?", len(fs))) &&
|
||||||
|
(!confirm || prompt.YesNo(fmt.Sprintf("really remove all these %d selected files permanently from the trash forever??", len(fs)))) {
|
||||||
|
removed, err := remove(fs)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("removed %d files before error %w", removed, err)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(os.Stdout, "removed %d files\n", removed)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(os.Stdout, "not doing anything\n")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ConfirmTrash(confirm bool, fs Files) error {
|
||||||
|
if !confirm || prompt.YesNo(fmt.Sprintf("trash %d selected files?", len(fs))) {
|
||||||
|
tfs := make([]string, 0, len(fs))
|
||||||
|
for _, file := range fs {
|
||||||
|
tfs = append(tfs, file.Path())
|
||||||
|
}
|
||||||
|
|
||||||
|
trashed := trashFiles(tfs)
|
||||||
|
|
||||||
|
var s string
|
||||||
|
if trashed > 1 {
|
||||||
|
s = "s"
|
||||||
|
}
|
||||||
|
fmt.Fprintf(os.Stdout, "trashed %d file%s\n", trashed, s)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(os.Stdout, "not doing anything\n")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func findTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) {
|
||||||
|
log.Debugf("searching for trashinfo files in %s", trashdir)
|
||||||
|
var files Files
|
||||||
|
|
||||||
|
infodir := filepath.Join(trashdir, "info")
|
||||||
|
entries, err := os.ReadDir(infodir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, entry := range entries {
|
||||||
|
if entry.IsDir() || filepath.Ext(entry.Name()) != trashInfoExt {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
path := filepath.Join(infodir, entry.Name())
|
||||||
|
|
||||||
// trashinfo is just an ini file, so
|
// trashinfo is just an ini file, so
|
||||||
c, err := ini.Load(path)
|
trashInfo, err := ini.Load(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
log.Errorf("error reading %s: %s", path, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
section := trashInfo.Section(trashInfoSec)
|
||||||
|
if section == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
basepath := dirs.PercentDecode(section.Key(trashInfoPath).String())
|
||||||
|
if !strings.HasPrefix(basepath, string(os.PathSeparator)) {
|
||||||
|
root, err := getRoot(trashdir)
|
||||||
|
if err == nil {
|
||||||
|
basepath = filepath.Join(root, basepath)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if section := c.Section(trashInfoSec); section != nil {
|
|
||||||
basepath := section.Key(trashInfoPath).String()
|
|
||||||
|
|
||||||
filename := filepath.Base(basepath)
|
filename := filepath.Base(basepath)
|
||||||
trashedpath := strings.Replace(strings.Replace(path, "info", "files", 1), trashInfoExt, "", 1)
|
trashedpath := strings.Replace(strings.Replace(path, "info", "files", 1), trashInfoExt, "", 1)
|
||||||
info, err := os.Lstat(trashedpath)
|
info, err := os.Lstat(trashedpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error reading %s: %s", trashedpath, err)
|
log.Errorf("error reading '%s': %s", trashedpath, err)
|
||||||
return nil
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
s := section.Key(trashInfoDate).Value()
|
s := section.Key(trashInfoDate).Value()
|
||||||
date, err := time.ParseInLocation(trashInfoDateFmt, s, time.Local)
|
date, err := time.ParseInLocation(trashInfoDateFmt, s, time.Local)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
log.Errorf("error parsing date '%s' in trashinfo file '%s': %s", s, path, err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if ogdir != "" && filepath.Dir(basepath) != ogdir {
|
if ogdir != "" && filepath.Dir(basepath) != ogdir {
|
||||||
return nil
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var size int64
|
||||||
|
if d, ok := loadedDirSizes[info.Name()]; ok {
|
||||||
|
size = d.size
|
||||||
|
} else if info.IsDir() {
|
||||||
|
size = calculateDirSize(trashedpath)
|
||||||
|
} else {
|
||||||
|
size = info.Size()
|
||||||
}
|
}
|
||||||
|
|
||||||
if fltr.Match(info) {
|
if fltr.Match(info) {
|
||||||
|
@ -108,19 +201,66 @@ func FindTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) {
|
||||||
trashinfo: path,
|
trashinfo: path,
|
||||||
trashed: date,
|
trashed: date,
|
||||||
isdir: info.IsDir(),
|
isdir: info.IsDir(),
|
||||||
filesize: info.Size(),
|
filesize: size,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if outerr != nil {
|
|
||||||
return Files{}, outerr
|
|
||||||
}
|
|
||||||
return files, nil
|
return files, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Restore(files Files) (restored int, err error) {
|
func trashFile(filename string) error {
|
||||||
|
trashDir, err := getTrashDir(filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
trashInfoFilename, outPath := getTrashFilenames(filepath.Base(filename), trashDir)
|
||||||
|
|
||||||
|
if err := os.Rename(filename, outPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var path string
|
||||||
|
if trashDir == homeTrash {
|
||||||
|
path = filename
|
||||||
|
} else {
|
||||||
|
root, err := getRoot(trashDir)
|
||||||
|
if err != nil {
|
||||||
|
path = filename
|
||||||
|
} else {
|
||||||
|
path = strings.Replace(filename, root+string(os.PathSeparator), "", 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Debugf("fucking %s %s %s", filename, trashDir, path)
|
||||||
|
|
||||||
|
trashInfo, err := formatter.Format(trashInfoTemplate, formatter.Named{
|
||||||
|
"path": path,
|
||||||
|
"date": time.Now().Format(trashInfoDateFmt),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.WriteFile(trashInfoFilename, []byte(trashInfo), noExecuteUserPerm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func trashFiles(files []string) (trashed int) {
|
||||||
|
for _, file := range files {
|
||||||
|
if err := trashFile(file); err != nil {
|
||||||
|
log.Errorf("cannot trash '%s': %s", file, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
trashed++
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func restore(files Files) (restored int, err error) {
|
||||||
for _, maybeFile := range files {
|
for _, maybeFile := range files {
|
||||||
file, ok := maybeFile.(TrashInfo)
|
file, ok := maybeFile.(TrashInfo)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -128,7 +268,7 @@ func Restore(files Files) (restored int, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var cancel bool
|
var cancel bool
|
||||||
outpath := dirs.UnEscape(file.ogpath)
|
outpath := dirs.PercentDecode(file.ogpath)
|
||||||
log.Infof("restoring %s back to %s\n", file.name, outpath)
|
log.Infof("restoring %s back to %s\n", file.name, outpath)
|
||||||
if _, e := os.Lstat(outpath); e == nil {
|
if _, e := os.Lstat(outpath); e == nil {
|
||||||
outpath, cancel = prompt.NewPath(outpath)
|
outpath, cancel = prompt.NewPath(outpath)
|
||||||
|
@ -139,7 +279,7 @@ func Restore(files Files) (restored int, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
basedir := filepath.Dir(outpath)
|
basedir := filepath.Dir(outpath)
|
||||||
if _, e := os.Stat(basedir); e != nil {
|
if _, e := os.Lstat(basedir); e != nil {
|
||||||
if err = os.MkdirAll(basedir, executePerm); err != nil {
|
if err = os.MkdirAll(basedir, executePerm); err != nil {
|
||||||
return restored, err
|
return restored, err
|
||||||
}
|
}
|
||||||
|
@ -158,21 +298,7 @@ func Restore(files Files) (restored int, err error) {
|
||||||
return restored, err
|
return restored, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConfirmRestore(confirm bool, fs Files) error {
|
func remove(files Files) (removed int, err error) {
|
||||||
if !confirm || prompt.YesNo(fmt.Sprintf("restore %d selected files?", len(fs))) {
|
|
||||||
log.Info("doing the thing")
|
|
||||||
restored, err := Restore(fs)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("restored %d files before error %w", restored, err)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(os.Stdout, "restored %d files\n", restored)
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(os.Stdout, "not doing anything\n")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func Remove(files Files) (removed int, err error) {
|
|
||||||
for _, maybeFile := range files {
|
for _, maybeFile := range files {
|
||||||
file, ok := maybeFile.(TrashInfo)
|
file, ok := maybeFile.(TrashInfo)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -197,82 +323,7 @@ func Remove(files Files) (removed int, err error) {
|
||||||
return removed, err
|
return removed, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConfirmClean(confirm bool, fs Files) error {
|
func randomString(length int) string {
|
||||||
if prompt.YesNo(fmt.Sprintf("remove %d selected files permanently from the trash?", len(fs))) &&
|
|
||||||
(!confirm || prompt.YesNo(fmt.Sprintf("really remove all these %d selected files permanently from the trash forever??", len(fs)))) {
|
|
||||||
removed, err := Remove(fs)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("removed %d files before error %w", removed, err)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(os.Stdout, "removed %d files\n", removed)
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(os.Stdout, "not doing anything\n")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TrashFile(trashDir, name string) error {
|
|
||||||
trashinfoFilename, outPath := ensureUniqueName(filepath.Base(name), trashDir)
|
|
||||||
|
|
||||||
if err := os.Rename(name, outPath); err != nil {
|
|
||||||
if strings.Contains(err.Error(), "invalid cross-device link") {
|
|
||||||
// TODO: use $topdir/.Trash as per XDG spec
|
|
||||||
// TODO: maybe figure out if filesystem is truly different or is a btrfs subvolume
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
trashInfo, err := formatter.Format(trashInfoTemplate, formatter.Named{
|
|
||||||
"path": name,
|
|
||||||
"date": time.Now().Format(trashInfoDateFmt),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := os.WriteFile(trashinfoFilename, []byte(trashInfo), noExecuteUserPerm); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TrashFiles(trashDir string, files ...string) (trashed int) {
|
|
||||||
for _, file := range files {
|
|
||||||
if err := TrashFile(trashDir, file); err != nil {
|
|
||||||
log.Errorf("cannot trash '%s': %s", file, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
trashed++
|
|
||||||
}
|
|
||||||
return trashed
|
|
||||||
}
|
|
||||||
|
|
||||||
func ConfirmTrash(confirm bool, fs Files, trashDir string) error {
|
|
||||||
if !confirm || prompt.YesNo(fmt.Sprintf("trash %d selected files?", len(fs))) {
|
|
||||||
tfs := make([]string, 0, len(fs))
|
|
||||||
for _, file := range fs {
|
|
||||||
log.Debugf("gonna trash %s", file.Path())
|
|
||||||
tfs = append(tfs, file.Path())
|
|
||||||
}
|
|
||||||
|
|
||||||
trashed := TrashFiles(trashDir, tfs...)
|
|
||||||
|
|
||||||
var files string
|
|
||||||
if trashed == 1 {
|
|
||||||
files = "file"
|
|
||||||
} else {
|
|
||||||
files = "files"
|
|
||||||
}
|
|
||||||
fmt.Fprintf(os.Stdout, "trashed %d %s\n", trashed, files)
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(os.Stdout, "not doing anything\n")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func randomFilename(length int) string {
|
|
||||||
out := strings.Builder{}
|
out := strings.Builder{}
|
||||||
for range length {
|
for range length {
|
||||||
out.WriteByte(randomChar())
|
out.WriteByte(randomChar())
|
||||||
|
@ -285,17 +336,17 @@ func randomChar() byte {
|
||||||
return chars[rand.Intn(len(chars))]
|
return chars[rand.Intn(len(chars))]
|
||||||
}
|
}
|
||||||
|
|
||||||
func ensureUniqueName(filename, trashDir string) (string, string) {
|
func getTrashFilenames(filename, trashDir string) (string, string) {
|
||||||
var (
|
var (
|
||||||
filedir = filepath.Join(trashDir, "files")
|
filedir = filepath.Join(trashDir, "files")
|
||||||
infodir = filepath.Join(trashDir, "info")
|
infodir = filepath.Join(trashDir, "info")
|
||||||
)
|
)
|
||||||
|
|
||||||
info := filepath.Join(infodir, filename+trashInfoExt)
|
info := filepath.Join(infodir, filename+trashInfoExt)
|
||||||
if _, err := os.Stat(info); os.IsNotExist(err) {
|
if _, err := os.Lstat(info); os.IsNotExist(err) {
|
||||||
// doesn't exist, so use it
|
// doesn't exist, so use it
|
||||||
path := filepath.Join(filedir, filename)
|
path := filepath.Join(filedir, filename)
|
||||||
return info, path
|
return dirs.PercentEncode(info), dirs.PercentEncode(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// otherwise, try random suffixes until one works
|
// otherwise, try random suffixes until one works
|
||||||
|
@ -303,12 +354,106 @@ func ensureUniqueName(filename, trashDir string) (string, string) {
|
||||||
var tries int
|
var tries int
|
||||||
for {
|
for {
|
||||||
tries++
|
tries++
|
||||||
rando := randomFilename(randomStrLength)
|
rando := randomString(randomStrLength)
|
||||||
newName := filepath.Join(infodir, filename+rando+trashInfoExt)
|
newInfo := filepath.Join(infodir, filename+rando+trashInfoExt)
|
||||||
if _, err := os.Stat(newName); os.IsNotExist(err) {
|
newFile := filepath.Join(filedir, filename+rando)
|
||||||
|
_, infoErr := os.Lstat(newInfo)
|
||||||
|
_, fileErr := os.Lstat(newFile)
|
||||||
|
if os.IsNotExist(infoErr) && os.IsNotExist(fileErr) {
|
||||||
path := filepath.Join(filedir, filename+rando)
|
path := filepath.Join(filedir, filename+rando)
|
||||||
log.Debugf("settled on random name %s%s on the %s try", filename, rando, humanize.Ordinal(tries))
|
log.Debugf("settled on random name %s%s on the %s try", filename, rando, humanize.Ordinal(tries))
|
||||||
return newName, path
|
return dirs.PercentEncode(newInfo), dirs.PercentEncode(path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getTrashDir(filename string) (string, error) {
|
||||||
|
root, err := getRoot(filename)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
var trashDir string
|
||||||
|
if strings.Contains(filename, xdg.Home) {
|
||||||
|
trashDir = filepath.Join(xdg.DataHome, trashName[1:])
|
||||||
|
} else {
|
||||||
|
trashDir = filepath.Clean(filepath.Join(root, trashName))
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Lstat(trashDir); err != nil {
|
||||||
|
usr, _ := user.Current()
|
||||||
|
trashDir += "-" + usr.Uid
|
||||||
|
if err := os.Mkdir(trashDir, executePerm); err != nil {
|
||||||
|
return "", fmt.Errorf("%s%s does not exist and creation of %s failed", root, trashName, trashDir)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if link, err := os.Readlink(trashDir); err == nil && link != "" {
|
||||||
|
return "", fmt.Errorf("trash dir %s is a symbolic link", trashDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
return trashDir, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRoot(path string) (string, error) {
|
||||||
|
var roots []string
|
||||||
|
|
||||||
|
// populate a list of mountpoints on the system
|
||||||
|
_, err := mountinfo.GetMounts(func(i *mountinfo.Info) (skip bool, stop bool) {
|
||||||
|
roots = append(roots, i.Mountpoint)
|
||||||
|
return false, false
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var depth uint8 = 1 // 255 seems a reasonable recursion maximum
|
||||||
|
current := path
|
||||||
|
|
||||||
|
// recursively search upwards by using filepath.Clean and ..
|
||||||
|
for {
|
||||||
|
if depth == 0 {
|
||||||
|
return path, fmt.Errorf("reached max depth getting root of %s", path)
|
||||||
|
}
|
||||||
|
|
||||||
|
current = filepath.Clean(current)
|
||||||
|
|
||||||
|
if current == string(os.PathSeparator) || slices.Contains(roots, current) {
|
||||||
|
return current, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
current += string(os.PathSeparator) + ".."
|
||||||
|
depth++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getAllTrashes() []string {
|
||||||
|
trashes := []string{homeTrash}
|
||||||
|
usr, err := user.Current()
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = mountinfo.GetMounts(func(mount *mountinfo.Info) (skip bool, stop bool) {
|
||||||
|
point := mount.Mountpoint
|
||||||
|
trashDir := filepath.Clean(filepath.Join(point, trashName))
|
||||||
|
userTrashDir := trashDir + "-" + usr.Uid
|
||||||
|
|
||||||
|
if _, err := os.Lstat(trashDir); err == nil {
|
||||||
|
trashes = append(trashes, trashDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Lstat(userTrashDir); err == nil {
|
||||||
|
trashes = append(trashes, userTrashDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, false
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("error reading mounts: %s", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return trashes
|
||||||
|
}
|
||||||
|
|
|
@ -73,6 +73,7 @@ type model struct {
|
||||||
keys keyMap
|
keys keyMap
|
||||||
selected map[string]bool
|
selected map[string]bool
|
||||||
selectsize int64
|
selectsize int64
|
||||||
|
totalsize int64
|
||||||
readonly bool
|
readonly bool
|
||||||
once bool
|
once bool
|
||||||
filtering bool
|
filtering bool
|
||||||
|
@ -86,8 +87,8 @@ type model struct {
|
||||||
fltrfiles files.Files
|
fltrfiles files.Files
|
||||||
}
|
}
|
||||||
|
|
||||||
func newModel(fls []files.File, selectall, readonly, once bool, workdir string, mode modes.Mode) (m model) {
|
func newModel(fls files.Files, selectall, readonly, once bool, workdir string, mode modes.Mode) model {
|
||||||
m = model{
|
m := model{
|
||||||
keys: defaultKeyMap(),
|
keys: defaultKeyMap(),
|
||||||
readonly: readonly,
|
readonly: readonly,
|
||||||
once: once,
|
once: once,
|
||||||
|
@ -95,6 +96,7 @@ func newModel(fls []files.File, selectall, readonly, once bool, workdir string,
|
||||||
selected: map[string]bool{},
|
selected: map[string]bool{},
|
||||||
selectsize: 0,
|
selectsize: 0,
|
||||||
files: fls,
|
files: fls,
|
||||||
|
totalsize: fls.TotalSize(),
|
||||||
}
|
}
|
||||||
|
|
||||||
m.termwidth, m.termheight = termSizes()
|
m.termwidth, m.termheight = termSizes()
|
||||||
|
@ -116,7 +118,7 @@ func newModel(fls []files.File, selectall, readonly, once bool, workdir string,
|
||||||
m.selectAll()
|
m.selectAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
type keyMap struct {
|
type keyMap struct {
|
||||||
|
@ -331,6 +333,8 @@ func (m model) header() string {
|
||||||
keysFmt = strings.Join(keys, wideDot)
|
keysFmt = strings.Join(keys, wideDot)
|
||||||
selectFmt = strings.Join(selectKeys, wideDot)
|
selectFmt = strings.Join(selectKeys, wideDot)
|
||||||
filterFmt = strings.Join(filterKeys, wideDot)
|
filterFmt = strings.Join(filterKeys, wideDot)
|
||||||
|
totalSize = humanize.Bytes(uint64(m.totalsize))
|
||||||
|
selectedSize = fmt.Sprintf("%s/%s", humanize.Bytes(uint64(m.selectsize)), totalSize)
|
||||||
)
|
)
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
|
@ -338,24 +342,23 @@ func (m model) header() string {
|
||||||
right = fmt.Sprintf(" Filtering %s %s", dot, filterFmt)
|
right = fmt.Sprintf(" Filtering %s %s", dot, filterFmt)
|
||||||
case m.mode == modes.Interactive:
|
case m.mode == modes.Interactive:
|
||||||
right = fmt.Sprintf(" %s %s %s", keysFmt, dot, selectFmt)
|
right = fmt.Sprintf(" %s %s %s", keysFmt, dot, selectFmt)
|
||||||
left = fmt.Sprintf("%d/%d %s %s", len(m.selected), len(m.fltrfiles), dot, humanize.Bytes(uint64(m.selectsize)))
|
left = fmt.Sprintf("%d/%d %s %s", len(m.selected), len(m.fltrfiles), dot, selectedSize)
|
||||||
case m.mode == modes.Listing:
|
case m.mode == modes.Listing:
|
||||||
var filtered string
|
var filtered string
|
||||||
if m.filter != "" || m.filtering {
|
if m.filter != "" || m.filtering {
|
||||||
filtered = " (filtered)"
|
filtered = " (filtered)"
|
||||||
}
|
}
|
||||||
right = fmt.Sprintf(" Showing%s %d files in trash", filtered, len(m.fltrfiles))
|
right = fmt.Sprintf(" Showing%s %d files in trash (%s)", filtered, len(m.fltrfiles), totalSize)
|
||||||
default:
|
default:
|
||||||
var wd string
|
var wd string
|
||||||
if m.workdir != "" {
|
if m.workdir != "" {
|
||||||
wd = " in " + dirs.UnExpand(m.workdir, "")
|
wd = " in " + dirs.UnExpand(m.workdir, "")
|
||||||
}
|
}
|
||||||
right = fmt.Sprintf(" %s%s %s %s", m.mode.String(), wd, dot, selectFmt)
|
right = fmt.Sprintf(" %s%s %s %s", m.mode.String(), wd, dot, selectFmt)
|
||||||
left = fmt.Sprintf("%d/%d %s %s", len(m.selected), len(m.fltrfiles), dot, humanize.Bytes(uint64(m.selectsize)))
|
left = fmt.Sprintf("%d/%d %s %s", len(m.selected), len(m.fltrfiles), dot, selectedSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
// offset of 2 again because of table border
|
spacerWidth = (m.termwidth - lipgloss.Width(right) - lipgloss.Width(left)) + 1
|
||||||
spacerWidth = m.termwidth - lipgloss.Width(right) - lipgloss.Width(left) - poffset
|
|
||||||
if spacerWidth <= 0 {
|
if spacerWidth <= 0 {
|
||||||
spacerWidth = 1 // always at least one space
|
spacerWidth = 1 // always at least one space
|
||||||
}
|
}
|
||||||
|
@ -662,14 +665,14 @@ func Show(fls files.Files, once bool, workdir string) error {
|
||||||
|
|
||||||
func newRow(file files.File, workdir string) table.Row {
|
func newRow(file files.File, workdir string) table.Row {
|
||||||
var time, size string
|
var time, size string
|
||||||
|
name := file.Name()
|
||||||
time = humanize.Time(file.Date())
|
time = humanize.Time(file.Date())
|
||||||
if file.IsDir() {
|
if file.IsDir() {
|
||||||
size = bar
|
name += string(os.PathSeparator)
|
||||||
} else {
|
|
||||||
size = humanize.Bytes(uint64(file.Filesize()))
|
|
||||||
}
|
}
|
||||||
|
size = humanize.Bytes(uint64(file.Filesize()))
|
||||||
return table.Row{
|
return table.Row{
|
||||||
dirs.UnEscape(file.Name()),
|
dirs.PercentDecode(name),
|
||||||
dirs.UnExpand(filepath.Dir(file.Path()), workdir),
|
dirs.UnExpand(filepath.Dir(file.Path()), workdir),
|
||||||
time,
|
time,
|
||||||
size,
|
size,
|
||||||
|
|
|
@ -52,7 +52,7 @@ func NewPath(path string) (string, bool) {
|
||||||
Run(); err != nil {
|
Run(); err != nil {
|
||||||
return path, false
|
return path, false
|
||||||
}
|
}
|
||||||
if _, e := os.Stat(path); e != nil {
|
if _, e := os.Lstat(path); e != nil {
|
||||||
return path, false
|
return path, false
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
2
justfile
2
justfile
|
@ -1,5 +1,5 @@
|
||||||
binary := "gt"
|
binary := "gt"
|
||||||
version := "0.0.2"
|
version := "0.0.3"
|
||||||
build_dir := "bin"
|
build_dir := "bin"
|
||||||
dist_dir := "dist"
|
dist_dir := "dist"
|
||||||
cmd := "."
|
cmd := "."
|
||||||
|
|
75
main.go
75
main.go
|
@ -4,6 +4,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
|
@ -14,6 +15,7 @@ import (
|
||||||
"git.burning.moe/celediel/gt/internal/filter"
|
"git.burning.moe/celediel/gt/internal/filter"
|
||||||
"git.burning.moe/celediel/gt/internal/interactive"
|
"git.burning.moe/celediel/gt/internal/interactive"
|
||||||
"git.burning.moe/celediel/gt/internal/interactive/modes"
|
"git.burning.moe/celediel/gt/internal/interactive/modes"
|
||||||
|
"golang.org/x/term"
|
||||||
|
|
||||||
"github.com/adrg/xdg"
|
"github.com/adrg/xdg"
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
|
@ -23,7 +25,7 @@ import (
|
||||||
const (
|
const (
|
||||||
appname string = "gt"
|
appname string = "gt"
|
||||||
appsubtitle string = "xdg trash cli"
|
appsubtitle string = "xdg trash cli"
|
||||||
appversion string = "v0.0.2"
|
appversion string = "v0.0.3"
|
||||||
appdesc string = `A small command line program to interface with the
|
appdesc string = `A small command line program to interface with the
|
||||||
Freedesktop.org / XDG trash specification.
|
Freedesktop.org / XDG trash specification.
|
||||||
|
|
||||||
|
@ -34,8 +36,8 @@ See gt(1) for more information.`
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
loglvl string
|
|
||||||
fltr *filter.Filter
|
fltr *filter.Filter
|
||||||
|
loglvl string
|
||||||
onArg, beforeArg, afterArg string
|
onArg, beforeArg, afterArg string
|
||||||
globArg, patternArg string
|
globArg, patternArg string
|
||||||
unGlobArg, unPatternArg string
|
unGlobArg, unPatternArg string
|
||||||
|
@ -45,10 +47,13 @@ var (
|
||||||
askconfirm, all bool
|
askconfirm, all bool
|
||||||
workdir, ogdir cli.Path
|
workdir, ogdir cli.Path
|
||||||
recursive bool
|
recursive bool
|
||||||
|
isTerminal bool
|
||||||
trashDir = filepath.Join(xdg.DataHome, "Trash")
|
|
||||||
|
|
||||||
beforeAll = func(_ *cli.Context) error {
|
beforeAll = func(_ *cli.Context) error {
|
||||||
|
if term.IsTerminal(int(os.Stdout.Fd())) && term.IsTerminal(int(os.Stdin.Fd())) {
|
||||||
|
isTerminal = true
|
||||||
|
}
|
||||||
|
|
||||||
// setup log
|
// setup log
|
||||||
log.SetReportTimestamp(true)
|
log.SetReportTimestamp(true)
|
||||||
log.SetTimeFormat(time.TimeOnly)
|
log.SetTimeFormat(time.TimeOnly)
|
||||||
|
@ -62,19 +67,19 @@ var (
|
||||||
log.Errorf("unknown log level '%s' (possible values: debug, info, warn, error, fatal, default: warn)", loglvl)
|
log.Errorf("unknown log level '%s' (possible values: debug, info, warn, error, fatal, default: warn)", loglvl)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure trash directories exist
|
if !isTerminal {
|
||||||
if _, e := os.Stat(trashDir); os.IsNotExist(e) {
|
log.SetLevel(math.MaxInt32)
|
||||||
if err := os.Mkdir(trashDir, executePerm); err != nil {
|
}
|
||||||
|
|
||||||
|
// ensure personal trash directories exist
|
||||||
|
homeTrash := filepath.Join(xdg.DataHome, "Trash")
|
||||||
|
if _, e := os.Lstat(filepath.Join(homeTrash, "info")); os.IsNotExist(e) {
|
||||||
|
if err := os.MkdirAll(filepath.Join(homeTrash, "info"), executePerm); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if _, e := os.Stat(filepath.Join(trashDir, "info")); os.IsNotExist(e) {
|
if _, e := os.Lstat(filepath.Join(homeTrash, "files")); os.IsNotExist(e) {
|
||||||
if err := os.Mkdir(filepath.Join(trashDir, "info"), executePerm); err != nil {
|
if err := os.MkdirAll(filepath.Join(homeTrash, "files"), executePerm); err != nil {
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if _, e := os.Stat(filepath.Join(trashDir, "files")); os.IsNotExist(e) {
|
|
||||||
if err := os.Mkdir(filepath.Join(trashDir, "files"), executePerm); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -107,10 +112,8 @@ var (
|
||||||
mode modes.Mode
|
mode modes.Mode
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
infiles, err = files.FindTrash(trashDir, ogdir, fltr)
|
|
||||||
if err != nil {
|
infiles = files.FindInAllTrashes(ogdir, fltr)
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(infiles) <= 0 {
|
if len(infiles) <= 0 {
|
||||||
var msg string
|
var msg string
|
||||||
if fltr.Blank() {
|
if fltr.Blank() {
|
||||||
|
@ -121,6 +124,11 @@ var (
|
||||||
fmt.Fprintln(os.Stdout, msg)
|
fmt.Fprintln(os.Stdout, msg)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !isTerminal {
|
||||||
|
fmt.Fprint(os.Stdout, infiles.String())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
selected, mode, err = interactive.Select(infiles, false, false, workdir, modes.Interactive)
|
selected, mode, err = interactive.Select(infiles, false, false, workdir, modes.Interactive)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -158,7 +166,7 @@ var (
|
||||||
}
|
}
|
||||||
filesToTrash = append(filesToTrash, file)
|
filesToTrash = append(filesToTrash, file)
|
||||||
}
|
}
|
||||||
return files.ConfirmTrash(askconfirm, filesToTrash, trashDir)
|
return files.ConfirmTrash(askconfirm, filesToTrash)
|
||||||
}
|
}
|
||||||
|
|
||||||
beforeCommands = func(ctx *cli.Context) (err error) {
|
beforeCommands = func(ctx *cli.Context) (err error) {
|
||||||
|
@ -187,6 +195,7 @@ var (
|
||||||
}
|
}
|
||||||
|
|
||||||
after = func(_ *cli.Context) error {
|
after = func(_ *cli.Context) error {
|
||||||
|
files.WriteDirectorySizes()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,10 +220,7 @@ var (
|
||||||
|
|
||||||
// if none of the args were files, then find files based on filter
|
// if none of the args were files, then find files based on filter
|
||||||
if len(filesToTrash) == 0 {
|
if len(filesToTrash) == 0 {
|
||||||
fls, err := files.FindDisk(workdir, recursive, fltr)
|
fls := files.FindDisk(workdir, recursive, fltr)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(fls) == 0 {
|
if len(fls) == 0 {
|
||||||
fmt.Fprintln(os.Stdout, "no files to trash")
|
fmt.Fprintln(os.Stdout, "no files to trash")
|
||||||
return nil
|
return nil
|
||||||
|
@ -231,7 +237,7 @@ var (
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return files.ConfirmTrash(askconfirm, selected, trashDir)
|
return files.ConfirmTrash(askconfirm, selected)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -242,9 +248,7 @@ var (
|
||||||
Flags: slices.Concat(listFlags, trashedFlags, filterFlags),
|
Flags: slices.Concat(listFlags, trashedFlags, filterFlags),
|
||||||
Before: beforeCommands,
|
Before: beforeCommands,
|
||||||
Action: func(_ *cli.Context) error {
|
Action: func(_ *cli.Context) error {
|
||||||
log.Debugf("searching in directory %s for files", trashDir)
|
fls := files.FindInAllTrashes(ogdir, fltr)
|
||||||
|
|
||||||
fls, err := files.FindTrash(trashDir, ogdir, fltr)
|
|
||||||
|
|
||||||
var msg string
|
var msg string
|
||||||
log.Debugf("filter '%s' is blank? %t in %s", fltr, fltr.Blank(), ogdir)
|
log.Debugf("filter '%s' is blank? %t in %s", fltr, fltr.Blank(), ogdir)
|
||||||
|
@ -257,8 +261,11 @@ var (
|
||||||
if len(fls) == 0 {
|
if len(fls) == 0 {
|
||||||
fmt.Fprintln(os.Stdout, msg)
|
fmt.Fprintln(os.Stdout, msg)
|
||||||
return nil
|
return nil
|
||||||
} else if err != nil {
|
}
|
||||||
return err
|
|
||||||
|
if !isTerminal {
|
||||||
|
fmt.Fprint(os.Stdout, fls.String())
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return interactive.Show(fls, noInterArg, workdir)
|
return interactive.Show(fls, noInterArg, workdir)
|
||||||
|
@ -273,14 +280,10 @@ var (
|
||||||
Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags),
|
Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags),
|
||||||
Before: beforeCommands,
|
Before: beforeCommands,
|
||||||
Action: func(_ *cli.Context) error {
|
Action: func(_ *cli.Context) error {
|
||||||
log.Debugf("searching in directory %s for files", trashDir)
|
fls := files.FindInAllTrashes(ogdir, fltr)
|
||||||
|
|
||||||
fls, err := files.FindTrash(trashDir, ogdir, fltr)
|
|
||||||
if len(fls) == 0 {
|
if len(fls) == 0 {
|
||||||
fmt.Fprintln(os.Stdout, "no files to restore")
|
fmt.Fprintln(os.Stdout, "no files to restore")
|
||||||
return nil
|
return nil
|
||||||
} else if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
selected, _, err := interactive.Select(fls, all, all, workdir, modes.Restoring)
|
selected, _, err := interactive.Select(fls, all, all, workdir, modes.Restoring)
|
||||||
|
@ -304,12 +307,10 @@ var (
|
||||||
Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags),
|
Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags),
|
||||||
Before: beforeCommands,
|
Before: beforeCommands,
|
||||||
Action: func(_ *cli.Context) error {
|
Action: func(_ *cli.Context) error {
|
||||||
fls, err := files.FindTrash(trashDir, ogdir, fltr)
|
fls := files.FindInAllTrashes(ogdir, fltr)
|
||||||
if len(fls) == 0 {
|
if len(fls) == 0 {
|
||||||
fmt.Fprintln(os.Stdout, "no files to clean")
|
fmt.Fprintln(os.Stdout, "no files to clean")
|
||||||
return nil
|
return nil
|
||||||
} else if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
selected, _, err := interactive.Select(fls, all, all, workdir, modes.Cleaning)
|
selected, _, err := interactive.Select(fls, all, all, workdir, modes.Cleaning)
|
||||||
|
|
Loading…
Reference in a new issue