Skip to content

Commit

Permalink
Add --bat, -b, --list, -t for listing files with syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Nov 6, 2024
1 parent 9289355 commit 56af69f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
4 changes: 3 additions & 1 deletion v2/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ Flags:
The default filename is input.txt. Handy for AoC.
-o, --ollama Use Ollama and the ` + codeCompletionModel + ` model
for tab completion (experimental feature).
-b, --bat Cat the file with bat (useful in connection with -c and -p).
-b, --bat List the given file using bat, if it exists in the PATH.
This can be useful in connection with -c or -p.
-t, --list List the given file using the red/black theme.
-v, --version Display the current version.
See the man page for more information.
Expand Down
32 changes: 19 additions & 13 deletions v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func main() {
nanoMode bool
ollamaTabCompletion bool
batFlag bool
catFlag bool
)

pflag.BoolVarP(&copyFlag, "copy", "c", false, "copy a file into the clipboard and quit")
Expand All @@ -87,7 +88,8 @@ func main() {
pflag.StringVarP(&inputFileWhenRunning, "input-file", "i", "input.txt", "input file when building and running programs")
pflag.BoolVarP(&nanoMode, "nano", "a", false, "Nano/Pico mode")
pflag.BoolVarP(&ollamaTabCompletion, "ollama", "o", false, "use Ollama for tab completion")
pflag.BoolVarP(&batFlag, "bat", "b", false, "Cat the file with colors instead of editing it")
pflag.BoolVarP(&batFlag, "bat", "b", false, "Cat the file with colors instead of editing it, using bat")
pflag.BoolVarP(&catFlag, "list", "t", false, "List the file with colors instead of editing it")

pflag.Parse()

Expand Down Expand Up @@ -183,10 +185,11 @@ func main() {
fmt.Printf("Wrote %d bytes to %s from the clipboard.\n", n, filename)
}
if batFlag {
// Run bat and quit
if err := quitBat(filename); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
// List the file in a colorful way, using bat, and quit
quitBat(filename)
} else if catFlag {
// List the file in a colorful way and quit
quitCat(&FilenameOrData{filename, []byte{}, 0, false})
}

return
Expand Down Expand Up @@ -220,11 +223,13 @@ func main() {
fmt.Printf("Copied %d byte%s from %s to the clipboard.\n", n, plural, filename)
}
if batFlag {
// Run bat and quit
if err := quitBat(filename); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
// List the file in a colorful way, using bat, and quit
quitBat(filename)
} else if catFlag {
// List the file in a colorful way and quit
quitCat(&FilenameOrData{filename, []byte{}, 0, false})
}

return
}

Expand Down Expand Up @@ -421,10 +426,11 @@ func main() {
}

if batFlag { // This should NOT happen if only ORBITON_BAT is set!
// Run bat and quit
if err := quitBat(fnord.filename); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
// List the file in a colorful way, using bat, and quit
quitBat(fnord.filename)
} else if catFlag {
// List the file in a colorful way and quit
quitCat(&fnord)
}

// Initialize the VT100 terminal
Expand Down
27 changes: 24 additions & 3 deletions v2/quit.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/xyproto/env/v2"
"github.com/xyproto/files"
"github.com/xyproto/syntax"
"github.com/xyproto/textoutput"
"github.com/xyproto/vt100"
)
Expand Down Expand Up @@ -84,12 +85,31 @@ func quitExecShellCommand(tty *vt100.TTY, workDir string, shellCommand string) {
syscall.Exec(shellExecutable, []string{shellExecutable, "-c", shellCommand}, env.Environ())
}

func quitBat(filename string) error {
// quitCat tries to list the given source code file using syntax.CatBytes, and then exits
func quitCat(fnord *FilenameOrData) {
quitMut.Lock()
defer quitMut.Unlock()
if fnord.Empty() {
if sourceCodeBytes, err := os.ReadFile(fnord.filename); err == nil { // success
if err := syntax.CatBytes(sourceCodeBytes, tout); err == nil { // success
vt100.ShowCursor(true)
os.Exit(0)
}
}
} else {
if err := syntax.CatBytes(fnord.data, tout); err == nil { // success
vt100.ShowCursor(true)
os.Exit(0)
}
}
vt100.ShowCursor(true)
workDir := filepath.Dir(filename)
_ = os.Chdir(workDir)
os.Exit(1) // could not cat the file in a syntax highlighted way
}

// quitBat tries to list the given source code file using "bat", if "bat" exists in the path, and then exits
func quitBat(filename string) error {
quitMut.Lock()
defer quitMut.Unlock()
batCommandLine := env.Str("ORBITON_BAT", "bat")
batExecutable := batCommandLine
args := []string{batExecutable}
Expand All @@ -110,6 +130,7 @@ func quitBat(filename string) error {
return fmt.Errorf("%q is not available in the PATH", batExecutable)
}
args = append(args, filename)
vt100.ShowCursor(true)
syscall.Exec(batExecutable, args, env.Environ())
return nil // this is never reached
}
Expand Down

0 comments on commit 56af69f

Please sign in to comment.