From fa61c16e59199d387ea940d42e84c3b7d886e38e Mon Sep 17 00:00:00 2001 From: LukasWillin Date: Tue, 29 Aug 2023 11:24:36 +0200 Subject: [PATCH] (enhancement #4) Add new parameters -i | --ignore-line-prefix (+ tests) --- src/column_ansi.pl | 16 +++++++++++++ src/column_ansi.sh | 16 ++++++++++--- tests/functional/parameters.test.sh | 36 +++++++++++++++++++++++++++++ tests/test_column_ansi.sh | 12 +++++++++- 4 files changed, 76 insertions(+), 4 deletions(-) diff --git a/src/column_ansi.pl b/src/column_ansi.pl index 8a9c4a0..1fce9d4 100644 --- a/src/column_ansi.pl +++ b/src/column_ansi.pl @@ -28,6 +28,12 @@ sub trim_ansi { my $ALIGN_RIGHT = $ENV{"PCOLUMN_ALIGN_RIGHT"}; my $ALIGN_CENTER = $ENV{"PCOLUMN_ALIGN_CENTER"}; my $HIDDEN_COLUMNS = $ENV{"PCOLUMN_HIDDEN_COLUMNS"}; +my $IGNORE_LINE_PREFIX = $ENV{"PCOLUMN_IGNORE_LINE_PREFIX"}; +my $LENGTH_IGNORE_LINE_PREFIX = length($IGNORE_LINE_PREFIX); +my $IS_IGNORE_LINE_PREFIX_SET = 0; +if ($LENGTH_IGNORE_LINE_PREFIX ne 0){ + $IS_IGNORE_LINE_PREFIX_SET = 1; +} # Default values for INPUT_SEPARATOR and OUTPUT_SEPARATOR if ($INPUT_SEPARATOR eq ""){ @@ -77,6 +83,10 @@ sub trim_ansi { foreach my $line_ref (@stdin) { my $line = $line_ref; $line =~ s/\r?\n?$//; + # 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 @@ -105,6 +115,12 @@ sub trim_ansi { foreach my $line_ref (@stdin) { my $line = $line_ref; $line =~ s/\r?\n?$//; + # 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 diff --git a/src/column_ansi.sh b/src/column_ansi.sh index 4d5960c..3b2b07f 100644 --- a/src/column_ansi.sh +++ b/src/column_ansi.sh @@ -32,6 +32,7 @@ column_ansi () local _align_right=" "; local _align_center=" "; local _hidden_columns="0"; + local _ignore_line_prefix=""; # From this excellent StackOverflow answer: https://stackoverflow.com/a/14203146/8965861 OPTIND=1; @@ -61,6 +62,10 @@ column_ansi () -t | --table) # Does nothing - Only for compatibility reasons shift; ;; + -i | --ignore-line-prefix) + _ignore_line_prefix="$2"; + shift 2 || return 1; + ;; -\? | -h |--help) __usage return 0; @@ -78,16 +83,17 @@ column_ansi () export PCOLUMN_ALIGN_RIGHT="${_align_right}"; export PCOLUMN_ALIGN_CENTER="${_align_center}"; export PCOLUMN_HIDDEN_COLUMNS="${_hidden_columns}"; + export PCOLUMN_IGNORE_LINE_PREFIX="${_ignore_line_prefix}"; # Call the actual Perl program perl "${__script_path}column_ansi.pl" || return 1; - unset PCOLUMN_INPUT_SEPARATOR; unset PCOLUMN_OUTPUT_SEPARATOR; unset PCOLUMN_ALIGN_RIGHT; unset PCOLUMN_ALIGN_CENTER; unset PCOLUMN_HIDDEN_COLUMNS; + unset PCOLUMN_IGNORE_LINE_PREFIX; } # shellckeck disable=SC2059 @@ -124,16 +130,20 @@ column_ansi () printf "${__indent_2}Specify the columns delimiter for table output (default is two spaces).\n"; printf "\n"; printf "${__indent_1}${__red}-R${__reset} ${__underlined}COLUMNS${__reset}, ${__red}--table-right${__reset} ${__underlined}COLUMNS${__reset}\n"; - printf "${__indent_2}Right align text in the specified columns (comma-separated).\n"; + printf "${__indent_2}Right align text in the specified columns (comma-separated, 1 based array).\n"; printf "\n"; printf "${__indent_1}${__red}-H${__reset} ${__underlined}COLUMNS${__reset}, ${__red}--table-hide${__reset} ${__underlined}COLUMNS${__reset}\n"; printf "${__indent_2}Don't print specified columns. ${__striked}The special placeholder '-' maybe be used to hide all unnamed columns (see --table-columns).${__reset}\n"; printf "${__indent_2}${__bold}IMPORTANT${__reset}: The striked part of the description is still not implemented.\n"; printf "\n"; printf "${__indent_1}${__red}-C${__reset} ${__underlined}COLUMNS${__reset}, ${__red}--table-center${__reset} ${__underlined}COLUMNS${__reset}\n"; - printf "${__indent_2}Center align text in the specified columns (comma-separated).\n"; + printf "${__indent_2}Center align text in the specified columns (comma-separated, 1 based array).\n"; 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 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"; printf "${__indent_2}Display help text and exit.\n"; printf "\n"; diff --git a/tests/functional/parameters.test.sh b/tests/functional/parameters.test.sh index e1036bb..5c9fb6f 100644 --- a/tests/functional/parameters.test.sh +++ b/tests/functional/parameters.test.sh @@ -49,6 +49,42 @@ Context "Parameter testing:" End End + Describe "should have ignore option..." + input_text="$( + %text + #|First SecondVeryVeryVeryVeryLongText Third + #|\i Ignored Line + #|First SecondShortText Third + )" + + expected_output_param_i="$( + %text + #|First SecondVeryVeryVeryVeryLongText Third + #| Ignored Line + #|First SecondShortText Third + )" + + Parameters + # By default the text gets aligned at the LEFT of the column. + # The 2nd line should be skipped and not treated as table content. + "-i '\i'" "${input_text}" "${expected_output_param_i}" + "--ignore-line-prefix '\i'" "${input_text}" "${expected_output_param_i}" + End + + Example "$1" + Set 'errexit:on' + + Data "$2" + When call eval timeout 3 "${LIBRARY_PATH}" $1 + The status should be success + if [ -n "$3" ]; then + The output should eq "$3" + else + The output should be defined + fi + End + End + Describe "should SUCCEED when passed the right arguments:" long_input_text="$( %text 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 '@'