mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-17 04:30:56 +01:00
refactoring and gouncritic, 1st part
This commit is contained in:
@@ -41,11 +41,11 @@ func contains(s []int, e int) bool {
|
||||
|
||||
// parse columns list given with -c, modifies config.UseColumns based
|
||||
// on eventually given regex
|
||||
func PrepareColumns(c *cfg.Config, data *Tabdata) error {
|
||||
if len(c.Columns) > 0 {
|
||||
for _, use := range strings.Split(c.Columns, ",") {
|
||||
func PrepareColumns(conf *cfg.Config, data *Tabdata) error {
|
||||
if len(conf.Columns) > 0 {
|
||||
for _, use := range strings.Split(conf.Columns, ",") {
|
||||
if len(use) == 0 {
|
||||
msg := fmt.Sprintf("Could not parse columns list %s: empty column", c.Columns)
|
||||
msg := fmt.Sprintf("Could not parse columns list %s: empty column", conf.Columns)
|
||||
return errors.New(msg)
|
||||
}
|
||||
|
||||
@@ -54,14 +54,14 @@ func PrepareColumns(c *cfg.Config, data *Tabdata) error {
|
||||
// might be a regexp
|
||||
colPattern, err := regexp.Compile(use)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("Could not parse columns list %s: %v", c.Columns, err)
|
||||
msg := fmt.Sprintf("Could not parse columns list %s: %v", conf.Columns, err)
|
||||
return errors.New(msg)
|
||||
}
|
||||
|
||||
// find matching header fields
|
||||
for i, head := range data.headers {
|
||||
if colPattern.MatchString(head) {
|
||||
c.UseColumns = append(c.UseColumns, i+1)
|
||||
conf.UseColumns = append(conf.UseColumns, i+1)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -70,45 +70,45 @@ func PrepareColumns(c *cfg.Config, data *Tabdata) error {
|
||||
// a colum spec is not a number, we process them above
|
||||
// inside the err handler for atoi(). so only add the
|
||||
// number, if it's really just a number.
|
||||
c.UseColumns = append(c.UseColumns, usenum)
|
||||
conf.UseColumns = append(conf.UseColumns, usenum)
|
||||
}
|
||||
}
|
||||
|
||||
// deduplicate: put all values into a map (value gets map key)
|
||||
// thereby removing duplicates, extract keys into new slice
|
||||
// and sort it
|
||||
imap := make(map[int]int, len(c.UseColumns))
|
||||
for _, i := range c.UseColumns {
|
||||
imap := make(map[int]int, len(conf.UseColumns))
|
||||
for _, i := range conf.UseColumns {
|
||||
imap[i] = 0
|
||||
}
|
||||
c.UseColumns = nil
|
||||
conf.UseColumns = nil
|
||||
for k := range imap {
|
||||
c.UseColumns = append(c.UseColumns, k)
|
||||
conf.UseColumns = append(conf.UseColumns, k)
|
||||
}
|
||||
sort.Ints(c.UseColumns)
|
||||
sort.Ints(conf.UseColumns)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// prepare headers: add numbers to headers
|
||||
func numberizeAndReduceHeaders(c cfg.Config, data *Tabdata) {
|
||||
func numberizeAndReduceHeaders(conf cfg.Config, data *Tabdata) {
|
||||
numberedHeaders := []string{}
|
||||
maxwidth := 0 // start from scratch, so we only look at displayed column widths
|
||||
|
||||
for i, head := range data.headers {
|
||||
for idx, head := range data.headers {
|
||||
headlen := 0
|
||||
if len(c.Columns) > 0 {
|
||||
if len(conf.Columns) > 0 {
|
||||
// -c specified
|
||||
if !contains(c.UseColumns, i+1) {
|
||||
if !contains(conf.UseColumns, idx+1) {
|
||||
// ignore this one
|
||||
continue
|
||||
}
|
||||
}
|
||||
if c.NoNumbering {
|
||||
if conf.NoNumbering {
|
||||
numberedHeaders = append(numberedHeaders, head)
|
||||
headlen = len(head)
|
||||
} else {
|
||||
numhead := fmt.Sprintf("%s(%d)", head, i+1)
|
||||
numhead := fmt.Sprintf("%s(%d)", head, idx+1)
|
||||
headlen = len(numhead)
|
||||
numberedHeaders = append(numberedHeaders, numhead)
|
||||
}
|
||||
@@ -124,14 +124,14 @@ func numberizeAndReduceHeaders(c cfg.Config, data *Tabdata) {
|
||||
}
|
||||
|
||||
// exclude columns, if any
|
||||
func reduceColumns(c cfg.Config, data *Tabdata) {
|
||||
if len(c.Columns) > 0 {
|
||||
func reduceColumns(conf cfg.Config, data *Tabdata) {
|
||||
if len(conf.Columns) > 0 {
|
||||
reducedEntries := [][]string{}
|
||||
var reducedEntry []string
|
||||
for _, entry := range data.entries {
|
||||
reducedEntry = nil
|
||||
for i, value := range entry {
|
||||
if !contains(c.UseColumns, i+1) {
|
||||
if !contains(conf.UseColumns, i+1) {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -153,8 +153,9 @@ func trimRow(row []string) []string {
|
||||
return fixedrow
|
||||
}
|
||||
|
||||
func colorizeData(c cfg.Config, output string) string {
|
||||
if c.UseHighlight && color.IsConsole(os.Stdout) {
|
||||
func colorizeData(conf cfg.Config, output string) string {
|
||||
switch {
|
||||
case conf.UseHighlight && color.IsConsole(os.Stdout):
|
||||
highlight := true
|
||||
colorized := ""
|
||||
first := true
|
||||
@@ -167,17 +168,17 @@ func colorizeData(c cfg.Config, output string) string {
|
||||
// in pprint mode. This doesn't matter as long as
|
||||
// we don't use colorization. But with colors the
|
||||
// missing spaces can be seen.
|
||||
if c.OutputMode == cfg.Ascii {
|
||||
line = line + " "
|
||||
if conf.OutputMode == cfg.ASCII {
|
||||
line += " "
|
||||
}
|
||||
|
||||
line = c.HighlightHdrStyle.Sprint(line)
|
||||
line = conf.HighlightHdrStyle.Sprint(line)
|
||||
first = false
|
||||
} else {
|
||||
line = c.HighlightStyle.Sprint(line)
|
||||
line = conf.HighlightStyle.Sprint(line)
|
||||
}
|
||||
} else {
|
||||
line = c.NoHighlightStyle.Sprint(line)
|
||||
line = conf.NoHighlightStyle.Sprint(line)
|
||||
}
|
||||
highlight = !highlight
|
||||
|
||||
@@ -185,12 +186,12 @@ func colorizeData(c cfg.Config, output string) string {
|
||||
}
|
||||
|
||||
return colorized
|
||||
} else if len(c.Pattern) > 0 && !c.NoColor && color.IsConsole(os.Stdout) {
|
||||
r := regexp.MustCompile("(" + c.Pattern + ")")
|
||||
case len(conf.Pattern) > 0 && !conf.NoColor && color.IsConsole(os.Stdout):
|
||||
r := regexp.MustCompile("(" + conf.Pattern + ")")
|
||||
return r.ReplaceAllStringFunc(output, func(in string) string {
|
||||
return c.ColorStyle.Sprint(in)
|
||||
return conf.ColorStyle.Sprint(in)
|
||||
})
|
||||
} else {
|
||||
default:
|
||||
return output
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user