Skip to content

Commit

Permalink
(Issue LukeSavefrogs#4) test_column_ansi.sh: Add new string4 to test …
Browse files Browse the repository at this point in the history
…--ignore-line-prefix option
  • Loading branch information
LukasWillin committed Jun 15, 2023
1 parent 4c940f5 commit e54d695
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/column_ansi.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/column_ansi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ column_ansi ()
;;
-i | --ignore-line-prefix)
_ignore_line_prefix="$2";
shift 2;
shift 2 || return 1;
;;
-\? | -h |--help)
__usage
Expand Down Expand Up @@ -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";
Expand Down
12 changes: 11 additions & 1 deletion tests/test_column_ansi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 "'

Expand All @@ -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 '@'
Expand Down

0 comments on commit e54d695

Please sign in to comment.