-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (55 loc) · 1.65 KB
/
Copy pathmain.go
File metadata and controls
66 lines (55 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Command lsgo lists directory contents, in the same spirit as ls --
// colourised by file type, aware of symlinks and Git status, and able to
// show its output as a grid, a single column, a detailed table, or a
// tree.
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"github.com/jc21/lsgo/internal/app"
"github.com/jc21/lsgo/internal/cli"
"github.com/jc21/lsgo/internal/termwidth"
)
var Version = "0.0.0" // overridden at build time
func main() {
os.Exit(run(os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
}
func run(argv []string, stdin io.Reader, stdout, stderr *os.File) int {
isTerminal := termwidth.IsTerminal(stdout.Fd())
probe := func() (int, bool) { return termwidth.Width(stdout.Fd()) }
result, err := cli.ParseArgs(argv, cli.OSEnv, probe)
if err != nil {
fmt.Fprintf(stderr, "lsgo: %s\n", err)
return app.ExitOptionsErr
}
if result.ShowHelp {
fmt.Fprint(stdout, cli.HelpText)
return app.ExitSuccess
}
if result.ShowVersion {
fmt.Fprintf(stdout, "lsgo %s\n", Version)
return app.ExitSuccess
}
if result.Options.ReadStdin {
result.Options.Paths = append(result.Options.Paths, readStdinPaths(stdin)...)
}
lsColors, _ := os.LookupEnv("LS_COLORS")
a := app.New(result.Options, isTerminal, lsColors, stdout, stderr)
return a.Run()
}
// readStdinPaths reads newline-separated paths from r (--stdin), trimming
// any trailing '\r' so files piped from Windows-style tools still work.
func readStdinPaths(r io.Reader) []string {
var paths []string
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := strings.TrimSuffix(scanner.Text(), "\r")
if line != "" {
paths = append(paths, line)
}
}
return paths
}