From cc0a4106fbfbca4c7dbed879adb746caa58bfc27 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Mon, 22 Jul 2024 16:11:05 -0400 Subject: [PATCH] fix: tables --- ansi/table.go | 47 ++++++++++--------- ansi/testdata/TestRendererIssues/117.golden | 4 +- ansi/testdata/TestRendererIssues/315.golden | 4 +- ansi/testdata/TestRendererIssues/316.golden | 6 +-- ansi/testdata/TestRendererIssues/44.golden | 12 ++--- ansi/testdata/TestRendererIssues/46_2.golden | 22 ++++----- ansi/testdata/TestRendererIssues/60.golden | 48 ++++++++++---------- dracula.go | 3 -- styles.go | 18 +------- styles/ascii.json | 6 +-- styles/dark.json | 6 +-- styles/dracula.json | 6 +-- styles/light.json | 6 +-- styles/pink.json | 6 +-- styles/tokyo_night.json | 6 +-- 15 files changed, 82 insertions(+), 118 deletions(-) diff --git a/ansi/table.go b/ansi/table.go index 2c7e4264..6ac52948 100644 --- a/ansi/table.go +++ b/ansi/table.go @@ -52,30 +52,34 @@ func (e *TableElement) Render(w io.Writer, ctx RenderContext) error { renderText(iw, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix) renderText(iw, ctx.options.ColorProfile, style, rules.Prefix) width := int(ctx.blockStack.Width(ctx)) - ctx.table.lipgloss = table.New(). - Width(width). - StyleFunc(func(row, col int) lipgloss.Style { - st := lipgloss.NewStyle() - if m := ctx.options.Styles.Table.Margin; m != nil { - st = st.Margin(0, int(*m)) - } - if row == 0 { - st = st.Bold(true) - } + ctx.table.lipgloss = table.New().Width(width) - switch e.table.Alignments[col] { - case astext.AlignLeft: - st = st.Align(lipgloss.Left) - case astext.AlignCenter: - st = st.Align(lipgloss.Center) - case astext.AlignRight: - st = st.Align(lipgloss.Right) - } + return nil +} + +func (e *TableElement) setStyles(ctx RenderContext) { + ctx.table.lipgloss = ctx.table.lipgloss.StyleFunc(func(row, col int) lipgloss.Style { + st := lipgloss.NewStyle() + if m := ctx.options.Styles.Table.Margin; m != nil { + st = st.Padding(0, int(*m)) + } else { + st = st.Padding(0, 1) + } + if row == 0 { + st = st.Bold(true) + } - return st - }) + switch e.table.Alignments[col] { + case astext.AlignLeft: + st = st.Align(lipgloss.Left) + case astext.AlignCenter: + st = st.Align(lipgloss.Center) + case astext.AlignRight: + st = st.Align(lipgloss.Right) + } - return nil + return st + }) } func (e *TableElement) setBorders(ctx RenderContext) { @@ -101,6 +105,7 @@ func (e *TableElement) setBorders(ctx RenderContext) { func (e *TableElement) Finish(_ io.Writer, ctx RenderContext) error { rules := ctx.options.Styles.Table + e.setStyles(ctx) e.setBorders(ctx) ow := ctx.blockStack.Current().Block diff --git a/ansi/testdata/TestRendererIssues/117.golden b/ansi/testdata/TestRendererIssues/117.golden index 22637fc2..8474cb62 100644 --- a/ansi/testdata/TestRendererIssues/117.golden +++ b/ansi/testdata/TestRendererIssues/117.golden @@ -1,6 +1,6 @@                                                                                  - cmd β”‚descr + cmd β”‚ descr ──────────────────────────────────────┼───────────────────────────────────────── -  glow config  β”‚open glow config +  glow config  β”‚ open glow config diff --git a/ansi/testdata/TestRendererIssues/315.golden b/ansi/testdata/TestRendererIssues/315.golden index e7ca1fc2..de1039ea 100644 --- a/ansi/testdata/TestRendererIssues/315.golden +++ b/ansi/testdata/TestRendererIssues/315.golden @@ -1,6 +1,6 @@                                                                                  - Expression β”‚Value β”‚Type + Expression β”‚ Value β”‚ Type ────────────────────────────────────────────┼──────────────────┼──────────────── -  (1 >= 26) || (12 >= 6) {.java} β”‚s β”‚a +  (1 >= 26) || (12 >= 6) {.java} β”‚ s β”‚ a diff --git a/ansi/testdata/TestRendererIssues/316.golden b/ansi/testdata/TestRendererIssues/316.golden index 131cba89..57ec09f8 100644 --- a/ansi/testdata/TestRendererIssues/316.golden +++ b/ansi/testdata/TestRendererIssues/316.golden @@ -1,7 +1,7 @@                                                                                  - Aβ”‚B + A β”‚ B ──────────────────────────────────────────────────────────┼───────────────────── - Here https://example.comβ”‚hello - https://autolink.com https://autolink.comβ”‚world + Here https://example.com β”‚ hello + https://autolink.com https://autolink.com β”‚ world diff --git a/ansi/testdata/TestRendererIssues/44.golden b/ansi/testdata/TestRendererIssues/44.golden index cf12febd..97f3c5a0 100644 --- a/ansi/testdata/TestRendererIssues/44.golden +++ b/ansi/testdata/TestRendererIssues/44.golden @@ -1,9 +1,9 @@                                                                                  - Distri…β”‚Architectures β”‚Package - ───────┼───────────────────────────────────┼──────────────────────────────────── - Debian β”‚ amd64 ,  arm64 ,  armel ,  i386 ,…β”‚ deb  https://github.com/twpayne/ch… - RedHat β”‚ aarch64 ,  armhfp ,  i686 ,  ppc6…β”‚ rpm  https://github.com/twpayne/ch… - OpenSU…β”‚ aarch64 ,  armhfp ,  i686 ,  ppc6…β”‚ rpm  https://github.com/twpayne/ch… - Ubuntu β”‚ amd64 ,  arm64 ,  armel ,  i386 ,…β”‚ deb  https://github.com/twpayne/ch… + Distr… β”‚ Architectures β”‚ Package + ────────┼───────────────────────────────────┼─────────────────────────────────── + Debian β”‚  amd64 ,  arm64 ,  armel ,  i386… β”‚  deb  https://github.com/twpayne… + RedHat β”‚  aarch64 ,  armhfp ,  i686 ,  pp… β”‚  rpm  https://github.com/twpayne… + OpenS… β”‚  aarch64 ,  armhfp ,  i686 ,  pp… β”‚  rpm  https://github.com/twpayne… + Ubuntu β”‚  amd64 ,  arm64 ,  armel ,  i386… β”‚  deb  https://github.com/twpayne… diff --git a/ansi/testdata/TestRendererIssues/46_2.golden b/ansi/testdata/TestRendererIssues/46_2.golden index 62aa4370..265bc3e2 100644 --- a/ansi/testdata/TestRendererIssues/46_2.golden +++ b/ansi/testdata/TestRendererIssues/46_2.golden @@ -1,14 +1,14 @@                                                                                  - Dependency β”‚Installa…β”‚Operation - ─────────────────────────────────────────────┼─────────┼──────────────────────── - xdg-open (Linux), open(1) (macOS), cygstart …β”‚base β”‚desktop opener - file, coreutils (cp, mv, rm), xargs β”‚base β”‚file type, copy, move a… - tar, (un)zip [atool/bsdtar for more formats] β”‚base β”‚create, list, extract t… - archivemount, fusermount(3) β”‚optional β”‚mount, unmount archives - sshfs, rclone https://rclone.org/, fusermoun…β”‚optional β”‚mount, unmount remotes - trash-cli β”‚optional β”‚trash files (default ac… - vlock (Linux), bashlock (macOS), lock(1) (BS…β”‚optional β”‚terminal locker (fallba… - advcpmv (Linux) (integration https://github.…β”‚optional β”‚copy, move progress -  $VISUAL  (else  $EDITOR ),  $PAGER ,  $SHEL…β”‚optional β”‚fallback vi, less, sh + Dependency β”‚ Install… β”‚ Operation + ───────────────────────────────────────────┼──────────┼───────────────────────── + xdg-open (Linux), open(1) (macOS), cygst… β”‚ base β”‚ desktop opener + file, coreutils (cp, mv, rm), xargs β”‚ base β”‚ file type, copy, move … + tar, (un)zip [atool/bsdtar for more form… β”‚ base β”‚ create, list, extract … + archivemount, fusermount(3) β”‚ optional β”‚ mount, unmount archives + sshfs, rclone https://rclone.org/, fuser… β”‚ optional β”‚ mount, unmount remotes + trash-cli β”‚ optional β”‚ trash files (default a… + vlock (Linux), bashlock (macOS), lock(1)… β”‚ optional β”‚ terminal locker (fallb… + advcpmv (Linux) (integration https://git… β”‚ optional β”‚ copy, move progress +  $VISUAL  (else  $EDITOR ),  $PAGER ,  $… β”‚ optional β”‚ fallback vi, less, sh diff --git a/ansi/testdata/TestRendererIssues/60.golden b/ansi/testdata/TestRendererIssues/60.golden index c6e009b5..73f6755d 100644 --- a/ansi/testdata/TestRendererIssues/60.golden +++ b/ansi/testdata/TestRendererIssues/60.golden @@ -1,28 +1,28 @@                                                                                  - Library β”‚Version + Library β”‚ Version ───────────────────────────────────────────────────────────────┼──────────────── - ESMF https://github.com/esmf-org/esmf β”‚v8.6.1 - FMS https://github.com/NOAA-GFDL/FMS/ β”‚2024.01.02 - netCDF https://github.com/Unidata/netcdf-c β”‚4.9.2 - netCDF Fortran https://github.com/Unidata/netcdf-fortran β”‚4.6.1 - netCDF C++ https://github.com/Unidata/netcdf-cxx4 β”‚4.3.1 - HDF5 https://portal.hdfgroup.org/display/support β”‚1.10.11 - HDF4 https://portal.hdfgroup.org/display/support β”‚4.2.16-2 - GFE https://github.com/Goddard-Fortran-Ecosystem/GFE β”‚v1.16.0 - xgboost https://github.com/dmlc/xgboost β”‚v1.6.0 - libyaml https://github.com/yaml/libyaml.git β”‚0.2.5 - antlr2 https://www.antlr2.org/ β”‚2.7.7 - GSL https://www.gnu.org/software/gsl/ β”‚2.7 - jpeg http://www.ijg.org/ β”‚9e - zlib http://www.zlib.net/ β”‚1.3.1 - szip https://support.hdfgroup.org/doc_resource/SZIP/ β”‚2.1.1 - cURL https://curl.haxx.se/ β”‚8.8.0 - UDUNITS2 https://github.com/GMAO-SI-Team/UDUNITS-2.git β”‚2.2.28 - NCO http://nco.sourceforge.net/ β”‚5.2.6 - CDO https://code.mpimet.mpg.de/projects/cdo β”‚2.3.0 - nccmp https://gitlab.com/remikz/nccmp β”‚1.9.1.0 - HDF-EOS2 https://wiki.earthdata.nasa.gov/display/DAS β”‚3.0 - HDF-EOS5 https://wiki.earthdata.nasa.gov/display/DAS β”‚2.0 - SDP Toolkit https://wiki.earthdata.nasa.gov/display/DAS β”‚5.2.20 + ESMF https://github.com/esmf-org/esmf β”‚ v8.6.1 + FMS https://github.com/NOAA-GFDL/FMS/ β”‚ 2024.01.02 + netCDF https://github.com/Unidata/netcdf-c β”‚ 4.9.2 + netCDF Fortran https://github.com/Unidata/netcdf-fortran β”‚ 4.6.1 + netCDF C++ https://github.com/Unidata/netcdf-cxx4 β”‚ 4.3.1 + HDF5 https://portal.hdfgroup.org/display/support β”‚ 1.10.11 + HDF4 https://portal.hdfgroup.org/display/support β”‚ 4.2.16-2 + GFE https://github.com/Goddard-Fortran-Ecosystem/GFE β”‚ v1.16.0 + xgboost https://github.com/dmlc/xgboost β”‚ v1.6.0 + libyaml https://github.com/yaml/libyaml.git β”‚ 0.2.5 + antlr2 https://www.antlr2.org/ β”‚ 2.7.7 + GSL https://www.gnu.org/software/gsl/ β”‚ 2.7 + jpeg http://www.ijg.org/ β”‚ 9e + zlib http://www.zlib.net/ β”‚ 1.3.1 + szip https://support.hdfgroup.org/doc_resource/SZIP/ β”‚ 2.1.1 + cURL https://curl.haxx.se/ β”‚ 8.8.0 + UDUNITS2 https://github.com/GMAO-SI-Team/UDUNITS-2.git β”‚ 2.2.28 + NCO http://nco.sourceforge.net/ β”‚ 5.2.6 + CDO https://code.mpimet.mpg.de/projects/cdo β”‚ 2.3.0 + nccmp https://gitlab.com/remikz/nccmp β”‚ 1.9.1.0 + HDF-EOS2 https://wiki.earthdata.nasa.gov/display/DAS β”‚ 3.0 + HDF-EOS5 https://wiki.earthdata.nasa.gov/display/DAS β”‚ 2.0 + SDP Toolkit https://wiki.earthdata.nasa.gov/display/DAS β”‚ 5.2.20 diff --git a/dracula.go b/dracula.go index 0b0eb56a..cf0a7dfc 100644 --- a/dracula.go +++ b/dracula.go @@ -208,9 +208,6 @@ var DraculaStyleConfig = ansi.StyleConfig{ StyleBlock: ansi.StyleBlock{ StylePrimitive: ansi.StylePrimitive{}, }, - CenterSeparator: stringPtr("β”Ό"), - ColumnSeparator: stringPtr("β”‚"), - RowSeparator: stringPtr("─"), }, DefinitionDescription: ansi.StylePrimitive{ BlockPrefix: "\n🠢 ", diff --git a/styles.go b/styles.go index 840a4732..1d735ebe 100644 --- a/styles.go +++ b/styles.go @@ -110,11 +110,7 @@ var ( Margin: uintPtr(defaultMargin), }, }, - Table: ansi.StyleTable{ - CenterSeparator: stringPtr("+"), - ColumnSeparator: stringPtr("|"), - RowSeparator: stringPtr("-"), - }, + Table: ansi.StyleTable{}, DefinitionDescription: ansi.StylePrimitive{ BlockPrefix: "\n* ", }, @@ -324,9 +320,6 @@ var ( StyleBlock: ansi.StyleBlock{ StylePrimitive: ansi.StylePrimitive{}, }, - CenterSeparator: stringPtr("β”Ό"), - ColumnSeparator: stringPtr("β”‚"), - RowSeparator: stringPtr("─"), }, DefinitionDescription: ansi.StylePrimitive{ BlockPrefix: "\n🠢 ", @@ -536,9 +529,6 @@ var ( StyleBlock: ansi.StyleBlock{ StylePrimitive: ansi.StylePrimitive{}, }, - CenterSeparator: stringPtr("β”Ό"), - ColumnSeparator: stringPtr("β”‚"), - RowSeparator: stringPtr("─"), }, DefinitionDescription: ansi.StylePrimitive{ BlockPrefix: "\n🠢 ", @@ -642,11 +632,7 @@ var ( Suffix: " ", }, }, - Table: ansi.StyleTable{ - CenterSeparator: stringPtr("β”Ό"), - ColumnSeparator: stringPtr("β”‚"), - RowSeparator: stringPtr("─"), - }, + Table: ansi.StyleTable{}, DefinitionList: ansi.StyleBlock{}, DefinitionTerm: ansi.StylePrimitive{}, DefinitionDescription: ansi.StylePrimitive{ diff --git a/styles/ascii.json b/styles/ascii.json index 048a7298..be13b917 100644 --- a/styles/ascii.json +++ b/styles/ascii.json @@ -72,11 +72,7 @@ "code_block": { "margin": 2 }, - "table": { - "center_separator": "+", - "column_separator": "|", - "row_separator": "-" - }, + "table": {}, "definition_list": {}, "definition_term": {}, "definition_description": { diff --git a/styles/dark.json b/styles/dark.json index 0ba3bd5a..47bf0afc 100644 --- a/styles/dark.json +++ b/styles/dark.json @@ -180,11 +180,7 @@ } } }, - "table": { - "center_separator": "β”Ό", - "column_separator": "β”‚", - "row_separator": "─" - }, + "table": {}, "definition_list": {}, "definition_term": {}, "definition_description": { diff --git a/styles/dracula.json b/styles/dracula.json index ef0d2927..4bbcd497 100644 --- a/styles/dracula.json +++ b/styles/dracula.json @@ -177,11 +177,7 @@ } } }, - "table": { - "center_separator": "β”Ό", - "column_separator": "β”‚", - "row_separator": "─" - }, + "table": {}, "definition_list": {}, "definition_term": {}, "definition_description": { diff --git a/styles/light.json b/styles/light.json index ced0b54b..08533592 100644 --- a/styles/light.json +++ b/styles/light.json @@ -179,11 +179,7 @@ } } }, - "table": { - "center_separator": "β”Ό", - "column_separator": "β”‚", - "row_separator": "─" - }, + "table": {}, "definition_list": {}, "definition_term": {}, "definition_description": { diff --git a/styles/pink.json b/styles/pink.json index 10070eb5..39a7f9ec 100644 --- a/styles/pink.json +++ b/styles/pink.json @@ -79,11 +79,7 @@ "background_color": "236" }, "code_block": {}, - "table": { - "center_separator": "β”Ό", - "column_separator": "β”‚", - "row_separator": "─" - }, + "table": {}, "definition_list": {}, "definition_term": {}, "definition_description": { diff --git a/styles/tokyo_night.json b/styles/tokyo_night.json index 801278c8..82dd7755 100644 --- a/styles/tokyo_night.json +++ b/styles/tokyo_night.json @@ -169,11 +169,7 @@ } } }, - "table": { - "center_separator": "β”Ό", - "column_separator": "β”‚", - "row_separator": "─" - }, + "table": {}, "definition_list": {}, "definition_term": {}, "definition_description": {