提交 a2996516 编写于 作者: 陈琦

add pad before deal with prefix and postfix

上级 42998686
Version = 1.7
Version = 1.7
Language = zh
......@@ -94,10 +94,10 @@ fields:
postfix: "\t"
- field: field_length # 指定宽度。
range: 1-99 # 01\t,02\t,03\t..., 99\t
range: 1-99 # 001\n,002\n,003\n..., 099\n
length: 3 # 包含前后缀的宽度。
leftpad: 0 # 宽度不够时,补充的字符。
postfix: "\t"
postfix: "-"
- field: field_text # 从一个文件中随机读取。
range: users.txt:R # 相对当前文件路径。
......
......@@ -12,6 +12,7 @@ import (
stringUtils "github.com/easysoft/zendata/src/utils/string"
"github.com/easysoft/zendata/src/utils/vari"
"github.com/fatih/color"
"github.com/mattn/go-runewidth"
"regexp"
"strconv"
"strings"
......@@ -340,6 +341,9 @@ func loopFieldValWithFix(field *model.DefField, fieldValue model.FieldWithValues
*indexOfRow++
}
if field.Length > runewidth.StringWidth(loopStr) {
loopStr = stringUtils.AddPad(loopStr, *field)
}
if withFix && !vari.Trim {
loopStr = prefix + loopStr + postfix
}
......
......@@ -17,7 +17,7 @@ func GenExpressionValues(field model.DefField, valuesMap map[string][]string) (r
exp := field.Value
reg := regexp.MustCompile(`\$([_,a-z,A-Z,0-9]+)`)
arr := reg.FindAllStringSubmatch(exp,-1)
arr := reg.FindAllStringSubmatch(exp, -1)
total := 1
for _, items := range arr { // computer total
......@@ -48,7 +48,7 @@ func GenExpressionValues(field model.DefField, valuesMap map[string][]string) (r
tp := ""
var val interface{}
if len(referValues) > 0 {
valStr = referValues[i % len(referValues)]
valStr = referValues[i%len(referValues)]
valStr = strings.TrimLeft(valStr, referField.Prefix)
valStr = strings.TrimRight(valStr, referField.Postfix)
......@@ -71,16 +71,17 @@ func GenExpressionValues(field model.DefField, valuesMap map[string][]string) (r
}
mask := ""
if dataTypeFromValues == "int" {
if dataTypeFromValues == "int" {
mask = "%.0f"
} else {
mask = "%d"
}
str := fmt.Sprintf(field.Prefix + mask + field.Postfix, result)
str := fmt.Sprintf(mask, result)
if field.Length > runewidth.StringWidth(str) {
str = stringUtils.AddPad(str, field)
}
str = field.Prefix + str + field.Postfix
ret = append(ret, str)
}
}
......@@ -105,4 +106,4 @@ func getNumType(str string) (val interface{}, tp string) {
tp = "float"
return
}
\ No newline at end of file
}
......@@ -35,7 +35,7 @@ func Print(rows [][]string, format string, table string, colIsNumArr []bool,
col = replacePlaceholder(col)
field := vari.TopFieldMap[fields[j]]
if field.Length > runewidth.StringWidth(col) {
col = stringUtils.AddPad(col, field)
//col = stringUtils.AddPad(col, field)
}
if j > 0 && vari.Human { // use a tab
......
......@@ -5,7 +5,6 @@ import (
"github.com/360EntSecGroup-Skylar/excelize/v2"
constant "github.com/easysoft/zendata/src/utils/const"
logUtils "github.com/easysoft/zendata/src/utils/log"
stringUtils "github.com/easysoft/zendata/src/utils/string"
"github.com/easysoft/zendata/src/utils/vari"
"github.com/mattn/go-runewidth"
)
......@@ -13,12 +12,13 @@ import (
const (
sheetName = "Sheet1"
)
var (
csvWriter *csv.Writer
)
func Write(rows [][]string, format string, table string, colIsNumArr []bool,
fields []string) (lines []interface{}) {
fields []string) (lines []interface{}) {
f := excelize.NewFile()
index := f.NewSheet(sheetName)
......@@ -38,11 +38,11 @@ func Write(rows [][]string, format string, table string, colIsNumArr []bool,
col = replacePlaceholder(col)
field := vari.TopFieldMap[fields[j]]
if field.Length > runewidth.StringWidth(col) {
col = stringUtils.AddPad(col, field)
//col = stringUtils.AddPad(col, field)
}
if format == constant.FormatExcel {
colName, _ := excelize.CoordinatesToCellName(j + 1, i + 2)
colName, _ := excelize.CoordinatesToCellName(j+1, i+2)
f.SetCellValue(sheetName, colName, col)
} else if format == constant.FormatCsv {
......@@ -69,9 +69,9 @@ func Write(rows [][]string, format string, table string, colIsNumArr []bool,
func printExcelHeader(fields []string, f *excelize.File) {
headerLine := ""
for idx, field := range fields {
colName, _ := excelize.CoordinatesToCellName(idx + 1, 1)
colName, _ := excelize.CoordinatesToCellName(idx+1, 1)
f.SetCellValue(sheetName, colName, field)
}
logUtils.PrintLine(headerLine)
}
\ No newline at end of file
}
......@@ -13,6 +13,7 @@ import (
constant "github.com/easysoft/zendata/src/utils/const"
"github.com/mattn/go-runewidth"
"gopkg.in/yaml.v2"
"net/url"
"regexp"
"strconv"
"strings"
......@@ -72,7 +73,7 @@ func FindInArrBool(str string, arr []string) bool {
found, _ := FindInArr(str, arr)
return found
}
func FindInArr(str string, arr []string) (bool,int) {
func FindInArr(str string, arr []string) (bool, int) {
for index, s := range arr {
if str == s {
return true, index
......@@ -87,8 +88,8 @@ func StrInArr(str string, arr []string) bool {
return found
}
func InArray(need interface{}, arr []string) bool {
for _,v := range arr{
if need == v{
for _, v := range arr {
if need == v {
return true
}
}
......@@ -141,7 +142,7 @@ func FormatStr(format string, val interface{}, precision int) (ret string, pass
}
str := fmt.Sprintf(format, val)
if strings.Index(str,"%!") == 0 {
if strings.Index(str, "%!") == 0 {
return "", false
}
......@@ -149,7 +150,7 @@ func FormatStr(format string, val interface{}, precision int) (ret string, pass
}
func AddPad(str string, field model.DefField) string {
if field.Length > 0 && field.Length > runewidth.StringWidth(str) {
if field.Length > 0 && field.Length > runewidth.StringWidth(str) {
gap := field.Length - len(str)
if field.LeftPad != "" {
field.LeftPad = field.LeftPad[:1]
......@@ -173,7 +174,7 @@ func StartWith(str, sub string) bool {
return strings.Index(str, sub) == 0
}
func EndWith(str, sub string) bool {
return strings.Contains(str, sub) && strings.LastIndex(str, sub) == len(str) -len(sub)
return strings.Contains(str, sub) && strings.LastIndex(str, sub) == len(str)-len(sub)
}
func ConvertForSql(str string) (ret string) {
......@@ -181,10 +182,10 @@ func ConvertForSql(str string) (ret string) {
count := 0
for i, item := range arr {
if count % 2 == 1 && string(item) != "'" {
ret = ret + "'"
} else if i == len(arr) - 1 && count % 2 == 0 && string(item) == "'" {
ret = ret + "'"
if count%2 == 1 && string(item) != "'" {
ret = ret + "'"
} else if i == len(arr)-1 && count%2 == 0 && string(item) == "'" {
ret = ret + "'"
}
if string(item) != "'" {
......@@ -243,7 +244,7 @@ func Base64(str string) (ret string) {
return
}
func UrlEncode(str string) (ret string) {
ret = base64.URLEncoding.EncodeToString([]byte(str))
ret = url.QueryEscape(str)
return
}
......@@ -261,8 +262,8 @@ func ReplaceSpecialChars(bytes []byte) []byte {
}
if strings.Index(strings.TrimSpace(line), "range") == 0 || inRanges {
line = strings.ReplaceAll(line,"[", string(constant.LeftBrackets))
line = strings.ReplaceAll(line,"]", string(constant.RightBrackets))
line = strings.ReplaceAll(line, "[", string(constant.LeftBrackets))
line = strings.ReplaceAll(line, "]", string(constant.RightBrackets))
}
ret += line + "\n"
......@@ -280,7 +281,7 @@ func ConvertYamlStringToMapFormat(bytes []byte) (ret string) {
// replace '"test"' to "test"
reg := regexp.MustCompile(`([:\s]+?)'"(.*)"'`)
//if reg.MatchString(ret) {
ret = reg.ReplaceAllString(ret, `${1}"${2}"`)
ret = reg.ReplaceAllString(ret, `${1}"${2}"`)
//}
return
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册