示例#1
0
func (p *ackPresenter) Present(
	re *regexp.Regexp,
	ctx int,
	repos map[string]*config.Repo,
	res *Response) error {

	c := ansi.NewFor(p.f)

	buf := bytes.NewBuffer(make([]byte, 0, 20))

	for repo, resp := range res.Results {
		if _, err := fmt.Fprintf(p.f, "%s\n",
			c.Fg(repoNameFor(repos, repo), ansi.Red, ansi.Bold)); err != nil {
			return err
		}

		for _, file := range resp.Matches {
			if _, err := fmt.Fprintf(p.f, "%s\n",
				c.Fg(file.Filename, ansi.Green, ansi.Bold)); err != nil {
				return err
			}

			blocks := coalesceMatches(file.Matches)

			for _, block := range blocks {
				for i, n := 0, len(block.Lines); i < n; i++ {
					line := block.Lines[i]
					hasMatch := block.Matches[i]

					if hasMatch {
						line = hiliteMatches(c, re, line)
					}

					if _, err := fmt.Fprintf(p.f, "%s%s\n",
						lineNumber(c, buf, block.Start+i, hasMatch),
						line); err != nil {
						return err
					}
				}

				if ctx > 0 {
					if _, err := fmt.Fprintln(p.f, "--"); err != nil {
						return err
					}
				}
			}

			if _, err := fmt.Fprintln(p.f); err != nil {
				return err
			}
		}
	}

	return nil
}
示例#2
0
func (p *grepPresenter) Present(
	re *regexp.Regexp,
	ctx int,
	repos map[string]*config.Repo,
	res *Response) error {

	c := ansi.NewFor(p.f)

	if _, err := fmt.Fprintf(p.f, "%s\n",
		c.Fg("// TODO(knorton): Implement", ansi.Yellow, ansi.Bold)); err != nil {
		return err
	}

	return nil
}