Skip to content

Instantly share code, notes, and snippets.

@gmlewis
Created August 12, 2018 21:57
Show Gist options
  • Save gmlewis/ae8ea80a2e99811a9daca8b2a9ed9d87 to your computer and use it in GitHub Desktop.
Save gmlewis/ae8ea80a2e99811a9daca8b2a9ed9d87 to your computer and use it in GitHub Desktop.
filter-pub-test filters "pub run test" output to make it Emacs-friendly
// -*- compile-command: "go build filter-pub-test.go"; -*-
// filter-pub-test filters out the ANSI color codes from "pub run test" output.
// It also changes the output so that Emacs understands what file, line, and column
// numbers had the error so it can jump to the next error directly with "C-x `".
package main
import (
"bufio"
"bytes"
"log"
"os"
"regexp"
"strings"
)
var (
toRemove = []string{
"\x1b[0m",
"\x1b[1;30m",
"\x1b[1m",
"\x1b[31m",
"\x1b[32m",
}
crRE = regexp.MustCompile(`\s+\r`)
timeRE = regexp.MustCompile(`^\d\d:\d\d `)
fileLineColRE = regexp.MustCompile(`^\s+\S+\.dart \d+:\d+ `)
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(ScanLines)
var lines int
for scanner.Scan() {
lines++
buf := scanner.Bytes()
s := crRE.ReplaceAllString(string(buf), "\r")
for _, r := range toRemove {
s = strings.Replace(s, r, "", -1)
}
s = timeRE.ReplaceAllStringFunc(s, func(v string) string { return strings.Replace(v, ":", ";", -1) })
s = fileLineColRE.ReplaceAllStringFunc(s, func(v string) string {
v = strings.TrimLeft(v, " ")
return strings.Replace(v, ".dart ", ".dart:", -1)
})
os.Stdout.WriteString(s)
}
if err := scanner.Err(); err != nil {
log.Fatalf("scanner: %v", err)
}
}
func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexByte(data, '\r'); i >= 0 {
// We have a full line terminated by a carriage return.
return i + 1, data[0 : i+1], nil
}
if i := bytes.IndexByte(data, '\n'); i >= 0 {
// We have a full newline-terminated line.
return i + 1, data[0 : i+1], nil
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment