Use the new styling API for CLI infra
This commit is contained in:
parent
4a4500d56b
commit
249df69ac9
@ -19,6 +19,7 @@ import (
|
||||
"kitty"
|
||||
"kitty/tools/tty"
|
||||
"kitty/tools/utils"
|
||||
"kitty/tools/utils/style"
|
||||
"kitty/tools/wcswidth"
|
||||
)
|
||||
|
||||
@ -70,40 +71,22 @@ func ChoicesP(flags *pflag.FlagSet, name string, short string, usage string, cho
|
||||
|
||||
var stdout_is_terminal = false
|
||||
|
||||
func surrounder(start int, end int) func(string) string {
|
||||
return func(text string) string {
|
||||
if stdout_is_terminal {
|
||||
return fmt.Sprintf("\033[%dm%s\033[%dm", start, text, end)
|
||||
}
|
||||
return text
|
||||
}
|
||||
}
|
||||
|
||||
func compose_surrounders(surrounders ...func(string) string) func(string) string {
|
||||
return func(text string) string {
|
||||
if stdout_is_terminal {
|
||||
for _, surrounder := range surrounders {
|
||||
text = surrounder(text)
|
||||
}
|
||||
}
|
||||
return text
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
emph_fmt = surrounder(91, 39)
|
||||
cyan_fmt = surrounder(96, 39)
|
||||
green_fmt = surrounder(32, 39)
|
||||
blue_fmt = surrounder(34, 39)
|
||||
bright_red_fmt = surrounder(91, 39)
|
||||
yellow_fmt = surrounder(93, 39)
|
||||
italic_fmt = surrounder(3, 23)
|
||||
bold_fmt = surrounder(1, 22)
|
||||
title_fmt = compose_surrounders(blue_fmt, bold_fmt)
|
||||
exe_fmt = compose_surrounders(yellow_fmt, bold_fmt)
|
||||
fmt_ctx = style.Context{}
|
||||
cyan_fmt = fmt_ctx.SprintFunc("fg=bright-cyan")
|
||||
green_fmt = fmt_ctx.SprintFunc("fg=green")
|
||||
blue_fmt = fmt_ctx.SprintFunc("fg=blue")
|
||||
bright_red_fmt = fmt_ctx.SprintFunc("fg=bright-red")
|
||||
yellow_fmt = fmt_ctx.SprintFunc("fg=bright-yellow")
|
||||
italic_fmt = fmt_ctx.SprintFunc("italic")
|
||||
bold_fmt = fmt_ctx.SprintFunc("bold")
|
||||
title_fmt = fmt_ctx.SprintFunc("bold fg=blue")
|
||||
exe_fmt = fmt_ctx.SprintFunc("bold fg=bright-yellow")
|
||||
opt_fmt = green_fmt
|
||||
err_fmt = compose_surrounders(bright_red_fmt, bold_fmt)
|
||||
emph_fmt = bright_red_fmt
|
||||
err_fmt = fmt_ctx.SprintFunc("bold fg=bright-red")
|
||||
code_fmt = cyan_fmt
|
||||
url_fmt = fmt_ctx.UrlFunc("u=curly uc=cyan")
|
||||
)
|
||||
|
||||
func format_line_with_indent(output io.Writer, text string, indent string, screen_width int) {
|
||||
@ -215,10 +198,7 @@ func website_url(doc string) string {
|
||||
var prettify_pat = regexp.MustCompile(":([a-z]+):`([^`]+)`")
|
||||
|
||||
func hyperlink_for_url(url string, text string) string {
|
||||
if !stdout_is_terminal {
|
||||
return text
|
||||
}
|
||||
return "\x1b]8;;" + url + "\x1b\\\x1b[4:3;58:5:4m" + text + "\x1b[4:0;59m\x1b]8;;\x1b\\"
|
||||
return url_fmt(url, text)
|
||||
}
|
||||
|
||||
var hostname string = "*"
|
||||
@ -236,7 +216,7 @@ func CachedHostname() string {
|
||||
}
|
||||
|
||||
func hyperlink_for_path(path string, text string) string {
|
||||
if !stdout_is_terminal {
|
||||
if !fmt_ctx.AllowEscapeCodes {
|
||||
return text
|
||||
}
|
||||
path = strings.ReplaceAll(utils.Abspath(path), string(os.PathSeparator), "/")
|
||||
@ -504,6 +484,7 @@ func Init(root *cobra.Command) {
|
||||
vs = vs + " (" + kitty.VCSRevision + ")"
|
||||
}
|
||||
stdout_is_terminal = tty.IsTerminal(os.Stdout.Fd())
|
||||
fmt_ctx.AllowEscapeCodes = stdout_is_terminal
|
||||
RootCmd = root
|
||||
root.Version = vs
|
||||
root.SetUsageFunc(func(cmd *cobra.Command) error { return show_usage(cmd, false) })
|
||||
|
||||
@ -33,7 +33,7 @@ func (self *Context) UrlFunc(spec string) func(string, string) string {
|
||||
p := prefix_for_spec(spec)
|
||||
s := suffix_for_spec(spec)
|
||||
|
||||
return func(url string, text string) string {
|
||||
return func(url, text string) string {
|
||||
if !self.AllowEscapeCodes {
|
||||
return text
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user