diff --git a/src/column_ansi.pl b/src/column_ansi.pl index 8a9c4a0..3ed7f6e 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 ""){ @@ -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; @@ -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; diff --git a/src/column_ansi.sh b/src/column_ansi.sh index 4d5960c..e0d38c5 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; + ;; -\? | -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 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";