diff --git a/README.md b/README.md index 526f37f..b8b8f23 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,16 @@ # nyan -A small wrapper to make cat prettier. \ No newline at end of file +A small wrapper to make cat prettier while concatenating large files. + +# Features +* Pipes `cat` through `pv` to make a whole-operation progress bar with a time estimate. +* Pre-checks files _before_ passing them to `cat`. No more partial concatenations! + +# Usage +`nyan -o [OUTPUT_FILE] [INPUT_FILE]...` + +An output file defined with `-o` is required. This wrapper is designed for non-human-readable files +and as such does not output to stdout. + +# Dependencies +* `pv` is required for the progress bar. diff --git a/nyan b/nyan new file mode 100755 index 0000000..6c33f0e --- /dev/null +++ b/nyan @@ -0,0 +1,39 @@ +#!/bin/bash +optstring="o:" +declare -i abort=0 + +# TODO: Check for dependencies pv and stat. +# TODO: Add help option to display usage. +# TODO: Consider a switch for per-file progress bars rather than a whole-operation one. + +while getopts ${optstring} args; do + case $args in + o) output_file=${OPTARG} ;; + :) echo "Output file required." ;; + esac +done +shift $((OPTIND-1)) + +echo "Concatenating files..." +declare -i total_size=0 +for fname in "$@"; do + if [ ! -e "$fname" ]; then + abort=1 + echo "${fname} does not exist." + elif [ -d "$fname" ]; then + abort=1 + echo "${fname} is a directory." + else + current_file_size=$(stat -c%s "$fname") + total_size=$((total_size+current_file_size)) + # TODO: Use something more legible than bytes. + echo "Adding ${fname} at ${total_size}b." + fi +done + +if [ $abort -eq 0 ]; then + cat "$@" | pv -epts "$total_size" > "$output_file" + echo "Done!" +else + echo "Errors occurred, concatenation aborted." +fi