T: rewritten with functions.

This commit is contained in:
Ole Tange 2021-03-04 13:05:07 +01:00
parent 112ed47440
commit b02cf11736
2 changed files with 58 additions and 62 deletions

92
T/T
View file

@ -120,6 +120,7 @@ B<tee>(1), B<cat>(1)
=cut =cut
find_tmp_dir() { find_tmp_dir() {
# Search for .tmp in all parent dirs
dir="$1" dir="$1"
if [ -d "$1"/.tmp ] ; then if [ -d "$1"/.tmp ] ; then
echo "$1"/.tmp echo "$1"/.tmp
@ -134,73 +135,68 @@ find_tmp_dir() {
fi fi
} }
tmpdir="$(find_tmp_dir .)" oldversion() {
# Find $oldversion of $name
local tmpdir="$1"
local name="$2"
local oldversion="$3"
find "$tmpdir" -name "$name"-'[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]?[0-9][0-9]:[0-9][0-9]:[0-9][0-9]-[0-9]*' |
sort |
tail -n $oldversion |
head -n 1
}
#debug_log=/tmp/T-debug
debug_log=/dev/null
tmpdir=$(find_tmp_dir .)
mkdir -p "$tmpdir" mkdir -p "$tmpdir"
T_DIR="$tmpdir" case "$1" in
(-[0-9]*) oldversion=${1:1}
#DEBUG_LOG=/tmp/T-debug
DEBUG_LOG=/dev/null
if echo $1 | egrep '^-[0-9]+$' >/dev/null; then
T_BACK_NO=$(echo $1 | sed -e 's:-::')
shift shift
if [ -z "$1" ]; then ;;
# No filename given. Default = tempfile esac
T_NO_SLASH=tempfile
else if [ -z "$1" ]; then
# Map / to __ to avoid creating dirs in ~/.tmp name=tempfile
T_NO_SLASH=$(echo $1 | sed -e 's:/:__:g')
fi
T_TEMPFIL=$T_DIR/$T_NO_SLASH
T_BACK_FILENAME=$(find $T_DIR -name "$T_NO_SLASH"-'[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]?[0-9][0-9]:[0-9][0-9]:[0-9][0-9]-[0-9]*' \
| sort | tail -n $T_BACK_NO | head -n 1)
if [ -z "$T_BACK_FILENAME" ]; then
T_TEMPFIL=$T_DIR/$T_NO_SLASH
else
T_TEMPFIL=$T_BACK_FILENAME
fi
else else
if [ -z "$1" ]; then name=$(echo "$1" | sed -e 's:/:__:g')
# No filename given. Default = tempfile
T_TEMPFIL=$T_DIR/tempfile
else
# Map / to __ to avoid creating dirs in ~/.tmp
T_NO_SLASH=$(echo $1 | sed -e 's:/:__:g')
T_TEMPFIL=$T_DIR/$T_NO_SLASH
fi
fi fi
if [ -z "$oldversion" ]; then
readpathname="$tmpdir/$name"
writename="$name"-`date +"%Y-%m-%dT%H:%M:%S"`-$$
writepathname="$tmpdir/$writename"
else
readpathname=$(oldversion "$tmpdir" "$name" "$oldversion")
writepathname="$readpathname"
fi
T_NY_FIL="$T_TEMPFIL"-`date +"%Y-%m-%dT%H:%M:%S"`-$$ echo "$tmpdir $pathname $name $oldversion" >> $debug_log
mkdir $T_DIR 2>/dev/null
if tty -s ; then if tty -s ; then
# STDIN is terminal # STDIN is terminal
# Don't care what STDOUT is # Don't care what STDOUT is
# T | cat => T= cat file # T | cat => T= cat file
# T => T= cat file # T => T= cat file
echo cat "$T_TEMPFIL" >> $DEBUG_LOG echo cat "$readpathname" >> $debug_log
cat "$T_TEMPFIL" cat "$readpathname"
else else
# STDIN redir'ed
if [ -t 1 ] ; then if [ -t 1 ] ; then
# STDOUT = terminal # STDOUT = terminal
# cat | T => T= >file # cat | T => T= >file
echo cat '>' "$T_NY_FIL" >> $DEBUG_LOG echo cat '>' "$writepathname" >> $debug_log
cat > "$T_NY_FIL" cat > "$writepathname"
else else
# STDOUT redir'ed # STDOUT redir'ed
# cat | T | cat => T= tee file # cat | T | cat => T= tee file
echo tee "$T_NY_FIL" >> $DEBUG_LOG echo tee "$writepathname" >> $debug_log
tee "$T_NY_FIL" tee "$writepathname"
fi
if [ -z "$oldversion" ]; then
echo ln -s "$writename" "$readpathname" >> $debug_log
# Replace symlink to most recent file
ln -fs "$writename" "$readpathname"
fi fi
# Remove hardlink to most recent file
rm "$T_TEMPFIL" 2> /dev/null
# make a hardlink to the most recent file
ln "$T_NY_FIL" "$T_TEMPFIL"
fi fi

View file

@ -26,11 +26,11 @@ column, the line number is treated as the x-axis.
Examples: Examples:
seq 1 100 | plotpipe seq 1 100 | plotpipe
seq 1 100 | shuf | plotpipe seq 1 100 | shuf | plotpipe --smooth 10 --log y
paste <(seq 1 100) <(seq 1 100) <(seq 1 100 | shuf) | plotpipe paste <(seq 1 100) <(seq 1 100) <(seq 1 100 | shuf) | plotpipe
(echo "#Title"; echo "#Subtitle"; (echo "#Title"; echo "#Subtitle";
printf "Column1\tColumn2\tColumn3"; printf "Column 1\tColumn 2\tColumn 3\n";
paste <(seq 1 100) <(seq 1 100) <(seq 1 100 | shuf) ) | plotpipe paste <(seq 1 100) <(seq 51 150) <(seq 1 100 | shuf) ) | plotpipe
Copyright (C) 2020-2021 Ole Tange, http://ole.tange.dk and Free Copyright (C) 2020-2021 Ole Tange, http://ole.tange.dk and Free
Software Foundation, Inc. Software Foundation, Inc.