2022-09-30 14:08:59 +02:00
|
|
|
/*
|
|
|
|
|
Copyright © 2022 Thomas von Dein
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-30 19:14:58 +02:00
|
|
|
package lib
|
2022-09-30 14:08:59 +02:00
|
|
|
|
|
|
|
|
import (
|
2022-10-01 14:14:53 +02:00
|
|
|
"errors"
|
2022-09-30 19:14:58 +02:00
|
|
|
"fmt"
|
2022-10-10 20:14:51 +02:00
|
|
|
"github.com/gookit/color"
|
2022-10-19 12:44:19 +02:00
|
|
|
"github.com/tlinden/tablizer/cfg"
|
2022-10-10 20:14:51 +02:00
|
|
|
"os"
|
2022-10-01 14:14:53 +02:00
|
|
|
"regexp"
|
2022-10-15 16:27:04 +02:00
|
|
|
"sort"
|
2022-09-30 14:08:59 +02:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2022-09-30 19:14:58 +02:00
|
|
|
func contains(s []int, e int) bool {
|
|
|
|
|
for _, a := range s {
|
|
|
|
|
if a == e {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2022-09-30 14:08:59 +02:00
|
|
|
|
2022-10-19 12:44:19 +02:00
|
|
|
// 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, ",") {
|
2022-10-15 14:03:30 +02:00
|
|
|
if len(use) == 0 {
|
2022-10-19 12:44:19 +02:00
|
|
|
msg := fmt.Sprintf("Could not parse columns list %s: empty column", c.Columns)
|
2022-10-15 14:03:30 +02:00
|
|
|
return errors.New(msg)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-30 14:08:59 +02:00
|
|
|
usenum, err := strconv.Atoi(use)
|
|
|
|
|
if err != nil {
|
2022-10-15 14:03:30 +02:00
|
|
|
// might be a regexp
|
|
|
|
|
colPattern, err := regexp.Compile(use)
|
|
|
|
|
if err != nil {
|
2022-10-19 12:44:19 +02:00
|
|
|
msg := fmt.Sprintf("Could not parse columns list %s: %v", c.Columns, err)
|
2022-10-15 14:03:30 +02:00
|
|
|
return errors.New(msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// find matching header fields
|
|
|
|
|
for i, head := range data.headers {
|
|
|
|
|
if colPattern.MatchString(head) {
|
2022-10-19 12:44:19 +02:00
|
|
|
c.UseColumns = append(c.UseColumns, i+1)
|
2022-10-15 14:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// we digress from go best practises here, because if
|
|
|
|
|
// 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.
|
2022-10-19 12:44:19 +02:00
|
|
|
c.UseColumns = append(c.UseColumns, usenum)
|
2022-09-30 14:08:59 +02:00
|
|
|
}
|
2022-10-15 14:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-15 16:27:04 +02:00
|
|
|
// deduplicate: put all values into a map (value gets map key)
|
|
|
|
|
// thereby removing duplicates, extract keys into new slice
|
|
|
|
|
// and sort it
|
2022-10-19 12:44:19 +02:00
|
|
|
imap := make(map[int]int, len(c.UseColumns))
|
|
|
|
|
for _, i := range c.UseColumns {
|
2022-10-15 14:03:30 +02:00
|
|
|
imap[i] = 0
|
|
|
|
|
}
|
2022-10-19 12:44:19 +02:00
|
|
|
c.UseColumns = nil
|
2022-10-15 14:03:30 +02:00
|
|
|
for k := range imap {
|
2022-10-19 12:44:19 +02:00
|
|
|
c.UseColumns = append(c.UseColumns, k)
|
2022-09-30 14:08:59 +02:00
|
|
|
}
|
2022-10-19 12:44:19 +02:00
|
|
|
sort.Ints(c.UseColumns)
|
2022-09-30 14:08:59 +02:00
|
|
|
}
|
2022-10-02 14:22:31 +02:00
|
|
|
return nil
|
2022-09-30 14:08:59 +02:00
|
|
|
}
|
2022-10-01 14:14:53 +02:00
|
|
|
|
2022-10-15 14:03:30 +02:00
|
|
|
// prepare headers: add numbers to headers
|
2022-10-19 12:44:19 +02:00
|
|
|
func numberizeAndReduceHeaders(c cfg.Config, data *Tabdata) {
|
2022-10-05 14:31:01 +02:00
|
|
|
numberedHeaders := []string{}
|
2022-10-11 09:11:46 +02:00
|
|
|
maxwidth := 0 // start from scratch, so we only look at displayed column widths
|
|
|
|
|
|
2022-10-05 14:31:01 +02:00
|
|
|
for i, head := range data.headers {
|
2022-10-11 09:11:46 +02:00
|
|
|
headlen := 0
|
2022-10-19 12:44:19 +02:00
|
|
|
if len(c.Columns) > 0 {
|
2022-10-05 14:31:01 +02:00
|
|
|
// -c specified
|
2022-10-19 12:44:19 +02:00
|
|
|
if !contains(c.UseColumns, i+1) {
|
2022-10-05 14:31:01 +02:00
|
|
|
// ignore this one
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-19 12:44:19 +02:00
|
|
|
if c.NoNumbering {
|
2022-10-05 14:31:01 +02:00
|
|
|
numberedHeaders = append(numberedHeaders, head)
|
2022-10-11 09:11:46 +02:00
|
|
|
headlen = len(head)
|
2022-10-05 14:31:01 +02:00
|
|
|
} else {
|
2022-10-11 09:11:46 +02:00
|
|
|
numhead := fmt.Sprintf("%s(%d)", head, i+1)
|
|
|
|
|
headlen = len(numhead)
|
|
|
|
|
numberedHeaders = append(numberedHeaders, numhead)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if headlen > maxwidth {
|
|
|
|
|
maxwidth = headlen
|
2022-10-05 14:31:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
data.headers = numberedHeaders
|
2022-10-11 09:11:46 +02:00
|
|
|
if data.maxwidthHeader != maxwidth && maxwidth > 0 {
|
|
|
|
|
data.maxwidthHeader = maxwidth
|
|
|
|
|
}
|
2022-10-05 14:31:01 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-15 14:03:30 +02:00
|
|
|
// exclude columns, if any
|
2022-10-19 12:44:19 +02:00
|
|
|
func reduceColumns(c cfg.Config, data *Tabdata) {
|
|
|
|
|
if len(c.Columns) > 0 {
|
2022-10-05 14:31:01 +02:00
|
|
|
reducedEntries := [][]string{}
|
2022-10-14 19:51:19 +02:00
|
|
|
var reducedEntry []string
|
2022-10-05 14:31:01 +02:00
|
|
|
for _, entry := range data.entries {
|
|
|
|
|
reducedEntry = nil
|
|
|
|
|
for i, value := range entry {
|
2022-10-19 12:44:19 +02:00
|
|
|
if !contains(c.UseColumns, i+1) {
|
2022-10-05 14:31:01 +02:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reducedEntry = append(reducedEntry, value)
|
|
|
|
|
}
|
|
|
|
|
reducedEntries = append(reducedEntries, reducedEntry)
|
|
|
|
|
}
|
|
|
|
|
data.entries = reducedEntries
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 13:10:23 +02:00
|
|
|
func trimRow(row []string) []string {
|
|
|
|
|
// FIXME: remove this when we only use Tablewriter and strip in ParseFile()!
|
|
|
|
|
var fixedrow []string
|
|
|
|
|
for _, cell := range row {
|
|
|
|
|
fixedrow = append(fixedrow, strings.TrimSpace(cell))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fixedrow
|
|
|
|
|
}
|
2022-10-10 20:14:51 +02:00
|
|
|
|
2022-10-19 12:44:19 +02:00
|
|
|
func colorizeData(c cfg.Config, output string) string {
|
|
|
|
|
if len(c.Pattern) > 0 && !c.NoColor && color.IsConsole(os.Stdout) {
|
|
|
|
|
r := regexp.MustCompile("(" + c.Pattern + ")")
|
2022-10-25 14:24:05 +02:00
|
|
|
return r.ReplaceAllStringFunc(output, func(in string) string {
|
|
|
|
|
return c.ColorStyle.Sprint(in)
|
|
|
|
|
})
|
2022-10-10 20:14:51 +02:00
|
|
|
} else {
|
|
|
|
|
return output
|
|
|
|
|
}
|
|
|
|
|
}
|