From 51cd16b53a2fdca9aaa703e1facf3d8e1093177d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Adel=C3=A9?= <brunoadele@gmail.com> Date: Sat, 2 Dec 2023 19:56:34 +0100 Subject: [PATCH] feat: Add wrapping description text --- samples/generated_by_sh.cfwf | 111 +++++++++++----------- samples/generated_by_sh.yaml | 4 +- samples/generated_by_ts.cfwf | 111 +++++++++++----------- samples/generated_by_ts.yaml | 4 +- samples/samplescripts/generate_samples.sh | 37 ++++---- samples/samplescripts/generate_samples.ts | 38 ++++---- src/cfwf.ts | 90 ++++++++++-------- src/cfwf_test.ts | 32 ++++--- src/converter.ts | 4 +- src/types.ts | 18 ++-- src/utils.ts | 24 ++++- 11 files changed, 250 insertions(+), 223 deletions(-) diff --git a/samples/generated_by_sh.cfwf b/samples/generated_by_sh.cfwf index 5658154..1fb2d01 100644 --- a/samples/generated_by_sh.cfwf +++ b/samples/generated_by_sh.cfwf @@ -1,16 +1,15 @@ - - ___ _____ ______ _____ - / _ \ |_ _|| ___ \ |_ _| - / /_\ \ | | | |_/ / | | ___ _ _ _ __ - | _ | | | | __/ | | / _ \ | | | || '__| - | | | | | | | | | | | (_) || |_| || | - \_| |_/ \_/ \_| \_/ \___/ \__,_||_| - - ┈┈┈ -Here is a list of the best tennis players in the ATP rankings -from 2012 to 2022, as well as the list of winners of the -4 major Grand Slam tournaments. - ┄┄┄ + + ___ _____ ______ _____ + / _ \ |_ _|| ___ \ |_ _| + / /_\ \ | | | |_/ / | | ___ _ _ _ __ + | _ | | | | __/ | | / _ \ | | | || '__| + | | | | | | | | | | | (_) || |_| || | + \_| |_/ \_/ \_| \_/ \___/ \__,_||_| + + ┈┈┈ +Here is a list of the best tennis players in the ATP rankings from 2012 to 2022, as well as the list of winners of the 4 +major Grand Slam tournaments. + ┄┄┄ players The best players (number of winning matches) beetween 2012-2022 @@ -30,95 +29,91 @@ CRO Marin Cilic R 198 1988 14 RUS Andrey Rublev R 188 1997 13 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -The Australian Open is a tennis tournament held annually at Melbourne Park -in Melbourne, Victoria, Australia. The tournament is the first of the -four Grand Slam tennis events held each year. +The Australian Open is a tennis tournament held annually at Melbourne Park in Melbourne, Victoria, Australia. The +tournament is the first of the four Grand Slam tennis events held each year. australian_open Australian Open winners beetween 2012-2022 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -year tourney name birth nat winner +year tourney name birth nat winner ──── ─────────────── ───────── ────────────── -2022 Australian Open ESP Rafael Nadal +2022 Australian Open ESP Rafael Nadal 2021 Australian Open SRB Novak Djokovic 2020 Australian Open SRB Novak Djokovic 2019 Australian Open SRB Novak Djokovic -2018 Australian Open SUI Roger Federer -2017 Australian Open SUI Roger Federer +2018 Australian Open SUI Roger Federer +2017 Australian Open SUI Roger Federer 2016 Australian Open SRB Novak Djokovic 2015 Australian Open SRB Novak Djokovic -2014 Australian Open SUI Stan Wawrinka +2014 Australian Open SUI Stan Wawrinka 2013 Australian Open SRB Novak Djokovic 2012 Australian Open SRB Novak Djokovic ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -The major tennis tournament held over two weeks at the Stade Roland Garros -in Paris, France, beginning in late May each year. The tournament and venue are -named after the French aviator Roland Garros. The French Open is the premier -clay court championship in the world and the only Grand Slam tournament -currently held on this surface. +The major tennis tournament held over two weeks at the Stade Roland Garros in Paris, France, beginning in late May each +year. The tournament and venue are named after the French aviator Roland Garros. The French Open is the premier clay +court championship in the world and the only Grand Slam tournament currently held on this surface. roland_garros Roland Garros winners beetween 2012-2022 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -year tourney name birth nat winner +year tourney name birth nat winner ──── ───────────── ───────── ────────────── -2022 Roland Garros ESP Rafael Nadal +2022 Roland Garros ESP Rafael Nadal 2021 Roland Garros SRB Novak Djokovic -2020 Roland Garros ESP Rafael Nadal -2019 Roland Garros ESP Rafael Nadal -2018 Roland Garros ESP Rafael Nadal -2017 Roland Garros ESP Rafael Nadal +2020 Roland Garros ESP Rafael Nadal +2019 Roland Garros ESP Rafael Nadal +2018 Roland Garros ESP Rafael Nadal +2017 Roland Garros ESP Rafael Nadal 2016 Roland Garros SRB Novak Djokovic -2015 Roland Garros SUI Stan Wawrinka -2014 Roland Garros ESP Rafael Nadal -2013 Roland Garros ESP Rafael Nadal -2012 Roland Garros ESP Rafael Nadal +2015 Roland Garros SUI Stan Wawrinka +2014 Roland Garros ESP Rafael Nadal +2013 Roland Garros ESP Rafael Nadal +2012 Roland Garros ESP Rafael Nadal ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -The US Open Tennis Championships, commonly called the US Open, is a hardcourt tennis -tournament held annually in Queens, New York. Since 1987, the US Open has been -chronologically the fourth and final Grand Slam tournament of the year. +The US Open Tennis Championships, commonly called the US Open, is a hardcourt tennis tournament held annually in Queens, +New York. Since 1987, the US Open has been chronologically the fourth and final Grand Slam tournament of the year. us_open US Open winners beetween 2012-2022 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -year tourney name birth nat winner +year tourney name birth nat winner ──── ──────────── ───────── ─────────────── -2022 Us Open ESP Carlos Alcaraz +2022 Us Open ESP Carlos Alcaraz 2021 Us Open RUS Daniil Medvedev -2020 Us Open AUT Dominic Thiem -2019 US Open ESP Rafael Nadal -2018 US Open SRB Novak Djokovic -2017 US Open ESP Rafael Nadal -2016 US Open SUI Stan Wawrinka -2015 US Open SRB Novak Djokovic -2014 US Open CRO Marin Cilic -2013 US Open ESP Rafael Nadal -2012 US Open GBR Andy Murray +2020 Us Open AUT Dominic Thiem +2019 US Open ESP Rafael Nadal +2018 US Open SRB Novak Djokovic +2017 US Open ESP Rafael Nadal +2016 US Open SUI Stan Wawrinka +2015 US Open SRB Novak Djokovic +2014 US Open CRO Marin Cilic +2013 US Open ESP Rafael Nadal +2012 US Open GBR Andy Murray ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -The Championships, commonly known simply as Wimbledon, is the oldest tennis tournament -in the world and is regarded by many as the most prestigious. -It has been held at the All England Lawn Tennis and Croquet Club in Wimbledon, London, since 1877 +The Championships, commonly known simply as Wimbledon, is the oldest tennis tournament in the world and is regarded by +many as the most prestigious. It has been held at the All England Lawn Tennis and Croquet Club in Wimbledon, London, +since 1877 wimbledon Wimbledon winners beetween 2012-2022 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -year tourney name birth nat winner +year tourney name birth nat winner ──── ──────────── ───────── ────────────── 2022 Wimbledon SRB Novak Djokovic 2021 Wimbledon SRB Novak Djokovic 2019 Wimbledon SRB Novak Djokovic 2018 Wimbledon SRB Novak Djokovic -2017 Wimbledon SUI Roger Federer -2016 Wimbledon GBR Andy Murray +2017 Wimbledon SUI Roger Federer +2016 Wimbledon GBR Andy Murray 2015 Wimbledon SRB Novak Djokovic 2014 Wimbledon SRB Novak Djokovic -2013 Wimbledon GBR Andy Murray -2012 Wimbledon SUI Roger Federer +2013 Wimbledon GBR Andy Murray +2012 Wimbledon SUI Roger Federer ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ diff --git a/samples/generated_by_sh.yaml b/samples/generated_by_sh.yaml index 5ae3d7f..1cb3864 100644 --- a/samples/generated_by_sh.yaml +++ b/samples/generated_by_sh.yaml @@ -1,10 +1,10 @@ --- dataset: metadatas: - generated_with: 'cfwf@0.1.0 - https://github.com/badele/cfwf' + generated_with: 'cfwf@0.2.0 - https://github.com/badele/cfwf' sources: - 'https://github.com/JeffSackmann/tennis_atp' - title: ' ATP Tour' + title: ATP Tour tables: - metadatas: aligns: diff --git a/samples/generated_by_ts.cfwf b/samples/generated_by_ts.cfwf index 5658154..1fb2d01 100644 --- a/samples/generated_by_ts.cfwf +++ b/samples/generated_by_ts.cfwf @@ -1,16 +1,15 @@ - - ___ _____ ______ _____ - / _ \ |_ _|| ___ \ |_ _| - / /_\ \ | | | |_/ / | | ___ _ _ _ __ - | _ | | | | __/ | | / _ \ | | | || '__| - | | | | | | | | | | | (_) || |_| || | - \_| |_/ \_/ \_| \_/ \___/ \__,_||_| - - ┈┈┈ -Here is a list of the best tennis players in the ATP rankings -from 2012 to 2022, as well as the list of winners of the -4 major Grand Slam tournaments. - ┄┄┄ + + ___ _____ ______ _____ + / _ \ |_ _|| ___ \ |_ _| + / /_\ \ | | | |_/ / | | ___ _ _ _ __ + | _ | | | | __/ | | / _ \ | | | || '__| + | | | | | | | | | | | (_) || |_| || | + \_| |_/ \_/ \_| \_/ \___/ \__,_||_| + + ┈┈┈ +Here is a list of the best tennis players in the ATP rankings from 2012 to 2022, as well as the list of winners of the 4 +major Grand Slam tournaments. + ┄┄┄ players The best players (number of winning matches) beetween 2012-2022 @@ -30,95 +29,91 @@ CRO Marin Cilic R 198 1988 14 RUS Andrey Rublev R 188 1997 13 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -The Australian Open is a tennis tournament held annually at Melbourne Park -in Melbourne, Victoria, Australia. The tournament is the first of the -four Grand Slam tennis events held each year. +The Australian Open is a tennis tournament held annually at Melbourne Park in Melbourne, Victoria, Australia. The +tournament is the first of the four Grand Slam tennis events held each year. australian_open Australian Open winners beetween 2012-2022 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -year tourney name birth nat winner +year tourney name birth nat winner ──── ─────────────── ───────── ────────────── -2022 Australian Open ESP Rafael Nadal +2022 Australian Open ESP Rafael Nadal 2021 Australian Open SRB Novak Djokovic 2020 Australian Open SRB Novak Djokovic 2019 Australian Open SRB Novak Djokovic -2018 Australian Open SUI Roger Federer -2017 Australian Open SUI Roger Federer +2018 Australian Open SUI Roger Federer +2017 Australian Open SUI Roger Federer 2016 Australian Open SRB Novak Djokovic 2015 Australian Open SRB Novak Djokovic -2014 Australian Open SUI Stan Wawrinka +2014 Australian Open SUI Stan Wawrinka 2013 Australian Open SRB Novak Djokovic 2012 Australian Open SRB Novak Djokovic ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -The major tennis tournament held over two weeks at the Stade Roland Garros -in Paris, France, beginning in late May each year. The tournament and venue are -named after the French aviator Roland Garros. The French Open is the premier -clay court championship in the world and the only Grand Slam tournament -currently held on this surface. +The major tennis tournament held over two weeks at the Stade Roland Garros in Paris, France, beginning in late May each +year. The tournament and venue are named after the French aviator Roland Garros. The French Open is the premier clay +court championship in the world and the only Grand Slam tournament currently held on this surface. roland_garros Roland Garros winners beetween 2012-2022 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -year tourney name birth nat winner +year tourney name birth nat winner ──── ───────────── ───────── ────────────── -2022 Roland Garros ESP Rafael Nadal +2022 Roland Garros ESP Rafael Nadal 2021 Roland Garros SRB Novak Djokovic -2020 Roland Garros ESP Rafael Nadal -2019 Roland Garros ESP Rafael Nadal -2018 Roland Garros ESP Rafael Nadal -2017 Roland Garros ESP Rafael Nadal +2020 Roland Garros ESP Rafael Nadal +2019 Roland Garros ESP Rafael Nadal +2018 Roland Garros ESP Rafael Nadal +2017 Roland Garros ESP Rafael Nadal 2016 Roland Garros SRB Novak Djokovic -2015 Roland Garros SUI Stan Wawrinka -2014 Roland Garros ESP Rafael Nadal -2013 Roland Garros ESP Rafael Nadal -2012 Roland Garros ESP Rafael Nadal +2015 Roland Garros SUI Stan Wawrinka +2014 Roland Garros ESP Rafael Nadal +2013 Roland Garros ESP Rafael Nadal +2012 Roland Garros ESP Rafael Nadal ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -The US Open Tennis Championships, commonly called the US Open, is a hardcourt tennis -tournament held annually in Queens, New York. Since 1987, the US Open has been -chronologically the fourth and final Grand Slam tournament of the year. +The US Open Tennis Championships, commonly called the US Open, is a hardcourt tennis tournament held annually in Queens, +New York. Since 1987, the US Open has been chronologically the fourth and final Grand Slam tournament of the year. us_open US Open winners beetween 2012-2022 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -year tourney name birth nat winner +year tourney name birth nat winner ──── ──────────── ───────── ─────────────── -2022 Us Open ESP Carlos Alcaraz +2022 Us Open ESP Carlos Alcaraz 2021 Us Open RUS Daniil Medvedev -2020 Us Open AUT Dominic Thiem -2019 US Open ESP Rafael Nadal -2018 US Open SRB Novak Djokovic -2017 US Open ESP Rafael Nadal -2016 US Open SUI Stan Wawrinka -2015 US Open SRB Novak Djokovic -2014 US Open CRO Marin Cilic -2013 US Open ESP Rafael Nadal -2012 US Open GBR Andy Murray +2020 Us Open AUT Dominic Thiem +2019 US Open ESP Rafael Nadal +2018 US Open SRB Novak Djokovic +2017 US Open ESP Rafael Nadal +2016 US Open SUI Stan Wawrinka +2015 US Open SRB Novak Djokovic +2014 US Open CRO Marin Cilic +2013 US Open ESP Rafael Nadal +2012 US Open GBR Andy Murray ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -The Championships, commonly known simply as Wimbledon, is the oldest tennis tournament -in the world and is regarded by many as the most prestigious. -It has been held at the All England Lawn Tennis and Croquet Club in Wimbledon, London, since 1877 +The Championships, commonly known simply as Wimbledon, is the oldest tennis tournament in the world and is regarded by +many as the most prestigious. It has been held at the All England Lawn Tennis and Croquet Club in Wimbledon, London, +since 1877 wimbledon Wimbledon winners beetween 2012-2022 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -year tourney name birth nat winner +year tourney name birth nat winner ──── ──────────── ───────── ────────────── 2022 Wimbledon SRB Novak Djokovic 2021 Wimbledon SRB Novak Djokovic 2019 Wimbledon SRB Novak Djokovic 2018 Wimbledon SRB Novak Djokovic -2017 Wimbledon SUI Roger Federer -2016 Wimbledon GBR Andy Murray +2017 Wimbledon SUI Roger Federer +2016 Wimbledon GBR Andy Murray 2015 Wimbledon SRB Novak Djokovic 2014 Wimbledon SRB Novak Djokovic -2013 Wimbledon GBR Andy Murray -2012 Wimbledon SUI Roger Federer +2013 Wimbledon GBR Andy Murray +2012 Wimbledon SUI Roger Federer ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ diff --git a/samples/generated_by_ts.yaml b/samples/generated_by_ts.yaml index 5ae3d7f..1cb3864 100644 --- a/samples/generated_by_ts.yaml +++ b/samples/generated_by_ts.yaml @@ -1,10 +1,10 @@ --- dataset: metadatas: - generated_with: 'cfwf@0.1.0 - https://github.com/badele/cfwf' + generated_with: 'cfwf@0.2.0 - https://github.com/badele/cfwf' sources: - 'https://github.com/JeffSackmann/tennis_atp' - title: ' ATP Tour' + title: ATP Tour tables: - metadatas: aligns: diff --git a/samples/samplescripts/generate_samples.sh b/samples/samplescripts/generate_samples.sh index b2fc134..b006492 100755 --- a/samples/samplescripts/generate_samples.sh +++ b/samples/samplescripts/generate_samples.sh @@ -7,11 +7,10 @@ $CFWF dataset init -c /tmp/config.json # Dataset definition $CFWF dataset set \ -c /tmp/config.json \ - -t " ATP Tour" \ + -t "ATP Tour" \ -d "$(cat << EOD -Here is a list of the best tennis players in the ATP rankings -from 2012 to 2022, as well as the list of winners of the -4 major Grand Slam tournaments. +Here is a list of the best tennis players in the ATP rankings from 2012 to\ + 2022, as well as the list of winners of the 4 major Grand Slam tournaments. EOD )" \ -m '{ "sources": [ "https://github.com/JeffSackmann/tennis_atp" ]}' @@ -32,9 +31,9 @@ $CFWF dataset table add \ -t "australian_open" \ -s "Australian Open winners beetween 2012-2022" \ -d "$(cat << EOD -The Australian Open is a tennis tournament held annually at Melbourne Park -in Melbourne, Victoria, Australia. The tournament is the first of the -four Grand Slam tennis events held each year. +The Australian Open is a tennis tournament held annually at Melbourne Park in\ + Melbourne, Victoria, Australia. The tournament is the first of the four Grand\ + Slam tennis events held each year. EOD )" \ -l "year,tourney name,birth nat,winner" \ @@ -48,11 +47,11 @@ $CFWF dataset table add \ -t "roland_garros" \ -s "Roland Garros winners beetween 2012-2022" \ -d "$(cat << EOD -The major tennis tournament held over two weeks at the Stade Roland Garros -in Paris, France, beginning in late May each year. The tournament and venue are -named after the French aviator Roland Garros. The French Open is the premier -clay court championship in the world and the only Grand Slam tournament -currently held on this surface. +The major tennis tournament held over two weeks at the Stade Roland Garros in\ + Paris, France, beginning in late May each year. The tournament and venue are\ + named after the French aviator Roland Garros. The French Open is the premier\ + clay court championship in the world and the only Grand Slam tournament\ + currently held on this surface. EOD )" \ -l "year,tourney name,birth nat,winner" \ @@ -66,9 +65,10 @@ $CFWF dataset table add \ -t "us_open" \ -s "US Open winners beetween 2012-2022" \ -d "$(cat << EOD -The US Open Tennis Championships, commonly called the US Open, is a hardcourt tennis -tournament held annually in Queens, New York. Since 1987, the US Open has been -chronologically the fourth and final Grand Slam tournament of the year. +The US Open Tennis Championships, commonly called the US Open, is a hardcourt\ + tennis tournament held annually in Queens, New York. Since 1987, the US Open\ + has been chronologically the fourth and final Grand Slam tournament of the\ + year. EOD )" \ -l "year,tourney name,birth nat,winner" \ @@ -82,9 +82,10 @@ $CFWF dataset table add \ -t "wimbledon" \ -s "Wimbledon winners beetween 2012-2022" \ -d "$(cat << EOD -The Championships, commonly known simply as Wimbledon, is the oldest tennis tournament -in the world and is regarded by many as the most prestigious. -It has been held at the All England Lawn Tennis and Croquet Club in Wimbledon, London, since 1877 +The Championships, commonly known simply as Wimbledon, is the oldest tennis\ + tournament in the world and is regarded by many as the most prestigious. It has\ + been held at the All England Lawn Tennis and Croquet Club in Wimbledon, London,\ + since 1877 EOD )" \ -l "year,tourney name,birth nat,winner" \ diff --git a/samples/samplescripts/generate_samples.ts b/samples/samplescripts/generate_samples.ts index 9854b62..eb1fdfa 100644 --- a/samples/samplescripts/generate_samples.ts +++ b/samples/samplescripts/generate_samples.ts @@ -5,10 +5,10 @@ import { readDecodedCSVFile } from "../../src/converter.ts"; const samples = new CFWF({}); samples.setDatasetProperties({ - title: " ATP Tour", - description: "Here is a list of the best tennis players in the ATP rankings\n\ -from 2012 to 2022, as well as the list of winners of the\n\ -4 major Grand Slam tournaments.", + title: "ATP Tour", + description: "Here is a list of the best tennis players in the ATP rankings\ + from 2012 to 2022, as well as the list of winners of the 4 major Grand Slam\ + tournaments.", metadatas: { sources: [ "https://github.com/JeffSackmann/tennis_atp", @@ -38,9 +38,9 @@ samples.addTable({ tablename: "australian_open", subtitle: "Australian Open winners beetween 2012-2022", description: -"The Australian Open is a tennis tournament held annually at Melbourne Park\n\ -in Melbourne, Victoria, Australia. The tournament is the first of the\n\ -four Grand Slam tennis events held each year.", +"The Australian Open is a tennis tournament held annually at Melbourne Park in\ + Melbourne, Victoria, Australia. The tournament is the first of the four Grand\ + Slam tennis events held each year.", columns: ["year", "tourney name", "birth nat", "winner"], rows: australian_open.rows, metadatas: { @@ -56,11 +56,11 @@ samples.addTable({ tablename: "roland_garros", subtitle: "Roland Garros winners beetween 2012-2022", description: -"The major tennis tournament held over two weeks at the Stade Roland Garros\n\ -in Paris, France, beginning in late May each year. The tournament and venue are\n\ -named after the French aviator Roland Garros. The French Open is the premier\n\ -clay court championship in the world and the only Grand Slam tournament\n\ -currently held on this surface.", +"The major tennis tournament held over two weeks at the Stade Roland Garros in\ + Paris, France, beginning in late May each year. The tournament and venue are\ + named after the French aviator Roland Garros. The French Open is the premier\ + clay court championship in the world and the only Grand Slam tournament\ + currently held on this surface.", columns: ["year", "tourney name", "birth nat", "winner"], rows: rgarros.rows, metadatas: { @@ -76,9 +76,10 @@ samples.addTable({ tablename: "us_open", subtitle: "US Open winners beetween 2012-2022", description: -"The US Open Tennis Championships, commonly called the US Open, is a hardcourt tennis\n\ -tournament held annually in Queens, New York. Since 1987, the US Open has been\n\ -chronologically the fourth and final Grand Slam tournament of the year.", +"The US Open Tennis Championships, commonly called the US Open, is a hardcourt\ + tennis tournament held annually in Queens, New York. Since 1987, the US Open\ + has been chronologically the fourth and final Grand Slam tournament of the\ + year.", columns: ["year", "tourney name", "birth nat", "winner"], rows: usopen.rows, metadatas: { @@ -94,9 +95,10 @@ samples.addTable({ tablename: "wimbledon", subtitle: "Wimbledon winners beetween 2012-2022", description: -"The Championships, commonly known simply as Wimbledon, is the oldest tennis tournament\n\ -in the world and is regarded by many as the most prestigious.\n\ -It has been held at the All England Lawn Tennis and Croquet Club in Wimbledon, London, since 1877", +"The Championships, commonly known simply as Wimbledon, is the oldest tennis\ + tournament in the world and is regarded by many as the most prestigious. It\ + has been held at the All England Lawn Tennis and Croquet Club in Wimbledon,\ + London, since 1877", columns: ["year", "tourney name", "birth nat", "winner"], rows: wimbledon.rows, metadatas: { diff --git a/src/cfwf.ts b/src/cfwf.ts index e918442..2f5e25b 100644 --- a/src/cfwf.ts +++ b/src/cfwf.ts @@ -7,7 +7,14 @@ import { } from "./types.ts"; import { AvailableFonts } from "https://deno.land/x/deno_figlet@1.0.0/src/types.ts"; import { existsSync } from "https://deno.land/std@0.205.0/fs/exists.ts"; -import { align, CHARMARKERS, getMaxWidth, max, searchMarker } from "./utils.ts"; +import { + align, + DEFAULTOPTIONS, + getMaxWidth, + max, + searchMarker, + wrapText, +} from "./utils.ts"; import { modfmt, modyaml, text } from "../deps.ts"; import { version } from "./version.ts"; @@ -18,7 +25,7 @@ export class CFWF { // Title generatedtitle: string[]; - maxwidthtitle: number; + // maxwidthtitle: number; constructor(dsconfig: CFWFDataset) { dsconfig.dataset = dsconfig.dataset || {}; @@ -26,7 +33,7 @@ export class CFWF { this.config = dsconfig; this.generatedtitle = []; - this.maxwidthtitle = 0; + // this.maxwidthtitle = 0; } async _generateTitle(): Promise<void> { @@ -37,18 +44,26 @@ export class CFWF { let { title, metadatas } = dataset; metadatas = metadatas || {}; - const font = metadatas.font as AvailableFonts ?? "doom"; - const removetitlelines = metadatas.removetitlelines ?? 2; + const font = metadatas.font as AvailableFonts ?? DEFAULTOPTIONS.font; + const removetitlelines = metadatas.removetitlelines ?? + DEFAULTOPTIONS.removetitlelines; + const width = metadatas.width ?? DEFAULTOPTIONS.width; const txttitle = await text(title ?? "", font); const ageneratedtitle = txttitle.split("\n").slice( 0, -removetitlelines, ); - const maxwidthtitle = getMaxWidth(ageneratedtitle); + + const maxwidth = getMaxWidth(txttitle.split("\n")); + const nbspace = (width - maxwidth) / 2; + + for (let idx = 0; idx < ageneratedtitle.length; idx++) { + ageneratedtitle[idx] = " ".repeat(nbspace) + + ageneratedtitle[idx].trimEnd(); + } this.generatedtitle = ageneratedtitle; - this.maxwidthtitle = maxwidthtitle; } setDatasetProperties(params: DatasetType): void { @@ -86,12 +101,12 @@ export class CFWF { } importCFWF(content: string): void { - const chartitlesep = CHARMARKERS.chartitlesep ?? "┈"; - const chardescsep = CHARMARKERS.chardescsep ?? "┄"; - const chartabletop = CHARMARKERS.chartabletop ?? "━"; - const chartablemiddle = CHARMARKERS.chartablemiddle ?? "─"; - const chartablebottom = CHARMARKERS.chartablebottom ?? "━"; - const charyamlsep = CHARMARKERS.charyamlsep ?? "╌"; + const chartitlesep = DEFAULTOPTIONS.chartitlesep; + const chardescsep = DEFAULTOPTIONS.chardescsep; + const chartabletop = DEFAULTOPTIONS.chartabletop; + const chartablemiddle = DEFAULTOPTIONS.chartablemiddle; + const chartablebottom = DEFAULTOPTIONS.chartablebottom; + const charyamlsep = DEFAULTOPTIONS.charyamlsep; let lastmarkerpos = 0; let tabletopmarkerpos = 0; @@ -118,7 +133,7 @@ export class CFWF { dataset.description = lines.slice( titlemarkerpos + 1, descmarkerpos, - ).join("\n"); + ).join(" "); } let hastable = searchMarker(lines, chartabletop, lastmarkerpos); @@ -146,8 +161,7 @@ export class CFWF { table.description = lines.slice( lastmarkerpos + 1, tabletablenamepos - 1, - ) - .join("\n"); + ).join(" "); const headerline = lines[tabletopmarkerpos + 1]; const widthline = lines[tabletopmarkerpos + 2]; @@ -200,16 +214,15 @@ export class CFWF { } async outputCFWF(separate: boolean): Promise<FormatCFWF> { - const padding = CHARMARKERS.padding ?? 3; - const chartitlesep = CHARMARKERS.chartitlesep ?? "┈"; - const chardescsep = CHARMARKERS.chardescsep ?? "┄"; - const chartabletop = CHARMARKERS.chartabletop ?? "━"; - const chartablemiddle = CHARMARKERS.chartablemiddle ?? "─"; - const chartablebottom = CHARMARKERS.chartablebottom ?? "━"; - const charyamlsep = CHARMARKERS.charyamlsep ?? "╌"; + const padding = DEFAULTOPTIONS.padding; + const chartitlesep = DEFAULTOPTIONS.chartitlesep; + const chardescsep = DEFAULTOPTIONS.chardescsep; + const chartabletop = DEFAULTOPTIONS.chartabletop; + const chartablemiddle = DEFAULTOPTIONS.chartablemiddle; + const chartablebottom = DEFAULTOPTIONS.chartablebottom; + const charyamlsep = DEFAULTOPTIONS.charyamlsep; const lines: string[] = []; - let maxwidthdescription = 0; const config = structuredClone(this.config); @@ -227,7 +240,8 @@ export class CFWF { tables = tables || []; const metadatas = dataset.metadatas || {}; - let { title, generatedtitle, description } = dataset; + const { title, generatedtitle, description } = dataset; + const datasetwidth = metadatas.width ?? DEFAULTOPTIONS.width; // Compute max dataset title width size if (title && title.length > 0) { @@ -235,32 +249,24 @@ export class CFWF { } if (generatedtitle) { this.generatedtitle = generatedtitle.split("\n"); - this.maxwidthtitle = getMaxWidth(this.generatedtitle); } - // Compute max dataset description width size - if (description && description.length > 0) { - description = description.replaceAll("\\n", "\n"); - const adescription = description.split("\n"); - maxwidthdescription = getMaxWidth(adescription); - } - // Compute title or description max size - const maxwidthline = max(this.maxwidthtitle, maxwidthdescription); - if (this.generatedtitle && this.generatedtitle.length > 0) { lines.push(this.generatedtitle.join("\n")); lines.push( align( "center", chartitlesep.repeat(3), - maxwidthline, - ), + datasetwidth, + ).trimEnd(), ); } if (description && description.length > 0) { - lines.push(description); - lines.push(align("center", chardescsep.repeat(3), maxwidthline)); + lines.push(wrapText(description, datasetwidth).trimEnd()); + lines.push( + align("center", chardescsep.repeat(3), datasetwidth).trimEnd(), + ); } // Generate tables @@ -344,7 +350,7 @@ export class CFWF { // add tablename and subtitle if (table.description) { lines.push(""); - lines.push(table.description); + lines.push(wrapText(table.description, datasetwidth).trimEnd()); } lines.push(""); @@ -382,7 +388,7 @@ export class CFWF { } // add middle header line separator - lines.push(headers.join("")); + lines.push(headers.join("").trimEnd()); lines.push(middlelineheader.join("")); // Add rows datas @@ -406,7 +412,7 @@ export class CFWF { ); } } - lines.push(line.join("")); + lines.push(line.join("").trimEnd()); } // Add bottom line header diff --git a/src/cfwf_test.ts b/src/cfwf_test.ts index 001be0b..9f25040 100644 --- a/src/cfwf_test.ts +++ b/src/cfwf_test.ts @@ -2,7 +2,7 @@ import { CFWF } from "./cfwf.ts"; import { assertEquals } from "../test_deps.ts"; import { modyaml } from "../deps.ts"; -import { CHARMARKERS, searchMarker } from "./utils.ts"; +import { DEFAULTOPTIONS, searchMarker } from "./utils.ts"; import { version } from "./version.ts"; import { readTextCFWFFile } from "./converter.ts"; @@ -12,9 +12,9 @@ const title = "this is a title"; const comment = "this is a comment"; const footer = `cfwf@${version} - https://github.com/badele/cfwf`; -const chartitlesep = CHARMARKERS.chartitlesep || ""; -const chardescsep = CHARMARKERS.chardescsep || ""; -const charyamlsep = CHARMARKERS.charyamlsep || ""; +const chartitlesep = DEFAULTOPTIONS.chartitlesep; +const chardescsep = DEFAULTOPTIONS.chardescsep; +const charyamlsep = DEFAULTOPTIONS.charyamlsep; const datas_array = [ [1, 1, 1, 1, 1], @@ -215,8 +215,8 @@ test("Title & comment", async () => { }); test("Columns and headers columns size from generated_by_sh.cfwf", async () => { - const chartabletop = CHARMARKERS.chartabletop || "━"; - const chartablebottom = CHARMARKERS.chartablebottom || "━"; + const chartabletop = DEFAULTOPTIONS.chartabletop || "━"; + const chartablebottom = DEFAULTOPTIONS.chartablebottom || "━"; const content = await readTextCFWFFile("samples/generated_by_sh.cfwf"); const lines = content.split("\n"); @@ -226,8 +226,8 @@ test("Columns and headers columns size from generated_by_sh.cfwf", async () => { const yamlmarkerpos = searchMarker(lines, charyamlsep || ""); assertEquals(8, titlemarkerpos); - assertEquals(12, descmarkerpos); - assertEquals(125, yamlmarkerpos); + assertEquals(11, descmarkerpos); + assertEquals(120, yamlmarkerpos); let beginheadermarker = searchMarker(lines, chartabletop || ""); let endheadermarker = searchMarker( @@ -268,7 +268,7 @@ test("Columns and headers columns size from generated_by_sh.cfwf", async () => { assertEquals(table[table.length - 1].length, table[0].length); assertEquals( - "year tourney name birth nat winner ", + "year tourney name birth nat winner", table[1], ); assertEquals( @@ -276,7 +276,7 @@ test("Columns and headers columns size from generated_by_sh.cfwf", async () => { table[2], ); assertEquals( - "2022 Australian Open ESP Rafael Nadal ", + "2022 Australian Open ESP Rafael Nadal", table[3], ); }); @@ -401,10 +401,13 @@ test("Number & array", async () => { assertEquals(10, descmarkerpos); assertEquals(27, yamlmarkerpos); - const beginheadermarker = searchMarker(lines, CHARMARKERS.chartabletop || ""); + const beginheadermarker = searchMarker( + lines, + DEFAULTOPTIONS.chartabletop, + ); assertEquals( - "Id larger column right center left ", + "Id larger column right center left", lines[beginheadermarker + 1], ); assertEquals( @@ -412,7 +415,7 @@ test("Number & array", async () => { lines[beginheadermarker + 2], ); assertEquals( - " 3 12.430 333 33333 333 ", + " 3 12.430 333 33333 333", lines[beginheadermarker + 5], ); }); @@ -505,5 +508,6 @@ test("Reader & regenerate", async () => { await samples.saveCFWF("samples/samples_regenerated.cfwf", false); const newcontent = await readTextCFWFFile("samples/samples_regenerated.cfwf"); - assertEquals(newcontent, content); + + assertEquals(content, newcontent); }); diff --git a/src/converter.ts b/src/converter.ts index b0522fc..3367562 100644 --- a/src/converter.ts +++ b/src/converter.ts @@ -1,6 +1,6 @@ import { TableType } from "./types.ts"; import * as modcsv from "https://deno.land/std@0.204.0/csv/mod.ts"; -import { CHARMARKERS, readTextFile } from "./utils.ts"; +import { DEFAULTOPTIONS, readTextFile } from "./utils.ts"; import * as path from "https://deno.land/std@0.205.0/path/mod.ts"; import { existsSync } from "https://deno.land/std@0.205.0/fs/exists.ts"; import { CFWF } from "./cfwf.ts"; @@ -41,7 +41,7 @@ export async function readCFWFFile(filename: string): Promise<CFWF> { } export async function readTextCFWFFile(filename: string): Promise<string> { - const charyamlsep = CHARMARKERS.charyamlsep ?? "╌"; + const charyamlsep = DEFAULTOPTIONS.charyamlsep ?? "╌"; const metaname = filename.replace(".cfwf", ".yaml"); if (existsSync(metaname) === true) { diff --git a/src/types.ts b/src/types.ts index 78a40d1..d9e0e70 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,14 +6,15 @@ export type FormatCFWF = { }; export type CFWFOptions = { - padding?: number; - chartitlesep?: string; - chardescsep?: string; - chartabletop?: string; - chartablemiddle?: string; - chartablebottom?: string; - charyamlsep?: string; - removetitleline?: number; + width: number; + padding: number; + chartitlesep: string; + chardescsep: string; + chartabletop: string; + chartablemiddle: string; + chartablebottom: string; + charyamlsep: string; + removetitlelines: number; // deno-lint-ignore no-explicit-any metas?: any; font?: string; @@ -26,6 +27,7 @@ export type DatasetType = { description?: string; tables?: Record<string, TableType>; metadatas?: { + width?: number; orders?: string[]; font?: string; removetitlelines?: number; diff --git a/src/utils.ts b/src/utils.ts index b6fd2ea..7a5ce3f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,10 @@ import { Align, CFWFOptions } from "./types.ts"; -export const CHARMARKERS: CFWFOptions = { +export const DEFAULTOPTIONS: CFWFOptions = { + width: 120, padding: 3, + removetitlelines: 2, + font: "doom", chartitlesep: "┈", chardescsep: "┄", chartabletop: "━", @@ -48,6 +51,25 @@ export function getMaxWidth(lines: string[]): number { return maxmaintitlesize; } +export function wrapText(text: string, width: number): string { + const lines: string[] = []; + const words = text.split(" "); + + let line = ""; + for (let idx = 0; idx < words.length; idx++) { + const word = words[idx]; + + if (line.length + word.length > width) { + lines.push(line.trimEnd()); + line = ""; + } + line += word + " "; + } + lines.push(line.trimEnd()); + + return lines.join("\n"); +} + export function align(align: Align, text: string, maxsize: number): string { switch (align) { case "center": {