MCPcopy
hub / github.com/matryer/xbar / parseOutput

Method parseOutput

pkg/plugins/parse.go:19–129  ·  view source on GitHub ↗

parseOutput parses the output of a plugin run, and returns the Items.

(ctx context.Context, filename string, r io.Reader)

Source from the content-addressed store, hash-verified

17// parseOutput parses the output of a plugin run, and returns the
18// Items.
19func (p *Plugin) parseOutput(ctx context.Context, filename string, r io.Reader) (Items, error) {
20 var (
21 items Items
22 params ItemParams
23 depthPrefix string
24 previousItem *Item
25 ancestorItems []*Item
26 captureExpanded bool
27 line int
28 text string
29 err error
30 readErr error
31 )
32 br := bufio.NewReader(r)
33 for readErr == nil { // keep reading until we hit io.EOF
34 line++
35 text, readErr = br.ReadString('\n')
36 if readErr != nil && readErr != io.EOF {
37 // some other error reading (io.EOF is fine)
38 break
39 }
40 if readErr == io.EOF && text == "" {
41 // io.EOF and no text - looks like were done.
42 break
43 }
44 if readErr != io.EOF {
45 // not io.EOF, to trim off the delimiter
46 text = text[:len(text)-1]
47 }
48 text, params, err = parseParams(text)
49 if err != nil {
50 return items, &errParsing{
51 filename: filename,
52 line: line,
53 text: text,
54 err: err,
55 }
56 }
57 if !captureExpanded && strings.TrimSpace(text) == separator {
58 // first --- means end of cycle items,
59 // start collecting expanded items now
60 captureExpanded = true
61 continue
62 }
63
64 text, isSeparator := parseSeparator(text)
65
66 for !strings.HasPrefix(text, depthPrefix) {
67 // drop a level
68 ancestorItems = ancestorItems[:len(ancestorItems)-1]
69 depthPrefix = strings.TrimPrefix(depthPrefix, nesting)
70 }
71 if strings.HasPrefix(text, depthPrefix+nesting) {
72 // if this is a separator in the submenu,
73 // then don't treat it as a another submenu.
74 if strings.TrimPrefix(text, depthPrefix) != separator {
75 // increase a level
76 ancestorItems = append(ancestorItems, previousItem)

Callers 15

TestParseErrorsFunction · 0.95
TestParseMultiplePipesFunction · 0.95
TestStartWithPipeFunction · 0.95
TestTokenTooLongFunction · 0.95
TestNestedSeparatorsFunction · 0.95
TestSubmenusFunction · 0.95
TestPipeElsewhereFunction · 0.95
TestDropdownFunction · 0.95
TestAlternateFunction · 0.95
TestTrimFunction · 0.95
TestSeparatorFunction · 0.95
TestBlankLinesFunction · 0.95

Calls 3

parseParamsFunction · 0.85
parseSeparatorFunction · 0.85
EmojizeFunction · 0.85

Tested by 15

TestParseErrorsFunction · 0.76
TestParseMultiplePipesFunction · 0.76
TestStartWithPipeFunction · 0.76
TestTokenTooLongFunction · 0.76
TestNestedSeparatorsFunction · 0.76
TestSubmenusFunction · 0.76
TestPipeElsewhereFunction · 0.76
TestDropdownFunction · 0.76
TestAlternateFunction · 0.76
TestTrimFunction · 0.76
TestSeparatorFunction · 0.76
TestBlankLinesFunction · 0.76