提交 a2996516 编写于 作者: 陈琦

add pad before deal with prefix and postfix

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