listing prints plainly if in non-interative terminal

This commit is contained in:
Lilian Jónsdóttir 2024-08-14 15:48:06 -07:00
parent 34eef6a228
commit 6c3abd8d98
2 changed files with 32 additions and 0 deletions

View file

@ -3,6 +3,7 @@ package files
import ( import (
"cmp" "cmp"
"fmt"
"io/fs" "io/fs"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -22,6 +23,16 @@ 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 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

21
main.go
View file

@ -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"
@ -45,8 +47,13 @@ var (
askconfirm, all bool askconfirm, all bool
workdir, ogdir cli.Path workdir, ogdir cli.Path
recursive bool recursive bool
isTerminal bool
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)
@ -60,6 +67,10 @@ 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)
} }
if !isTerminal {
log.SetLevel(math.MaxInt32)
}
// ensure personal trash directories exist // ensure personal trash directories exist
homeTrash := filepath.Join(xdg.DataHome, "Trash") homeTrash := filepath.Join(xdg.DataHome, "Trash")
if _, e := os.Lstat(filepath.Join(homeTrash, "info")); os.IsNotExist(e) { if _, e := os.Lstat(filepath.Join(homeTrash, "info")); os.IsNotExist(e) {
@ -113,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
@ -246,6 +262,11 @@ var (
return nil return nil
} }
if !isTerminal {
fmt.Fprint(os.Stdout, fls.String())
return nil
}
return interactive.Show(fls, noInterArg, workdir) return interactive.Show(fls, noInterArg, workdir)
}, },
} }