github.com/kortschak/utter@v1.5.0/config.go (about) 1 /* 2 * Copyright (c) 2013 Dave Collins <dave@davec.name> 3 * Copyright (c) 2015 Dan Kortschak <dan.kortschak@adelaide.edu.au> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 package utter 19 20 import ( 21 "bytes" 22 "io" 23 "os" 24 ) 25 26 // ConfigState houses the configuration options used by utter to format and 27 // display values. There is a global instance, Config, that is used to control 28 // all top-level Formatter and Dump functionality. Each ConfigState instance 29 // provides methods equivalent to the top-level functions. 30 // 31 // The zero value for ConfigState provides no indentation. You would typically 32 // want to set it to a space or a tab. 33 // 34 // Alternatively, you can use NewDefaultConfig to get a ConfigState instance 35 // with default settings. See the documentation of NewDefaultConfig for default 36 // values. 37 type ConfigState struct { 38 // Indent specifies the string to use for each indentation level. The 39 // global config instance that all top-level functions use set this to a 40 // single space by default. If you would like more indentation, you might 41 // set this to a tab with "\t" or perhaps two spaces with " ". 42 Indent string 43 44 // NumericWidth specifies the number of columns to use when dumping 45 // a numeric slice or array (including bool). Zero specifies all entries 46 // on one line. 47 NumericWidth int 48 49 // StringWidth specifies the number of columns to use when dumping 50 // a string slice or array. Zero specifies all entries on one line. 51 StringWidth int 52 53 // Quoting specifies the quoting strategy to use when printing strings. 54 Quoting Quoting 55 56 // BytesWidth specifies the number of byte columns to use when dumping a 57 // byte slice or array. 58 BytesWidth int 59 60 // CommentBytes specifies whether byte slice or array dumps have ASCII 61 // comment annotations. 62 CommentBytes bool 63 64 // CommentPointers specifies whether pointer information will be added 65 // as comments. 66 CommentPointers bool 67 68 // IgnoreUnexported specifies that unexported struct fields should be 69 // ignored during a dump. 70 IgnoreUnexported bool 71 72 // OmitZero specifies that zero values should not be printed in a dump. 73 OmitZero bool 74 75 // LocalPackage specifies a package selector to trim from type information. 76 LocalPackage string 77 78 // ElideType specifies that type information defined by context should 79 // not be printed in a dump. 80 ElideType bool 81 82 // SortKeys specifies map keys should be sorted before being printed. Use 83 // this to have a more deterministic, diffable output. Note that only 84 // native types (bool, int, uint, floats, uintptr and string) are supported 85 // with other types sorted according to the reflect.Value.String() output 86 // which guarantees display stability. 87 SortKeys bool 88 } 89 90 // Quoting describes string quoting strategies. 91 // 92 // The numerical values of quoting constants are not guaranteed to be stable. 93 type Quoting uint 94 95 const ( 96 // Quote strings with double quotes. 97 DoubleQuote Quoting = 0 98 99 // AvoidEscapes quotes strings using backquotes to avoid escape 100 // sequences if possible, otherwise double quotes are used. 101 AvoidEscapes Quoting = 1 << iota 102 103 // Backquote always quotes strings using backquotes where possible 104 // within the string. For sections of strings that can not be 105 // backquoted, additional double quote syntax is used. 106 Backquote 107 108 // Force is a modifier of AvoidEscapes that adds additional double 109 // quote syntax to represent parts that cannot be backquoted. 110 Force 111 ) 112 113 // Config is the active configuration of the top-level functions. 114 // The configuration can be changed by modifying the contents of utter.Config. 115 var Config = ConfigState{ 116 Indent: " ", 117 NumericWidth: 1, 118 StringWidth: 1, 119 BytesWidth: 16, 120 CommentBytes: true, 121 } 122 123 // Fdump formats and displays the passed arguments to io.Writer w. It formats 124 // exactly the same as Dump. 125 func (c *ConfigState) Fdump(w io.Writer, a interface{}) { 126 fdump(c, w, a) 127 } 128 129 /* 130 Dump displays the passed parameters to standard out with newlines, customizable 131 indentation, and additional debug information such as complete types and all 132 pointer addresses used to indirect to the final value. It provides the 133 following features over the built-in printing facilities provided by the fmt 134 package: 135 136 * Pointers are dereferenced and followed 137 * Circular data structures are detected and handled properly 138 * Byte arrays and slices are dumped in a way similar to the hexdump -C command 139 which includes byte values in hex, and ASCII output 140 141 The configuration options are controlled by modifying the public members 142 of c. See ConfigState for options documentation. 143 144 See Fdump if you would prefer dumping to an arbitrary io.Writer or Sdump to 145 get the formatted result as a string. 146 */ 147 func (c *ConfigState) Dump(a interface{}) { 148 fdump(c, os.Stdout, a) 149 } 150 151 // Sdump returns a string with the passed arguments formatted exactly the same 152 // as Dump. 153 func (c *ConfigState) Sdump(a interface{}) string { 154 var buf bytes.Buffer 155 fdump(c, &buf, a) 156 return buf.String() 157 } 158 159 // NewDefaultConfig returns a ConfigState with the following default settings. 160 // 161 // Indent: " " 162 // NumericWidth: 1, 163 // StringWidth: 1, 164 // BytesWidth: 16 165 // CommentBytes: true 166 // CommentPointers: false 167 // IgnoreUnexported: false 168 // ElideType: false 169 // SortKeys: false 170 func NewDefaultConfig() *ConfigState { 171 return &ConfigState{ 172 Indent: " ", 173 NumericWidth: 1, 174 StringWidth: 1, 175 BytesWidth: 16, 176 CommentBytes: true, 177 } 178 }