From d2b9e2d40285ff61abb6d7bd59a88a899edcbd51 Mon Sep 17 00:00:00 2001 From: binaryDiv Date: Sat, 8 Aug 2020 00:19:33 +0200 Subject: [PATCH] bin/ocelot: version 1.1.0 - new style 'none' (option -n) - use 'big' as default style and '~' as default line character - add option -l for setting line character - add option -s for style 'singleline' - allow reading from stdin by using '-' as filename - allow reading from named pipes - do not print space character as prefix when prefix character is empty --- bin/ocelot | 72 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/bin/ocelot b/bin/ocelot index 2addbc8..000865a 100755 --- a/bin/ocelot +++ b/bin/ocelot @@ -1,5 +1,12 @@ #!/bin/bash +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # +# ocelot -- The cute alternative to cat (with headers!) # +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # +# Author: binaryDiv # +# Version: 1.1.0 # +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # + # Strict mode set -euo pipefail IFS=$'\n\t' @@ -7,8 +14,9 @@ IFS=$'\n\t' BASENAME=$(basename $0) # Global options -STYLE=singleline -PREFIX='#' +STYLE=big +PREFIXCHAR='#' +LINECHAR='~' # Helper functions @@ -17,16 +25,20 @@ PREFIX='#' # Prints help text function usage { cat << END_OF_HELPTEXT -Usage: $BASENAME [OPTION] FILES... +Usage: $BASENAME [OPTION] FILE... -Prints FILES with their filename as a header. Like cat but more cute. +Prints all FILEs with their filename as a header. Like cat but more cute. +If FILE is '-', it reads from standard input. General options: - -h print this help message + -h print this help message Style options: - -b show big headers (dashed lines above and under filename) - -p PREFIX use PREFIX in front of the header lines (default: '#') + -b show big headers (with lines above and under filename; default) + -s show single lined headers (only prefix and filename) + -n no style, just print filename and content + -p PREFIX use PREFIX in front of the header lines (default: '#') + -l LINECHAR use LINECHAR as character for lines in big headers (default: '~') END_OF_HELPTEXT } @@ -42,29 +54,37 @@ function print_file_with_header { # Prints a header function print_header { text=$1 + + if [[ $text == '-' ]]; then + text='STDIN' + fi case $STYLE in big) header_style_big "$text" ;; - singleline | *) + singleline) header_style_singleline "$text" ;; + + none | *) + header_style_none "$text" + ;; esac } # Print error message function print_error { text=$1 - echo "$PREFIX [$text]" + echo "${PREFIX}[$text]" } # Prints content of file function print_file { file=$1 - if [[ -f $file ]]; then + if [[ -f $file || -p $file || $file == '-' ]]; then cat $file elif [[ -d $file ]]; then print_error "File is a directory" @@ -77,18 +97,23 @@ function print_file { # Define header styles # -------------------- +function header_style_none { + text=$1 + echo $text +} + function header_style_singleline { text=$1 - echo "$PREFIX $text" + echo "${PREFIX}${text}" } function header_style_big { text=$1 length=${#text} - echo -n "$PREFIX "; characterline '-' $length - echo "$PREFIX $text" - echo -n "$PREFIX "; characterline '-' $length + echo -n "${PREFIX}"; characterline $LINECHAR $length + echo "${PREFIX}${text}" + echo -n "${PREFIX}"; characterline $LINECHAR $length } # Print a line of characters (e.g. `characterline '#' 32`) @@ -111,20 +136,19 @@ if [[ "$@" == "--help" ]]; then fi # Parse shell arguments and set global variables -while getopts ":hbp:" option; do +while getopts ":hbsnp:l:" option; do case $option in h) usage exit 0 ;; - b) - STYLE=big - ;; + b) STYLE=big ;; + s) STYLE=singleline ;; + n) STYLE=none ;; - p) - PREFIX="$OPTARG" - ;; + p) PREFIXCHAR="$OPTARG" ;; + l) LINECHAR="$OPTARG" ;; :) echo "$BASENAME: option -$OPTARG requires an argument" >&2 @@ -143,6 +167,12 @@ done # Remove parsed options from arguments shift $(($OPTIND - 1)) +# Set some global variables +PREFIX= +if [[ $PREFIXCHAR != '' ]]; then + PREFIX="$PREFIXCHAR " +fi + # Print all files for file in $@; do print_file_with_header $file