mirror of
https://gitea.com/gitea/tea.git
synced 2024-11-22 18:41:36 +01:00
3c312cb409
* github.com/araddon/dateparse upgrade => v0.0.0-20200409225146-d820a6159ab1 * code.gitea.io/sdk/gitea upgrade => v0.11.3 * github.com/olekukonko/tablewriter upgrade => v0.0.4 * github.com/mattn/go-runewidth upgrade => v0.0.9 * github.com/stretchr/testify upgrade => v1.5.1 * github.com/davecgh/go-spew upgrade => v1.1.1 * github.com/urfave/cli/v2 upgrade => v2.2.0 Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/129 Reviewed-by: techknowlogick <techknowlogick@gitea.io> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
94 lines
2.2 KiB
Go
94 lines
2.2 KiB
Go
// Copyright 2014 Oleku Konko All rights reserved.
|
|
// Use of this source code is governed by a MIT
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// This module is a Table Writer API for the Go Programming Language.
|
|
// The protocols were written in pure Go and works on windows and unix systems
|
|
|
|
package tablewriter
|
|
|
|
import (
|
|
"math"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/mattn/go-runewidth"
|
|
)
|
|
|
|
var ansi = regexp.MustCompile("\033\\[(?:[0-9]{1,3}(?:;[0-9]{1,3})*)?[m|K]")
|
|
|
|
func DisplayWidth(str string) int {
|
|
return runewidth.StringWidth(ansi.ReplaceAllLiteralString(str, ""))
|
|
}
|
|
|
|
// Simple Condition for string
|
|
// Returns value based on condition
|
|
func ConditionString(cond bool, valid, inValid string) string {
|
|
if cond {
|
|
return valid
|
|
}
|
|
return inValid
|
|
}
|
|
|
|
func isNumOrSpace(r rune) bool {
|
|
return ('0' <= r && r <= '9') || r == ' '
|
|
}
|
|
|
|
// Format Table Header
|
|
// Replace _ , . and spaces
|
|
func Title(name string) string {
|
|
origLen := len(name)
|
|
rs := []rune(name)
|
|
for i, r := range rs {
|
|
switch r {
|
|
case '_':
|
|
rs[i] = ' '
|
|
case '.':
|
|
// ignore floating number 0.0
|
|
if (i != 0 && !isNumOrSpace(rs[i-1])) || (i != len(rs)-1 && !isNumOrSpace(rs[i+1])) {
|
|
rs[i] = ' '
|
|
}
|
|
}
|
|
}
|
|
name = string(rs)
|
|
name = strings.TrimSpace(name)
|
|
if len(name) == 0 && origLen > 0 {
|
|
// Keep at least one character. This is important to preserve
|
|
// empty lines in multi-line headers/footers.
|
|
name = " "
|
|
}
|
|
return strings.ToUpper(name)
|
|
}
|
|
|
|
// Pad String
|
|
// Attempts to place string in the center
|
|
func Pad(s, pad string, width int) string {
|
|
gap := width - DisplayWidth(s)
|
|
if gap > 0 {
|
|
gapLeft := int(math.Ceil(float64(gap / 2)))
|
|
gapRight := gap - gapLeft
|
|
return strings.Repeat(string(pad), gapLeft) + s + strings.Repeat(string(pad), gapRight)
|
|
}
|
|
return s
|
|
}
|
|
|
|
// Pad String Right position
|
|
// This would place string at the left side of the screen
|
|
func PadRight(s, pad string, width int) string {
|
|
gap := width - DisplayWidth(s)
|
|
if gap > 0 {
|
|
return s + strings.Repeat(string(pad), gap)
|
|
}
|
|
return s
|
|
}
|
|
|
|
// Pad String Left position
|
|
// This would place string at the right side of the screen
|
|
func PadLeft(s, pad string, width int) string {
|
|
gap := width - DisplayWidth(s)
|
|
if gap > 0 {
|
|
return strings.Repeat(string(pad), gap) + s
|
|
}
|
|
return s
|
|
}
|