github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/shell/hintsummary/summary.go (about)

     1  package hintsummary
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/lmorg/murex/builtins/docs"
     7  	"github.com/lmorg/murex/lang"
     8  	"github.com/lmorg/murex/utils/cache"
     9  	"github.com/lmorg/murex/utils/escape"
    10  	"github.com/lmorg/murex/utils/man"
    11  	"github.com/lmorg/murex/utils/which"
    12  )
    13  
    14  var (
    15  	// Summary is an overriding summary for readline hints
    16  	Summary = New()
    17  )
    18  
    19  func Get(cmd string, checkManPage bool) (r []rune) {
    20  	var summary string
    21  	if cache.Read(cache.HINT_SUMMARY, cmd, &summary) {
    22  		return r
    23  	}
    24  
    25  	defer func() { cache.Write(cache.HINT_SUMMARY, cmd, string(r), cache.Days(30)) }()
    26  
    27  	custom := Summary.Get(cmd)
    28  	if custom != "" {
    29  		summary = custom
    30  	}
    31  
    32  	if lang.GlobalAliases.Exists(cmd) {
    33  		a := lang.GlobalAliases.Get(cmd)
    34  		alias := make([]string, len(a))
    35  		copy(alias, a)
    36  		escape.CommandLine(alias)
    37  		s := strings.Join(alias, " ")
    38  		r = []rune("(alias) " + s + " => ")
    39  		cmd = alias[0]
    40  	}
    41  
    42  	if lang.MxFunctions.Exists(cmd) {
    43  		if summary == "" {
    44  			summary, _ = lang.MxFunctions.Summary(cmd)
    45  		}
    46  
    47  		if summary == "" {
    48  			summary = "no summary written"
    49  		}
    50  		return append(r, []rune("(murex function) "+summary)...)
    51  	}
    52  
    53  	if lang.GoFunctions[cmd] != nil {
    54  		if summary == "" {
    55  			synonym := docs.Synonym[cmd]
    56  			summary = docs.Summary[synonym]
    57  		}
    58  
    59  		if summary == "" {
    60  			summary = "no doc written"
    61  		}
    62  		r = append(r, []rune("(builtin) "+summary)...)
    63  		return r
    64  	}
    65  
    66  	if checkManPage {
    67  		if summary == "" {
    68  			summary = man.ParseSummary(man.GetManPages(cmd))
    69  		}
    70  
    71  		if summary == "" {
    72  			summary = "no man page found"
    73  		}
    74  
    75  		w := readlink(which.Which(cmd))
    76  
    77  		r = append(r, []rune("("+w+") "+summary)...)
    78  		if len(r) > 0 {
    79  			return r
    80  		}
    81  	}
    82  
    83  	return nil
    84  }