parseOutput parses the output of a plugin run, and returns the Items.
(ctx context.Context, filename string, r io.Reader)
| 17 | // parseOutput parses the output of a plugin run, and returns the |
| 18 | // Items. |
| 19 | func (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) |