Skip to content

Commit

Permalink
pretty: add a new format style 'round', -w and -W allow multiple values
Browse files Browse the repository at this point in the history
  • Loading branch information
shenwei356 committed Dec 5, 2024
1 parent b6d6456 commit d1b6993
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 44 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
- [csvtk v0.31.2](https://github.com/shenwei356/csvtk/releases/tag/v0.31.2)
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.31.2/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.31.2)
- [csvtk v0.32.0](https://github.com/shenwei356/csvtk/releases/tag/v0.32.0)
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.32.0/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.32.0)
- `csvtk filter2/mutate2/mutate3`:
- fix a bug of mismatch between column names and values. [#295](https://github.com/shenwei356/csvtk/issues/295)
- fix a bug of mismatch between column names and values which was brought in v0.31.1. [#295](https://github.com/shenwei356/csvtk/issues/295)
- add some unit tests.
- `csvtk pretty`:
- `-w/--min-width` and `-W/--max-width` accept multiple values for setting column-specific thresholds.
- add a new format style `round` for round corners.
- [csvtk v0.31.1](https://github.com/shenwei356/csvtk/releases/tag/v0.31.1)
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.31.1/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.31.1)
- `csvtk filter2/mutate2/mutate3`:
Expand Down
15 changes: 15 additions & 0 deletions csvtk/cmd/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,21 @@ func getFlagStringSlice(cmd *cobra.Command, flag string) []string {
return value
}

func getFlagStringSliceAsInts(cmd *cobra.Command, flag string) []int {
values, err := cmd.Flags().GetStringSlice(flag)
checkError(err)

ints := make([]int, len(values))
for i, s := range values {
v, err := strconv.Atoi(s)
if err != nil {
checkError(fmt.Errorf("the value of %s should be a number: %s", flag, s))
}
ints[i] = v
}
return ints
}

func unshift(list *[]string, val string) {
if len(*list) == 0 {
list = &[]string{val}
Expand Down
64 changes: 52 additions & 12 deletions csvtk/cmd/pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ var prettyCmd = &cobra.Command{
How to:
1. First -n/--buf-rows rows are read to check the minimum and maximum widths
of each columns. You can also set the global thresholds -w/--min-width and
-W/--max-width.
of each columns.
You can also set the global or column-specific (the number of values need
equal to the number of columns) thresholds via -w/--min-width and -W/--max-width.
1a. Cells longer than the maximum width will be wrapped (default) or
clipped (--clip).
Usually, the text is wrapped in space (-x/--wrap-delimiter). But if one
Expand Down Expand Up @@ -120,6 +123,16 @@ Styles:
| 2 | Tiny |
└----┴------┘
round:
╭----┬------╮
| id | size |
├====┼======┤
| 1 | Huge |
├----┼------┤
| 2 | Tiny |
╰----┴------╯
bold:
┏━━━━┳━━━━━━┓
Expand Down Expand Up @@ -152,8 +165,8 @@ Styles:
alignRights := getFlagStringSlice(cmd, "align-right")
alignCenters := getFlagStringSlice(cmd, "align-center")
separator := getFlagString(cmd, "separator")
minWidth := getFlagNonNegativeInt(cmd, "min-width")
maxWidth := getFlagNonNegativeInt(cmd, "max-width")
minWidths := getFlagStringSliceAsInts(cmd, "min-width")
maxWidths := getFlagStringSliceAsInts(cmd, "max-width")
bufRows := getFlagNonNegativeInt(cmd, "buf-rows")
style := getFlagString(cmd, "style")
clip := getFlagBool(cmd, "clip")
Expand Down Expand Up @@ -201,6 +214,7 @@ Styles:
"3line": stable.StyleThreeLine,
"grid": stable.StyleGrid,
"light": stable.StyleLight,
"round": stable.StyleRound,
"bold": stable.StyleBold,
"double": stable.StyleDouble,
}
Expand All @@ -219,11 +233,11 @@ Styles:
checkError(fmt.Errorf("style not available: %s. available vaules: default, plain, simple, 3line, grid, light, bold, double", style))
}

if minWidth > 0 {
tbl.MinWidth(minWidth)
if len(minWidths) == 1 {
tbl.MinWidth(minWidths[0])
}
if maxWidth > 0 {
tbl.MaxWidth(maxWidth)
if len(maxWidths) == 1 {
tbl.MaxWidth(maxWidths[0])
}

if clip {
Expand All @@ -250,15 +264,41 @@ Styles:
checkFirstLine = false

ncols := len(record.All)

if len(minWidths) > 1 && len(minWidths) != ncols {
checkError(fmt.Errorf("the number of values from --min-width (%d) need equal to the number of columns (%d)", len(minWidths), ncols))
}
if len(maxWidths) > 1 && len(maxWidths) != ncols {
checkError(fmt.Errorf("the number of values from --max-width (%d) need equal to the number of columns (%d)", len(maxWidths), ncols))
}

if config.ShowRowNumber {
ncols++
}
header = make([]stable.Column, ncols)

var i, w int

if len(minWidths) > 1 {
for i, w = range minWidths {
if config.ShowRowNumber {
i++
}
header[i].MinWidth = w
}
}
if len(maxWidths) > 1 {
for i, w = range maxWidths {
if config.ShowRowNumber {
i++
}
header[i].MaxWidth = w
}
}

// the fields is 1-based
colnames2fileds := make(map[string][]int, ncols)

var i int
var col string
var ok bool
if !config.NoHeaderRow || record.IsHeaderRow {
Expand Down Expand Up @@ -348,12 +388,12 @@ func init() {
prettyCmd.Flags().StringP("separator", "s", " ", "fields/columns separator")
prettyCmd.Flags().StringSliceP("align-right", "r", []string{}, `align right for selected columns (field index/range or column name, type "csvtk pretty -h" for examples)`)
prettyCmd.Flags().StringSliceP("align-center", "m", []string{}, `align right for selected columns (field index/range or column name, type "csvtk pretty -h" for examples)`)
prettyCmd.Flags().IntP("min-width", "w", 0, "min width")
prettyCmd.Flags().IntP("max-width", "W", 0, "max width")
prettyCmd.Flags().StringSliceP("min-width", "w", []string{}, "min width, multiple values (min widths for each column, 0 for no limit) should be separated by commas. E.g., -w 0,10,10 limits the min widths of 2nd and 3rd columns")
prettyCmd.Flags().StringSliceP("max-width", "W", []string{}, "max width, multiple values (max widths for each column, 0 for no limit) should be separated by commas. E.g., -W 40,20,0 limits the max widths of 1st and 2nd columns")

prettyCmd.Flags().StringP("wrap-delimiter", "x", " ", "delimiter for wrapping cells")
prettyCmd.Flags().IntP("buf-rows", "n", 1024, "the number of rows to determine the min and max widths (0 for all rows)")
prettyCmd.Flags().StringP("style", "S", "", "output syle. available vaules: default, plain, simple, 3line, grid, light, bold, double. check https://github.com/shenwei356/stable")
prettyCmd.Flags().StringP("style", "S", "", "output syle. available vaules: default, plain, simple, 3line, grid, light, round, bold, double. check https://github.com/shenwei356/stable")
prettyCmd.Flags().BoolP("clip", "", false, "clip longer cell instead of wrapping")
prettyCmd.Flags().StringP("clip-mark", "", "...", "clip mark")
}
2 changes: 1 addition & 1 deletion csvtk/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

// VERSION of csvtk
const VERSION = "0.31.1"
const VERSION = "0.31.2"

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Expand Down
32 changes: 20 additions & 12 deletions doc/docs/download.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@

## Current Version

- [csvtk v0.31.1](https://github.com/shenwei356/csvtk/releases/tag/v0.31.1)
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.31.1/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.31.1)
- [csvtk v0.32.0](https://github.com/shenwei356/csvtk/releases/tag/v0.32.0)
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.32.0/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.32.0)
- `csvtk filter2/mutate2/mutate3`:
- **fix the slow speed**, I was stupid before. [#269](https://github.com/shenwei356/csvtk/issues/269)
- `csvtk csv2json`:
- further fix values with double quotes and new line symbols. [#291](https://github.com/shenwei356/csvtk/issues/291)
- fix a bug of mismatch between column names and values which was brought in v0.31.1. [#295](https://github.com/shenwei356/csvtk/issues/295)
- add some unit tests.
- `csvtk pretty`:
- `-w/--min-width` and `-W/--max-width` accept multiple values for setting column-specific thresholds.
- add a new format style `round` for round corners.


### Links

OS |Arch |File, 中国镜像 |Download Count
:------|:---------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Linux |32-bit |[csvtk_linux_386.tar.gz](https://github.com/shenwei356/csvtk/releases/download/v0.31.1/csvtk_linux_386.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_linux_386.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_linux_386.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.31.1/csvtk_linux_386.tar.gz)
Linux |**64-bit**|[**csvtk_linux_amd64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.31.1/csvtk_linux_amd64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_linux_amd64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_linux_amd64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.31.1/csvtk_linux_amd64.tar.gz)
Linux |**64-bit**|[**csvtk_linux_arm64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.31.1/csvtk_linux_arm64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_linux_arm64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_linux_arm64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.31.1/csvtk_linux_arm64.tar.gz)
macOS |**64-bit**|[**csvtk_darwin_amd64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.31.1/csvtk_darwin_amd64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_darwin_amd64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_darwin_amd64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.31.1/csvtk_darwin_amd64.tar.gz)
macOS |**arm64** |[**csvtk_darwin_arm64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.31.1/csvtk_darwin_arm64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_darwin_arm64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_darwin_arm64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.31.1/csvtk_darwin_arm64.tar.gz)
Windows|32-bit |[csvtk_windows_386.exe.tar.gz](https://github.com/shenwei356/csvtk/releases/download/v0.31.1/csvtk_windows_386.exe.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_windows_386.exe.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_windows_386.exe.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.31.1/csvtk_windows_386.exe.tar.gz)
Windows|**64-bit**|[**csvtk_windows_amd64.exe.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.31.1/csvtk_windows_amd64.exe.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_windows_amd64.exe.tar.gz)|[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_windows_amd64.exe.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.31.1/csvtk_windows_amd64.exe.tar.gz)
Linux |32-bit |[csvtk_linux_386.tar.gz](https://github.com/shenwei356/csvtk/releases/download/v0.32.0/csvtk_linux_386.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_linux_386.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_linux_386.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.32.0/csvtk_linux_386.tar.gz)
Linux |**64-bit**|[**csvtk_linux_amd64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.32.0/csvtk_linux_amd64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_linux_amd64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_linux_amd64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.32.0/csvtk_linux_amd64.tar.gz)
Linux |**64-bit**|[**csvtk_linux_arm64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.32.0/csvtk_linux_arm64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_linux_arm64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_linux_arm64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.32.0/csvtk_linux_arm64.tar.gz)
macOS |**64-bit**|[**csvtk_darwin_amd64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.32.0/csvtk_darwin_amd64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_darwin_amd64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_darwin_amd64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.32.0/csvtk_darwin_amd64.tar.gz)
macOS |**arm64** |[**csvtk_darwin_arm64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.32.0/csvtk_darwin_arm64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_darwin_arm64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_darwin_arm64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.32.0/csvtk_darwin_arm64.tar.gz)
Windows|32-bit |[csvtk_windows_386.exe.tar.gz](https://github.com/shenwei356/csvtk/releases/download/v0.32.0/csvtk_windows_386.exe.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_windows_386.exe.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_windows_386.exe.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.32.0/csvtk_windows_386.exe.tar.gz)
Windows|**64-bit**|[**csvtk_windows_amd64.exe.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.32.0/csvtk_windows_amd64.exe.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_windows_amd64.exe.tar.gz)|[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_windows_amd64.exe.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.32.0/csvtk_windows_amd64.exe.tar.gz)

**Notes**

Expand Down Expand Up @@ -143,6 +145,12 @@ fish:

## Release history

- [csvtk v0.31.1](https://github.com/shenwei356/csvtk/releases/tag/v0.31.1)
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.31.1/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.31.1)
- `csvtk filter2/mutate2/mutate3`:
- **fix the slow speed**, I was stupid before. [#269](https://github.com/shenwei356/csvtk/issues/269)
- `csvtk csv2json`:
- further fix values with double quotes and new line symbols. [#291](https://github.com/shenwei356/csvtk/issues/291)
- [csvtk v0.31.0](https://github.com/shenwei356/csvtk/releases/tag/v0.31.0)
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.31.0/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.31.0)
- new command:
Expand Down
Loading

0 comments on commit d1b6993

Please sign in to comment.