github.com/bookerzzz/grok@v0.0.0/dump.go (about)

     1  package grok
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"strings"
     8  )
     9  
    10  var (
    11  	colourReset  = "\x1B[0m"
    12  	colourRed    = "\x1B[38;5;124m"
    13  	colourYellow = "\x1B[38;5;208m"
    14  	colourBlue   = "\x1B[38;5;33m"
    15  	colourGrey   = "\x1B[38;5;144m"
    16  	colourGreen  = "\x1B[38;5;34m"
    17  	colourGold   = "\x1B[38;5;3m"
    18  	defaults = Conf{
    19  		depth:   0,
    20  		out:     os.Stdout,
    21  		tabstop: 4,
    22  		colour: true,
    23  		maxDepth: 10,
    24  		maxLength: 100,
    25  	}
    26  )
    27  
    28  type Option func(c *Conf)
    29  
    30  type Conf struct {
    31  	depth   int
    32  	out     io.Writer
    33  	tabstop int
    34  	colour bool
    35  	maxDepth int
    36  	maxLength int
    37  }
    38  
    39  
    40  func outer(c Conf) func(string, ...interface{}) {
    41  	return func(format string, params ...interface{}) {
    42  		c.out.Write([]byte(fmt.Sprintf(format, params...)))
    43  	}
    44  }
    45  
    46  func indent(c Conf) string {
    47  	return strings.Repeat(" ", c.depth*c.tabstop)
    48  }
    49  
    50  func colourer(c Conf) func(str string, colour string) string {
    51  	return func (str string, colour string) string {
    52  		if c.colour {
    53  			return colour + str + colourReset
    54  		}
    55  		return str
    56  	}
    57  }
    58  
    59  // WithWriter redirects output from debug functions to the given io.Writer
    60  func WithWriter(w io.Writer) Option {
    61  	return func(c *Conf) {
    62  		c.out = w
    63  	}
    64  }
    65  
    66  // WithoutColours disables colouring of output from debug functions. Defaults to `true`
    67  func WithoutColours() Option {
    68  	return func(c *Conf) {
    69  		c.colour = false
    70  	}
    71  }
    72  
    73  // WithMaxDepth sets the maximum recursion depth from debug functions. Defaults to `10`, use `0` for unlimited
    74  func WithMaxDepth(depth int) Option {
    75  	return func(c *Conf) {
    76  		c.maxDepth = depth
    77  	}
    78  }
    79  
    80  // WithMaxLength sets the maximum length of string values. Default is `100`, use `0` for unlimited
    81  func WithMaxLength(chars int) Option {
    82  	return func(c *Conf) {
    83  		c.maxLength = chars
    84  	}
    85  }
    86  
    87  // WithTabStop sets the width of a tabstop to the given char count. Defaults to `4`
    88  func WithTabStop(chars int) Option {
    89  	return func(c *Conf) {
    90  		c.tabstop = chars
    91  	}
    92  }