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