Example #1
0
// returns a function that will format and writes the line extracted from the logs of a given container
func write(prefix string, color *ansi.Color, timestamps bool) func(dest io.Writer, token []byte) (n int, err error) {
	return func(dest io.Writer, token []byte) (n int, err error) {
		countingWriter := countingWriter{Writer: dest}
		if color != nil {
			ansi.Output = &countingWriter
			color.Set()
		}
		_, err = countingWriter.Write([]byte(prefix))
		if err == nil {
			if !timestamps {
				// timestamps are always present in the incoming stream for
				// sorting purposes, so we strip them if the user didn't ask
				// for them
				const timestampPrefixLength = 31
				strip := timestampPrefixLength
				if string(token[0]) == "[" {
					// it seems that timestamps are wrapped in [] for events
					// streamed  in real time during a `docker logs -f`
					strip = strip + 2
				}
				token = token[strip:]
			}
			_, err = countingWriter.Write(token)
		}
		if err == nil {
			if color != nil {
				ansi.Unset()
			}
			_, err = dest.Write([]byte("\n"))
		}
		return countingWriter.written, err

	}
}
Example #2
0
// Print a thick line break (of '=' characters).
func boldLine(c *color.Color) {
	c.Set()
	defer colorUnset()
	lineBreak("=")
}
Example #3
0
func (b *Buntstift) printf(Color *color.Color, format string, a ...interface{}) (n int, err error) {
	Color.Set()
	defer b.unsetColor()
	return fmt.Fprintf(Output, format, a...)
}
Example #4
0
// Print a thin line break (of '-' characters).
func line(c *color.Color) {
	c.Set()
	defer colorUnset()
	lineBreak("-")
}