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
This commit is contained in:
parent
220074daa5
commit
d2b9e2d402
68
bin/ocelot
68
bin/ocelot
|
|
@ -1,5 +1,12 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
|
||||||
|
# ocelot -- The cute alternative to cat (with headers!) #
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
|
||||||
|
# Author: binaryDiv #
|
||||||
|
# Version: 1.1.0 #
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
|
||||||
|
|
||||||
# Strict mode
|
# Strict mode
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
IFS=$'\n\t'
|
IFS=$'\n\t'
|
||||||
|
|
@ -7,8 +14,9 @@ IFS=$'\n\t'
|
||||||
BASENAME=$(basename $0)
|
BASENAME=$(basename $0)
|
||||||
|
|
||||||
# Global options
|
# Global options
|
||||||
STYLE=singleline
|
STYLE=big
|
||||||
PREFIX='#'
|
PREFIXCHAR='#'
|
||||||
|
LINECHAR='~'
|
||||||
|
|
||||||
|
|
||||||
# Helper functions
|
# Helper functions
|
||||||
|
|
@ -17,16 +25,20 @@ PREFIX='#'
|
||||||
# Prints help text
|
# Prints help text
|
||||||
function usage {
|
function usage {
|
||||||
cat << END_OF_HELPTEXT
|
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:
|
General options:
|
||||||
-h print this help message
|
-h print this help message
|
||||||
|
|
||||||
Style options:
|
Style options:
|
||||||
-b show big headers (dashed lines above and under filename)
|
-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: '#')
|
-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
|
END_OF_HELPTEXT
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,28 +55,36 @@ function print_file_with_header {
|
||||||
function print_header {
|
function print_header {
|
||||||
text=$1
|
text=$1
|
||||||
|
|
||||||
|
if [[ $text == '-' ]]; then
|
||||||
|
text='STDIN'
|
||||||
|
fi
|
||||||
|
|
||||||
case $STYLE in
|
case $STYLE in
|
||||||
big)
|
big)
|
||||||
header_style_big "$text"
|
header_style_big "$text"
|
||||||
;;
|
;;
|
||||||
|
|
||||||
singleline | *)
|
singleline)
|
||||||
header_style_singleline "$text"
|
header_style_singleline "$text"
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
none | *)
|
||||||
|
header_style_none "$text"
|
||||||
|
;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
# Print error message
|
# Print error message
|
||||||
function print_error {
|
function print_error {
|
||||||
text=$1
|
text=$1
|
||||||
echo "$PREFIX [$text]"
|
echo "${PREFIX}[$text]"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Prints content of file
|
# Prints content of file
|
||||||
function print_file {
|
function print_file {
|
||||||
file=$1
|
file=$1
|
||||||
|
|
||||||
if [[ -f $file ]]; then
|
if [[ -f $file || -p $file || $file == '-' ]]; then
|
||||||
cat $file
|
cat $file
|
||||||
elif [[ -d $file ]]; then
|
elif [[ -d $file ]]; then
|
||||||
print_error "File is a directory"
|
print_error "File is a directory"
|
||||||
|
|
@ -77,18 +97,23 @@ function print_file {
|
||||||
# Define header styles
|
# Define header styles
|
||||||
# --------------------
|
# --------------------
|
||||||
|
|
||||||
|
function header_style_none {
|
||||||
|
text=$1
|
||||||
|
echo $text
|
||||||
|
}
|
||||||
|
|
||||||
function header_style_singleline {
|
function header_style_singleline {
|
||||||
text=$1
|
text=$1
|
||||||
echo "$PREFIX $text"
|
echo "${PREFIX}${text}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function header_style_big {
|
function header_style_big {
|
||||||
text=$1
|
text=$1
|
||||||
length=${#text}
|
length=${#text}
|
||||||
|
|
||||||
echo -n "$PREFIX "; characterline '-' $length
|
echo -n "${PREFIX}"; characterline $LINECHAR $length
|
||||||
echo "$PREFIX $text"
|
echo "${PREFIX}${text}"
|
||||||
echo -n "$PREFIX "; characterline '-' $length
|
echo -n "${PREFIX}"; characterline $LINECHAR $length
|
||||||
}
|
}
|
||||||
|
|
||||||
# Print a line of characters (e.g. `characterline '#' 32`)
|
# Print a line of characters (e.g. `characterline '#' 32`)
|
||||||
|
|
@ -111,20 +136,19 @@ if [[ "$@" == "--help" ]]; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Parse shell arguments and set global variables
|
# Parse shell arguments and set global variables
|
||||||
while getopts ":hbp:" option; do
|
while getopts ":hbsnp:l:" option; do
|
||||||
case $option in
|
case $option in
|
||||||
h)
|
h)
|
||||||
usage
|
usage
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
|
|
||||||
b)
|
b) STYLE=big ;;
|
||||||
STYLE=big
|
s) STYLE=singleline ;;
|
||||||
;;
|
n) STYLE=none ;;
|
||||||
|
|
||||||
p)
|
p) PREFIXCHAR="$OPTARG" ;;
|
||||||
PREFIX="$OPTARG"
|
l) LINECHAR="$OPTARG" ;;
|
||||||
;;
|
|
||||||
|
|
||||||
:)
|
:)
|
||||||
echo "$BASENAME: option -$OPTARG requires an argument" >&2
|
echo "$BASENAME: option -$OPTARG requires an argument" >&2
|
||||||
|
|
@ -143,6 +167,12 @@ done
|
||||||
# Remove parsed options from arguments
|
# Remove parsed options from arguments
|
||||||
shift $(($OPTIND - 1))
|
shift $(($OPTIND - 1))
|
||||||
|
|
||||||
|
# Set some global variables
|
||||||
|
PREFIX=
|
||||||
|
if [[ $PREFIXCHAR != '' ]]; then
|
||||||
|
PREFIX="$PREFIXCHAR "
|
||||||
|
fi
|
||||||
|
|
||||||
# Print all files
|
# Print all files
|
||||||
for file in $@; do
|
for file in $@; do
|
||||||
print_file_with_header $file
|
print_file_with_header $file
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue