github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/util/printer/printer.go (about) 1 // Copyright 2015 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package printer 15 16 import ( 17 "bytes" 18 "fmt" 19 ) 20 21 // Version information. 22 var ( 23 TiDBBuildTS = "None" 24 TiDBGitHash = "None" 25 ) 26 27 // PrintTiDBInfo prints the TiDB version information. 28 func PrintTiDBInfo() { 29 fmt.Printf("Welcome to the TiDB.\n") 30 fmt.Printf("Version:\n") 31 fmt.Printf("Git Commit Hash: %s\n", TiDBGitHash) 32 fmt.Printf("UTC Build Time: %s\n", TiDBBuildTS) 33 fmt.Printf("\n") 34 } 35 36 // checkValidity checks whether cols and every data have the same length. 37 func checkValidity(cols []string, datas [][]string) bool { 38 colLen := len(cols) 39 if len(datas) == 0 || colLen == 0 { 40 return false 41 } 42 43 for _, data := range datas { 44 if colLen != len(data) { 45 return false 46 } 47 } 48 49 return true 50 } 51 52 func getMaxColLen(cols []string, datas [][]string) []int { 53 maxColLen := make([]int, len(cols)) 54 for i, col := range cols { 55 maxColLen[i] = len(col) 56 } 57 58 for _, data := range datas { 59 for i, v := range data { 60 if len(v) > maxColLen[i] { 61 maxColLen[i] = len(v) 62 } 63 } 64 } 65 66 return maxColLen 67 } 68 69 func getPrintDivLine(maxColLen []int) []byte { 70 var value []byte 71 for _, v := range maxColLen { 72 value = append(value, '+') 73 value = append(value, bytes.Repeat([]byte{'-'}, v+2)...) 74 } 75 value = append(value, '+') 76 value = append(value, '\n') 77 return value 78 } 79 80 func getPrintCol(cols []string, maxColLen []int) []byte { 81 var value []byte 82 for i, v := range cols { 83 value = append(value, '|') 84 value = append(value, ' ') 85 value = append(value, []byte(v)...) 86 value = append(value, bytes.Repeat([]byte{' '}, maxColLen[i]+1-len(v))...) 87 } 88 value = append(value, '|') 89 value = append(value, '\n') 90 return value 91 } 92 93 func getPrintRow(data []string, maxColLen []int) []byte { 94 var value []byte 95 for i, v := range data { 96 value = append(value, '|') 97 value = append(value, ' ') 98 value = append(value, []byte(v)...) 99 value = append(value, bytes.Repeat([]byte{' '}, maxColLen[i]+1-len(v))...) 100 } 101 value = append(value, '|') 102 value = append(value, '\n') 103 return value 104 } 105 106 func getPrintRows(datas [][]string, maxColLen []int) []byte { 107 var value []byte 108 for _, data := range datas { 109 value = append(value, getPrintRow(data, maxColLen)...) 110 } 111 return value 112 } 113 114 // GetPrintResult gets a result with a formatted string. 115 func GetPrintResult(cols []string, datas [][]string) (string, bool) { 116 if !checkValidity(cols, datas) { 117 return "", false 118 } 119 120 var value []byte 121 maxColLen := getMaxColLen(cols, datas) 122 123 value = append(value, getPrintDivLine(maxColLen)...) 124 value = append(value, getPrintCol(cols, maxColLen)...) 125 value = append(value, getPrintDivLine(maxColLen)...) 126 value = append(value, getPrintRows(datas, maxColLen)...) 127 value = append(value, getPrintDivLine(maxColLen)...) 128 return string(value), true 129 }