From 6c3abd8d98525a1a2a1085be8573c0a579ab5430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lilian=20J=C3=B3nsd=C3=B3ttir?= Date: Wed, 14 Aug 2024 15:48:06 -0700 Subject: [PATCH] listing prints plainly if in non-interative terminal --- internal/files/files.go | 11 +++++++++++ main.go | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+) 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) }, }