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 (
"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

21
main.go
View file

@ -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)
},
}