github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/pkg/terminal/docgen.go (about)

     1  package terminal
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strings"
     7  )
     8  
     9  func replaceDocPath(s string) string {
    10  	const docpath = "Documentation/"
    11  
    12  	i0 := 0
    13  
    14  	for {
    15  		start := strings.Index(s[i0:], docpath)
    16  		if start < 0 {
    17  			return s
    18  		}
    19  		start += i0
    20  		if start-1 >= 0 && s[start-1] != ' ' {
    21  			i0 = start + len(docpath) + 1
    22  			continue
    23  		}
    24  		var end int
    25  		for end = start + len(docpath); end < len(s); end++ {
    26  			if s[end] == ' ' {
    27  				break
    28  			}
    29  		}
    30  		// If we captured a trailing dot, backtrack.
    31  		if s[end-1] == '.' {
    32  			end--
    33  		}
    34  
    35  		text := s[start:end]
    36  		s = s[:start] + fmt.Sprintf("[%s](//github.com/go-delve/delve/tree/master/%s)", text, text) + s[end:]
    37  		i0 = end + 1
    38  	}
    39  }
    40  
    41  func (c *Commands) WriteMarkdown(w io.Writer) {
    42  	fmt.Fprint(w, "# Configuration and Command History\n\n")
    43  	fmt.Fprint(w, "If `$XDG_CONFIG_HOME` is set, then configuration and command history files are located in `$XDG_CONFIG_HOME/dlv`. ")
    44  	fmt.Fprint(w, "Otherwise, they are located in `$HOME/.config/dlv` on Linux and `$HOME/.dlv` on other systems.\n\n")
    45  	fmt.Fprint(w, "The configuration file `config.yml` contains all the configurable options and their default values. ")
    46  	fmt.Fprint(w, "The command history is stored in `.dbg_history`.\n\n")
    47  
    48  	fmt.Fprint(w, "# Commands\n")
    49  
    50  	for _, cgd := range commandGroupDescriptions {
    51  		fmt.Fprintf(w, "\n## %s\n\n", cgd.description)
    52  
    53  		fmt.Fprint(w, "Command | Description\n")
    54  		fmt.Fprint(w, "--------|------------\n")
    55  		for _, cmd := range c.cmds {
    56  			if cmd.group != cgd.group {
    57  				continue
    58  			}
    59  			h := cmd.helpMsg
    60  			if idx := strings.Index(h, "\n"); idx >= 0 {
    61  				h = h[:idx]
    62  			}
    63  			fmt.Fprintf(w, "[%s](#%s) | %s\n", cmd.aliases[0], cmd.aliases[0], h)
    64  		}
    65  		fmt.Fprint(w, "\n")
    66  
    67  	}
    68  
    69  	for _, cmd := range c.cmds {
    70  		fmt.Fprintf(w, "## %s\n%s\n\n", cmd.aliases[0], replaceDocPath(cmd.helpMsg))
    71  		if len(cmd.aliases) > 1 {
    72  			fmt.Fprint(w, "Aliases:")
    73  			for _, alias := range cmd.aliases[1:] {
    74  				fmt.Fprintf(w, " %s", alias)
    75  			}
    76  			fmt.Fprint(w, "\n")
    77  		}
    78  		fmt.Fprint(w, "\n")
    79  	}
    80  }