mirror of https://github.com/cheat/cheat.git
chore: DRY out colorization code
Creates a `sheet.Colorize` method that DRYs out code that was duplicated among `cmd_search` and `cmd_view`.
This commit is contained in:
parent
bc623da74b
commit
87cba04ff2
|
@ -1,13 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alecthomas/chroma/quick"
|
|
||||||
"github.com/cheat/cheat/internal/config"
|
"github.com/cheat/cheat/internal/config"
|
||||||
"github.com/cheat/cheat/internal/sheet"
|
"github.com/cheat/cheat/internal/sheet"
|
||||||
"github.com/cheat/cheat/internal/sheets"
|
"github.com/cheat/cheat/internal/sheets"
|
||||||
|
@ -85,23 +83,7 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) {
|
||||||
|
|
||||||
// if colorization was requested, apply it here
|
// if colorization was requested, apply it here
|
||||||
if conf.Color(opts) {
|
if conf.Color(opts) {
|
||||||
|
sheet.Colorize(conf)
|
||||||
// if the syntax was not specified, default to bash
|
|
||||||
lex := sheet.Syntax
|
|
||||||
if lex == "" {
|
|
||||||
lex = "bash"
|
|
||||||
}
|
|
||||||
|
|
||||||
var buf bytes.Buffer
|
|
||||||
err = quick.Highlight(
|
|
||||||
&buf,
|
|
||||||
sheet.Text,
|
|
||||||
lex,
|
|
||||||
conf.Formatter,
|
|
||||||
conf.Style,
|
|
||||||
)
|
|
||||||
|
|
||||||
sheet.Text = buf.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// output the cheatsheet title
|
// output the cheatsheet title
|
||||||
|
|
|
@ -5,8 +5,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alecthomas/chroma/quick"
|
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/config"
|
"github.com/cheat/cheat/internal/config"
|
||||||
"github.com/cheat/cheat/internal/sheets"
|
"github.com/cheat/cheat/internal/sheets"
|
||||||
)
|
)
|
||||||
|
@ -43,29 +41,11 @@ func cmdView(opts map[string]interface{}, conf config.Config) {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !conf.Color(opts) {
|
// apply colorization if requested
|
||||||
|
if conf.Color(opts) {
|
||||||
|
sheet.Colorize(conf)
|
||||||
|
}
|
||||||
|
|
||||||
|
// display the cheatsheet
|
||||||
fmt.Print(sheet.Text)
|
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
package sheet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
|
||||||
|
"github.com/cheat/cheat/internal/config"
|
||||||
|
|
||||||
|
"github.com/alecthomas/chroma/quick"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Colorize applies syntax-highlighting to a cheatsheet's Text.
|
||||||
|
func (s *Sheet) Colorize(conf config.Config) {
|
||||||
|
|
||||||
|
// if the syntax was not specified, default to bash
|
||||||
|
lex := s.Syntax
|
||||||
|
if lex == "" {
|
||||||
|
lex = "bash"
|
||||||
|
}
|
||||||
|
|
||||||
|
// write colorized text into a buffer
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := quick.Highlight(
|
||||||
|
&buf,
|
||||||
|
s.Text,
|
||||||
|
lex,
|
||||||
|
conf.Formatter,
|
||||||
|
conf.Style,
|
||||||
|
)
|
||||||
|
|
||||||
|
// if colorization somehow failed, do nothing
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise, swap the cheatsheet's Text with its colorized equivalent
|
||||||
|
s.Text = buf.String()
|
||||||
|
}
|
Loading…
Reference in New Issue