Skip to content

Commit

Permalink
WIP Issue LukeSavefrogs#4 (enhancement) Print line as is instead of i…
Browse files Browse the repository at this point in the history
…nterpreting it as column
  • Loading branch information
LukasWillin committed Nov 10, 2022
1 parent e8b8094 commit 4c940f5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
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 @@ -80,6 +86,10 @@ sub trim_ansi {
$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;
}
my @columns = quotewords($INPUT_SEPARATOR, 1, $line); # split(/\Q$INPUT_SEPARATOR/, $line);
my $column_index = 0;

Expand Down Expand Up @@ -108,6 +118,12 @@ sub trim_ansi {
$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;
}
my @columns = quotewords($INPUT_SEPARATOR, 1, $line); # split (/\Q$INPUT_SEPARATOR/, $line);
my $column_index = -1;

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;
;;
-\? | -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 table 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

0 comments on commit 4c940f5

Please sign in to comment.