github.com/puellanivis/breton@v0.2.16/lib/display/tables/formats.go (about)

     1  package tables
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	dispwidth "github.com/puellanivis/breton/lib/display/width"
     8  )
     9  
    10  // Divider defines a set of dividers to be used when printing a single specific row.
    11  type Divider struct {
    12  	// lines are build with:
    13  	//	`{{.Left}}{{.Space}}` +
    14  	//	strings.Join(columns, `{{.Space}}{{.Bar}}{{.Space}}`) +
    15  	//	`{{.Space}}{{.Right}}`
    16  	Left  string // The left-side of the row.         e.g. "|" for ASCII
    17  	Space string // The character used for spacing.   e.g. " " for ASCII
    18  	Bar   string // The separator between two colums. e.g. "|" for ASCII
    19  	Right string // The right-side of the row.        e.g. "|" for ASCII
    20  }
    21  
    22  // Format defines a set of dividers for all types of rows.
    23  type Format struct {
    24  	Upper  *Divider // The top    e.g. ┌---+---+---┐
    25  	Inner  *Divider // Content    e.g. | x | y | z |
    26  	Middle *Divider // The middle e.g. ├---+---+---┤
    27  	Lower  *Divider // the bottom e.g. └---+---+---┘
    28  
    29  	WidthFunc func(string) int
    30  }
    31  
    32  var (
    33  	// Empty does nothing but autoscale each column, and put a single space between.
    34  	Empty = &Format{
    35  		Inner: &Divider{
    36  			Space: " ",
    37  		},
    38  		// If UTF-8 is detected per Default, then tables.Empty will use the same WidthFunc as tables.Unicode
    39  	}
    40  
    41  	// ASCII uses barebones ASCII line-drawing characters, i.e. |, +, and -
    42  	ASCII = &Format{
    43  		Upper: &Divider{
    44  			Left:  "+",
    45  			Space: "-",
    46  			Bar:   "+",
    47  			Right: "+",
    48  		},
    49  		Inner: &Divider{
    50  			Left:  "|",
    51  			Space: " ",
    52  			Bar:   "|",
    53  			Right: "|",
    54  		},
    55  		Middle: &Divider{
    56  			Left:  "+",
    57  			Space: "-",
    58  			Bar:   "+",
    59  			Right: "+",
    60  		},
    61  		Lower: &Divider{
    62  			Left:  "+",
    63  			Space: "-",
    64  			Bar:   "+",
    65  			Right: "+",
    66  		},
    67  	}
    68  
    69  	// Unicode uses Unicode line-drawing characters.
    70  	Unicode = &Format{
    71  		Upper: &Divider{
    72  			Left:  "┌",
    73  			Space: "─",
    74  			Bar:   "┬",
    75  			Right: "┐",
    76  		},
    77  		Inner: &Divider{
    78  			Left:  "│",
    79  			Space: " ",
    80  			Bar:   "│",
    81  			Right: "│",
    82  		},
    83  		Middle: &Divider{
    84  			Left:  "├",
    85  			Space: "─",
    86  			Bar:   "┼",
    87  			Right: "┤",
    88  		},
    89  		Lower: &Divider{
    90  			Left:  "└",
    91  			Space: "─",
    92  			Bar:   "┴",
    93  			Right: "┘",
    94  		},
    95  
    96  		WidthFunc: dispwidth.String,
    97  	}
    98  
    99  	// HTML outputs a _very_ simple HTML table.
   100  	HTML = &Format{
   101  		Inner: &Divider{
   102  			Left:  "<tr><td class=\"first\">",
   103  			Bar:   "</td><td>",
   104  			Right: "</td></tr>",
   105  		},
   106  		Upper: &Divider{
   107  			Left: "<table>",
   108  		},
   109  		Lower: &Divider{
   110  			Left: "</table>",
   111  		},
   112  	}
   113  )
   114  
   115  var (
   116  	// Default is the default Format to use. This starts out defaulting to
   117  	// tables.ASCII, but if environment variables LC_ALL or LANG ends
   118  	// with .UTF-8, at runtime, then it switches to tables.Unicode
   119  	// by default. (LC_ALL overrides LANG)
   120  	Default = ASCII
   121  )
   122  
   123  func checkUTF8(lang string) {
   124  	if strings.HasSuffix(lang, ".UTF-8") {
   125  		Default = Unicode
   126  		Empty.WidthFunc = Unicode.WidthFunc
   127  	}
   128  }
   129  
   130  func init() {
   131  	if lang := os.Getenv("LC_ALL"); lang != "" {
   132  		checkUTF8(lang)
   133  		return
   134  	}
   135  
   136  	checkUTF8(os.Getenv("LANG"))
   137  }