mirror of
https://github.com/cheat/cheat.git
synced 2024-09-29 23:11:31 +02:00
3afea0972c
- Corrects an error with `--search`. Previously, `--search` was not aware of whether it was outputted to a TTY, and would apply colorization at all times. This resulted in unwanted behavior when, for example, piping search results into a paginator. - Corrects an error with `--color`. Previously, `--color` would be ignored if output was being written to a non-TTY. This made it impossible, for example, to `cheat tar --color | less -R`, as colorization would always be stripped. The behavior of `--color` has been modified such that it now behaves similarly to `--color=always` in other applications.
72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/alecthomas/chroma/quick"
|
|
|
|
"github.com/cheat/cheat/internal/config"
|
|
"github.com/cheat/cheat/internal/sheets"
|
|
)
|
|
|
|
// cmdView displays a cheatsheet for viewing.
|
|
func cmdView(opts map[string]interface{}, conf config.Config) {
|
|
|
|
cheatsheet := opts["<cheatsheet>"].(string)
|
|
|
|
// load the cheatsheets
|
|
cheatsheets, err := sheets.Load(conf.Cheatpaths)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, fmt.Sprintf("failed to list cheatsheets: %v", err))
|
|
os.Exit(1)
|
|
}
|
|
|
|
// filter cheatcheats by tag if --tag was provided
|
|
if opts["--tag"] != nil {
|
|
cheatsheets = sheets.Filter(
|
|
cheatsheets,
|
|
strings.Split(opts["--tag"].(string), ","),
|
|
)
|
|
}
|
|
|
|
// consolidate the cheatsheets found on all paths into a single map of
|
|
// `title` => `sheet` (ie, allow more local cheatsheets to override less
|
|
// local cheatsheets)
|
|
consolidated := sheets.Consolidate(cheatsheets)
|
|
|
|
// fail early if the requested cheatsheet does not exist
|
|
sheet, ok := consolidated[cheatsheet]
|
|
if !ok {
|
|
fmt.Printf("No cheatsheet found for '%s'.\n", cheatsheet)
|
|
os.Exit(0)
|
|
}
|
|
|
|
if !conf.Color(opts) {
|
|
fmt.Print(sheet.Text)
|
|
os.Exit(0)
|
|
}
|
|
|
|
// otherwise, colorize the output
|
|
// if the syntax was not specified, default to bash
|
|
lex := sheet.Syntax
|
|
if lex == "" {
|
|
lex = "bash"
|
|
}
|
|
|
|
// apply syntax highlighting
|
|
err = quick.Highlight(
|
|
os.Stdout,
|
|
sheet.Text,
|
|
lex,
|
|
conf.Formatter,
|
|
conf.Style,
|
|
)
|
|
|
|
// if colorization somehow failed, output non-colorized text
|
|
if err != nil {
|
|
fmt.Print(sheet.Text)
|
|
}
|
|
}
|