Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 1 | // Copyright 2014 Oleku Konko All rights reserved. |
| 2 | // Use of this source code is governed by a MIT |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // This module is a Table Writer API for the Go Programming Language. |
| 6 | // The protocols were written in pure Go and works on windows and unix systems |
| 7 | |
| 8 | package tablewriter |
| 9 | |
| 10 | import ( |
| 11 | "math" |
| 12 | "regexp" |
| 13 | "strings" |
| 14 | |
| 15 | "github.com/mattn/go-runewidth" |
| 16 | ) |
| 17 | |
| 18 | var ansi = regexp.MustCompile("\033\\[(?:[0-9]{1,3}(?:;[0-9]{1,3})*)?[m|K]") |
| 19 | |
| 20 | func DisplayWidth(str string) int { |
| 21 | return runewidth.StringWidth(ansi.ReplaceAllLiteralString(str, "")) |
| 22 | } |
| 23 | |
| 24 | // Simple Condition for string |
| 25 | // Returns value based on condition |
| 26 | func ConditionString(cond bool, valid, inValid string) string { |
| 27 | if cond { |
| 28 | return valid |
| 29 | } |
| 30 | return inValid |
| 31 | } |
| 32 | |
| 33 | func isNumOrSpace(r rune) bool { |
| 34 | return ('0' <= r && r <= '9') || r == ' ' |
| 35 | } |
| 36 | |
| 37 | // Format Table Header |
| 38 | // Replace _ , . and spaces |
| 39 | func Title(name string) string { |
| 40 | origLen := len(name) |
| 41 | rs := []rune(name) |
| 42 | for i, r := range rs { |
| 43 | switch r { |
| 44 | case '_': |
| 45 | rs[i] = ' ' |
| 46 | case '.': |
| 47 | // ignore floating number 0.0 |
| 48 | if (i != 0 && !isNumOrSpace(rs[i-1])) || (i != len(rs)-1 && !isNumOrSpace(rs[i+1])) { |
| 49 | rs[i] = ' ' |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | name = string(rs) |
| 54 | name = strings.TrimSpace(name) |
| 55 | if len(name) == 0 && origLen > 0 { |
| 56 | // Keep at least one character. This is important to preserve |
| 57 | // empty lines in multi-line headers/footers. |
| 58 | name = " " |
| 59 | } |
| 60 | return strings.ToUpper(name) |
| 61 | } |
| 62 | |
| 63 | // Pad String |
| 64 | // Attempts to place string in the center |
| 65 | func Pad(s, pad string, width int) string { |
| 66 | gap := width - DisplayWidth(s) |
| 67 | if gap > 0 { |
| 68 | gapLeft := int(math.Ceil(float64(gap / 2))) |
| 69 | gapRight := gap - gapLeft |
| 70 | return strings.Repeat(string(pad), gapLeft) + s + strings.Repeat(string(pad), gapRight) |
| 71 | } |
| 72 | return s |
| 73 | } |
| 74 | |
| 75 | // Pad String Right position |
| 76 | // This would place string at the left side of the screen |
| 77 | func PadRight(s, pad string, width int) string { |
| 78 | gap := width - DisplayWidth(s) |
| 79 | if gap > 0 { |
| 80 | return s + strings.Repeat(string(pad), gap) |
| 81 | } |
| 82 | return s |
| 83 | } |
| 84 | |
| 85 | // Pad String Left position |
| 86 | // This would place string at the right side of the screen |
| 87 | func PadLeft(s, pad string, width int) string { |
| 88 | gap := width - DisplayWidth(s) |
| 89 | if gap > 0 { |
| 90 | return strings.Repeat(string(pad), gap) + s |
| 91 | } |
| 92 | return s |
| 93 | } |