Port various recent changes to CLI help output
This commit is contained in:
parent
74b768bfc2
commit
ad6d928c12
@ -57,6 +57,7 @@ func newChoicesVal(val ChoicesVal, p *ChoicesVal) *choicesVal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func add_choices(flags *pflag.FlagSet, p *ChoicesVal, choices []string, name string, short string, usage string) {
|
func add_choices(flags *pflag.FlagSet, p *ChoicesVal, choices []string, name string, short string, usage string) {
|
||||||
|
usage = strings.TrimSpace(usage) + "\n" + "Choices: " + strings.Join(choices, ", ")
|
||||||
value := ChoicesVal{Choice: choices[0], allowed: choices}
|
value := ChoicesVal{Choice: choices[0], allowed: choices}
|
||||||
flags.VarP(newChoicesVal(value, p), name, short, usage)
|
flags.VarP(newChoicesVal(value, p), name, short, usage)
|
||||||
}
|
}
|
||||||
@ -87,10 +88,14 @@ var blue_fmt = color.New(color.FgBlue).SprintFunc()
|
|||||||
var green_fmt = color.New(color.FgGreen).SprintFunc()
|
var green_fmt = color.New(color.FgGreen).SprintFunc()
|
||||||
|
|
||||||
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) {
|
||||||
if strings.TrimSpace(text) == "" {
|
trimmed := strings.TrimSpace(text)
|
||||||
|
if trimmed == "" {
|
||||||
fmt.Fprintln(output, indent)
|
fmt.Fprintln(output, indent)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if trimmed == "#placeholder_for_formatting#" {
|
||||||
|
return
|
||||||
|
}
|
||||||
x := len(indent)
|
x := len(indent)
|
||||||
fmt.Fprint(output, indent)
|
fmt.Fprint(output, indent)
|
||||||
in_escape := 0
|
in_escape := 0
|
||||||
@ -189,7 +194,27 @@ func website_url(doc string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var prettify_pat = regexp.MustCompile(":([a-z]+):`([^`]+)`")
|
var prettify_pat = regexp.MustCompile(":([a-z]+):`([^`]+)`")
|
||||||
var ref_pat = regexp.MustCompile(`\s*<\S+?>`)
|
|
||||||
|
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\\"
|
||||||
|
}
|
||||||
|
|
||||||
|
var hostname string = "*"
|
||||||
|
|
||||||
|
func CachedHostname() string {
|
||||||
|
if hostname == "*" {
|
||||||
|
h, err := os.Hostname()
|
||||||
|
if err != nil {
|
||||||
|
hostname = h
|
||||||
|
} else {
|
||||||
|
hostname = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hostname
|
||||||
|
}
|
||||||
|
|
||||||
func hyperlink_for_path(path string, text string) string {
|
func hyperlink_for_path(path string, text string) string {
|
||||||
if !stdout_is_terminal {
|
if !stdout_is_terminal {
|
||||||
@ -200,11 +225,22 @@ func hyperlink_for_path(path string, text string) string {
|
|||||||
if err == nil && fi.IsDir() {
|
if err == nil && fi.IsDir() {
|
||||||
path = strings.TrimSuffix(path, "/") + "/"
|
path = strings.TrimSuffix(path, "/") + "/"
|
||||||
}
|
}
|
||||||
host, err := os.Hostname()
|
host := CachedHostname()
|
||||||
if err != nil {
|
url := "file://" + host + path
|
||||||
host = ""
|
return hyperlink_for_url(url, text)
|
||||||
}
|
}
|
||||||
return "\x1b]8;;file://" + host + path + "\x1b\\" + text + "\x1b]8;;\x1b\\"
|
|
||||||
|
func text_and_target(x string) (text string, target string) {
|
||||||
|
parts := strings.SplitN(x, "<", 2)
|
||||||
|
text = strings.TrimSpace(parts[0])
|
||||||
|
target = strings.TrimRight(parts[len(parts)-1], ">")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func ref_hyperlink(x string, prefix string) string {
|
||||||
|
text, target := text_and_target(x)
|
||||||
|
url := "kitty+doc://" + CachedHostname() + "/#ref=" + prefix + target
|
||||||
|
return hyperlink_for_url(url, text)
|
||||||
}
|
}
|
||||||
|
|
||||||
func prettify(text string) string {
|
func prettify(text string) string {
|
||||||
@ -222,7 +258,11 @@ func prettify(text string) string {
|
|||||||
case "doc":
|
case "doc":
|
||||||
return website_url(val)
|
return website_url(val)
|
||||||
case "ref":
|
case "ref":
|
||||||
return ref_pat.ReplaceAllString(val, ``)
|
return ref_hyperlink(val, "")
|
||||||
|
case "ac":
|
||||||
|
return ref_hyperlink(val, "action-")
|
||||||
|
case "term":
|
||||||
|
return ref_hyperlink(val, "term-")
|
||||||
case "code":
|
case "code":
|
||||||
return code_fmt(val)
|
return code_fmt(val)
|
||||||
case "option":
|
case "option":
|
||||||
@ -317,11 +357,16 @@ func show_usage(cmd *cobra.Command, use_pager bool) error {
|
|||||||
fmt.Fprint(&output, ", ", opt_fmt("-"+flag.Shorthand))
|
fmt.Fprint(&output, ", ", opt_fmt("-"+flag.Shorthand))
|
||||||
}
|
}
|
||||||
defval := ""
|
defval := ""
|
||||||
|
fmt.Println(flag.Value.Type())
|
||||||
switch flag.Value.Type() {
|
switch flag.Value.Type() {
|
||||||
default:
|
default:
|
||||||
if flag.DefValue != "" {
|
if flag.DefValue != "" {
|
||||||
defval = fmt.Sprintf("[=%s]", italic_fmt(flag.DefValue))
|
defval = fmt.Sprintf("[=%s]", italic_fmt(flag.DefValue))
|
||||||
}
|
}
|
||||||
|
case "stringArray":
|
||||||
|
if flag.DefValue != "[]" {
|
||||||
|
defval = fmt.Sprintf("[=%s]", italic_fmt(flag.DefValue))
|
||||||
|
}
|
||||||
case "bool":
|
case "bool":
|
||||||
case "count":
|
case "count":
|
||||||
}
|
}
|
||||||
@ -337,9 +382,6 @@ func show_usage(cmd *cobra.Command, use_pager bool) error {
|
|||||||
msg = "Print the version of " + RootCmd.Name() + ": " + italic_fmt(RootCmd.Version)
|
msg = "Print the version of " + RootCmd.Name() + ": " + italic_fmt(RootCmd.Version)
|
||||||
}
|
}
|
||||||
format_with_indent(&output, msg, " ", screen_width)
|
format_with_indent(&output, msg, " ", screen_width)
|
||||||
if cmd.Annotations["choices-"+flag.Name] != "" {
|
|
||||||
fmt.Fprintln(&output, " Choices:", strings.Join(strings.Split(cmd.Annotations["choices-"+flag.Name], "\000"), ", "))
|
|
||||||
}
|
|
||||||
fmt.Fprintln(&output)
|
fmt.Fprintln(&output)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user