From e54d695479a51d97ebc8dd5082654b1948194df6 Mon Sep 17 00:00:00 2001 From: LukasWillin Date: Thu, 15 Jun 2023 17:15:22 +0200 Subject: [PATCH] (Issue #4) test_column_ansi.sh: Add new string4 to test --ignore-line-prefix option --- src/column_ansi.pl | 12 ++++++------ src/column_ansi.sh | 4 ++-- tests/test_column_ansi.sh | 12 +++++++++++- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/column_ansi.pl b/src/column_ansi.pl index 3ed7f6e..1fce9d4 100644 --- a/src/column_ansi.pl +++ b/src/column_ansi.pl @@ -83,13 +83,13 @@ sub trim_ansi { foreach my $line_ref (@stdin) { my $line = $line_ref; $line =~ s/\r?\n?$//; - $line =~ s/(\\+)/$1$1/g; # escape backslashes - $line =~ s/"/\\"/g; # escape double quotes - $line =~ s/'/\\'/g; # escape single quotes # Ignore line if prefixed with argument from `-i` if ($IS_IGNORE_LINE_PREFIX_SET && (rindex $line, $IGNORE_LINE_PREFIX, 0) eq 0) { next; } + $line =~ s/(\\+)/$1$1/g; # escape backslashes + $line =~ s/"/\\"/g; # escape double quotes + $line =~ s/'/\\'/g; # escape single quotes my @columns = quotewords($INPUT_SEPARATOR, 1, $line); # split(/\Q$INPUT_SEPARATOR/, $line); my $column_index = 0; @@ -115,15 +115,15 @@ sub trim_ansi { foreach my $line_ref (@stdin) { my $line = $line_ref; $line =~ s/\r?\n?$//; - $line =~ s/(\\+)/$1$1/g; # escape backslashes - $line =~ s/"/\\"/g; # escape double quotes - $line =~ s/'/\\'/g; # escape single quotes # Print as is if prefixed with argument from `-i` but ommit the prefix if ($IS_IGNORE_LINE_PREFIX_SET && (rindex $line, $IGNORE_LINE_PREFIX, 0) eq 0) { print(substr $line, $LENGTH_IGNORE_LINE_PREFIX); print("\n"); next; } + $line =~ s/(\\+)/$1$1/g; # escape backslashes + $line =~ s/"/\\"/g; # escape double quotes + $line =~ s/'/\\'/g; # escape single quotes my @columns = quotewords($INPUT_SEPARATOR, 1, $line); # split (/\Q$INPUT_SEPARATOR/, $line); my $column_index = -1; diff --git a/src/column_ansi.sh b/src/column_ansi.sh index e0d38c5..3b2b07f 100644 --- a/src/column_ansi.sh +++ b/src/column_ansi.sh @@ -64,7 +64,7 @@ column_ansi () ;; -i | --ignore-line-prefix) _ignore_line_prefix="$2"; - shift 2; + shift 2 || return 1; ;; -\? | -h |--help) __usage @@ -141,7 +141,7 @@ column_ansi () printf "${__indent_2}${__bold}IMPORTANT${__reset}: This option is not present in the original column command.\n"; printf "\n"; printf "${__indent_1}${__red}-i${__reset}, ${__red}--ignore-line-prefix${__reset}\n"; - printf "${__indent_2}Print line as is and do not treat it as table content.\n"; + printf "${__indent_2}Print line as is and do not treat it as tabular content.\n"; printf "${__indent_2}This option allows for table sections and titles while keeping columns aligned.\n"; printf "\n"; printf "${__indent_1}${__red}-h${__reset}, ${__red}--help${__reset}\n"; diff --git a/tests/test_column_ansi.sh b/tests/test_column_ansi.sh index b00fc5c..4db72e5 100644 --- a/tests/test_column_ansi.sh +++ b/tests/test_column_ansi.sh @@ -173,6 +173,13 @@ System Up Time : \tRotation : # Expect: \Backslash | "Quotes" | Backslash\ | Separator " | " in quotes | Separator ' | ' in quotes # (Quotes should not be parsed - a separator should take effect as a separator even if in quotes) + string4_mixed_columns_and_ignored_lines=" +Date & Time\t: $(date '+%Y-%m-%d %H:%M:%S')\tOther Date & Time\t: $(date '+%Y%m%d%H%M') +\i -- This is an informative line about above dates. Note that it's not used to align tabular content. +More Date & Time\t: $(date --utc '+%Y-%m-%dT%H:%M:%S.%3N%z')\tRunning out of Date & Time\t: $(date '+%Y-%m-%dT%H:%M:%S.%3N%z') +\i -- You might find that above dates are kind of same same but different and that this notice is pointless apart from testing purposes. +"; + # string4='Dangling quotes "'"'" # Expect: Dangling quotes "' @@ -196,10 +203,13 @@ System Up Time : \tRotation : printf "\n\n" - printf "\033[1mCOLUMN (Custom - mine): Empty Data without Ansi Codes\033[0m\n" + printf "\033[1mCOLUMN (Custom - mine): Empty Data without Ansi Codes:\033[0m\n" time echo -e -n "${string2}" | column_ansi -t -s $'\t' printf "\n\n" + printf "\033[1mCOLUMN (Custom - mine): Mixed tabular data with inline comments:\033[0m\n" + time echo -e -n "${string4_mixed_columns_and_ignored_lines}" | column_ansi -t -i '\i' -s $'\t'; + printf "\n\n" # printf "\033[1mCOLUMN (Custom - mine): Backslashes and quotes\033[0m\n" # time echo -n "${string3}" | column_ansi -o " | " -s '@'