Skip to content

Commit 931d99e

Browse files
committed
Added -skip flag to skip certain lines in the input file
1 parent 785709d commit 931d99e

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ go install github.com/msoap/t-plot@latest
1616
- `-s ...` - style, "bar-simple", "bar-horizontal-1px", "bar-vertical-1px" (default: "bar-simple")
1717
- `-c "#"` - chart symbol (default: `#`)
1818
- `-w N` - width of chart (default: rest of terminal width)
19+
- `-skip regex` - skip lines matching regex
1920
- `-h` - print help and exit
2021

2122
## Examples

t-plot.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ Options:
44
- `-s ...` - style, "bar-simple", "bar-horizontal-1px", "bar-vertical-1px" (default: "bar-simple")
55
- `-c "#"` - chart character (default: `#`)
66
- `-w N` - width of chart (default: rest of terminal width using $COLUMNS)
7-
- `-h` - print help and exit
7+
- `-skip regex` - skip lines matching regex
8+
- `-h` - print help and exit
89
*/
910
package main
1011

1112
import (
1213
"flag"
1314
"fmt"
1415
"os"
16+
"regexp"
1517
"slices"
1618
"strconv"
1719
"strings"
@@ -36,9 +38,11 @@ type opt struct {
3638
columnN int
3739
barChar string
3840
width int
41+
skipReg *regexp.Regexp
3942
}
4043

4144
type lineData struct {
45+
line string
4246
num float64
4347
width int
4448
}
@@ -52,7 +56,7 @@ func main() {
5256
}
5357

5458
info := getTextInfo(cfg, lines)
55-
maxs := getAllMax(info)
59+
maxs := getAllMax(cfg.skipReg, info)
5660
chartLines := createChart(cfg, lines, info, maxs)
5761
fmt.Println(strings.Join(chartLines, "\n"))
5862
}
@@ -64,12 +68,14 @@ func printErr(frmt string, args ...any) {
6468

6569
func parseArgs() opt {
6670
res := opt{}
71+
skipReg := ""
6772

6873
doHelp := flag.Bool("h", false, "print help and exit")
6974
flag.Var(&res.style, "s", `style, "bar-simple", "bar-horizontal-1px", "bar-vertical-1px" (default: "bar-simple")`)
7075
flag.IntVar(&res.columnN, "k", 0, "column number for plot, starting from 1, by default try to detect the first column of numbers")
7176
flag.StringVar(&res.barChar, "c", "■", "bar chart character")
7277
flag.IntVar(&res.width, "w", 0, "width of chart")
78+
flag.StringVar(&skipReg, "skip", "", "skip lines matching regex")
7379
flag.Parse()
7480

7581
if *doHelp {
@@ -81,6 +87,14 @@ func parseArgs() opt {
8187
printErr("bar chart character is empty\n")
8288
}
8389

90+
if skipReg != "" {
91+
var err error
92+
res.skipReg, err = regexp.Compile(skipReg)
93+
if err != nil {
94+
printErr("compile skip regex %q: %s\n", skipReg, err)
95+
}
96+
}
97+
8498
return res
8599
}
86100

@@ -105,6 +119,7 @@ func getTextInfo(cfg opt, lines []string) []lineData {
105119
columnN = detectNumbersColumn(fieldsList)
106120
}
107121
for i, line := range lines {
122+
res[i].line = line
108123
res[i].width = runewidth.StringWidth(line)
109124

110125
fields := fieldsList[i]
@@ -178,9 +193,12 @@ func detectNumbersColumn(fieldsList [][]string) int {
178193
return 1 // Default to first column if no numeric columns found
179194
}
180195

181-
func getAllMax(info []lineData) lineData {
196+
func getAllMax(skipReg *regexp.Regexp, info []lineData) lineData {
182197
maxNum, maxWidth := 0.0, 0
183198
for _, item := range info {
199+
if skipReg != nil && skipReg.MatchString(item.line) {
200+
continue
201+
}
184202
if item.num > maxNum {
185203
maxNum = item.num
186204
}
@@ -189,7 +207,7 @@ func getAllMax(info []lineData) lineData {
189207
}
190208
}
191209

192-
return lineData{maxNum, maxWidth}
210+
return lineData{"", maxNum, maxWidth}
193211
}
194212

195213
func createChart(cfg opt, lines []string, info []lineData, maxs lineData) []string {
@@ -212,6 +230,10 @@ func createChart(cfg opt, lines []string, info []lineData, maxs lineData) []stri
212230

213231
res := make([]string, len(lines))
214232
for i := range lines {
233+
if cfg.skipReg != nil && cfg.skipReg.MatchString(lines[i]) {
234+
res[i] = lines[i]
235+
continue
236+
}
215237
res[i] = lines[i] + "\t" + barChart[i]
216238
}
217239

0 commit comments

Comments
 (0)