Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #4 (enhancement) Print line as is instead of interpreting it as column #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/column_ansi.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""){
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions src/column_ansi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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";
Expand Down
36 changes: 36 additions & 0 deletions tests/functional/parameters.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
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