diff --git a/internal/files/files.go b/internal/files/files.go index 46b2f4b..16b96ac 100644 --- a/internal/files/files.go +++ b/internal/files/files.go @@ -3,6 +3,7 @@ package files import ( "cmp" + "fmt" "io/fs" "path/filepath" "strconv" @@ -22,6 +23,16 @@ type File interface { 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 SortByModified(a, b File) int { if a.Date().Before(b.Date()) { return 1 diff --git a/main.go b/main.go index e3201d5..3556198 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ package main import ( "fmt" "io/fs" + "math" "os" "path/filepath" "slices" @@ -14,6 +15,7 @@ import ( "git.burning.moe/celediel/gt/internal/filter" "git.burning.moe/celediel/gt/internal/interactive" "git.burning.moe/celediel/gt/internal/interactive/modes" + "golang.org/x/term" "github.com/adrg/xdg" "github.com/charmbracelet/log" @@ -45,8 +47,13 @@ var ( askconfirm, all bool workdir, ogdir cli.Path recursive bool + isTerminal bool beforeAll = func(_ *cli.Context) error { + if term.IsTerminal(int(os.Stdout.Fd())) && term.IsTerminal(int(os.Stdin.Fd())) { + isTerminal = true + } + // setup log log.SetReportTimestamp(true) log.SetTimeFormat(time.TimeOnly) @@ -60,6 +67,10 @@ var ( log.Errorf("unknown log level '%s' (possible values: debug, info, warn, error, fatal, default: warn)", loglvl) } + if !isTerminal { + log.SetLevel(math.MaxInt32) + } + // ensure personal trash directories exist homeTrash := filepath.Join(xdg.DataHome, "Trash") if _, e := os.Lstat(filepath.Join(homeTrash, "info")); os.IsNotExist(e) { @@ -113,6 +124,11 @@ var ( fmt.Fprintln(os.Stdout, msg) return nil } + + if !isTerminal { + fmt.Fprint(os.Stdout, infiles.String()) + return nil + } selected, mode, err = interactive.Select(infiles, false, false, workdir, modes.Interactive) if err != nil { return err @@ -246,6 +262,11 @@ var ( return nil } + if !isTerminal { + fmt.Fprint(os.Stdout, fls.String()) + return nil + } + return interactive.Show(fls, noInterArg, workdir) }, }