From: Florian Baumann Date: Tue, 30 Nov 2010 15:33:10 +0000 (+0100) Subject: added handmade commandline history tool X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=aa228fb0c37da5525482194f322541ddafd6a2df;p=common%2Fbash_it.git added handmade commandline history tool --- diff --git a/plugins/hcht.plugin.bash b/plugins/hcht.plugin.bash new file mode 100644 index 0000000..c83f6b4 --- /dev/null +++ b/plugins/hcht.plugin.bash @@ -0,0 +1,121 @@ +#!/bin/bash +# hch.plugin.bash: the handmade commandline history tool +# Copyright: (C) 2010 Florian Baumann +# License: GPL-3 +# Date: Dienstag 2010-11-30 + +### readme +# basiclly the handmade commandline history tool was made for storing +# informations. yeah, you're right. sounds a bit boring. many other +# applications can do this much better. but storing things from commandline? +# +# hcht was fitted to work at your terminal. +# your daily stuff like notices, todos, commands or output from a +# all these things will be stored without complex syntax. +# +# once you defined your storing-directory you will be able to easily +# save all the stuff listed above. +# + +### create a file +# the basic feature. open a file, do stuff and save. +# *.hch trailor is required. +# +# $ hcht evilcommand.hch +# +# this will create a new file or edit a existing one. +# paste your command or notice in your favorite editor + +### show all stored informations +# to get an overview of your storedir: +# +# $ hcht + +### todo with a whole sentence +# you can give hcht a bunch of parameters +# +# $ hcht this is a long reminder about a anything + +### save last executed command +# lets say you did a great hack at your system and you +# want to save it without complicated use of coping: +# +# $ hcht !! +# +# the "!!" will repeat the _last_ command you executed at +# your terminal. after asking you about a name the hch file +# will be saved. + +### read from stdin +# hcht is also able to read anything from stdin. +# +# $ cat any_important_logfile | hcht anylog +# +# "anylog" will be the name of the saved file. + +### configure store dir +# like /home/user/.hcht/ or something +hchtstoredir="" + +hcht() { + # configured? + if [ -z $hchtstoredir ]; then + echo "ERROR: handmade commandline history tool isn't configured." + return 1 + else + hchtstoredir=$(echo $hchtstoredir | sed -e 's/\/$//') + fi + + # dir available? + if [ ! -d $hchtstoredir ]; then + echo "ERROR: No such directory: $hchtstoredir" + return 1 + fi + + # get favorite editor + if [ -z $EDITOR ]; then + EDITOR=$(which vim || which nano) + fi + + # check if stdin-data is present and save content + if [ "$(tty)" = "not a tty" ]; then + hchname=$(echo $1 | sed -e 's/\ //g') + if [ -z $hchname ]; then + cat < /dev/stdin >> $hchtstoredir/$(date +%Y%m%d%H%M%S).hch + else + cat < /dev/stdin >> $hchtstoredir/$hchname.hch + fi + return 0 + fi + + # list all hch files if no parameter is given + if [ $# -eq 0 ]; then + for file in $(ls $hchtstoredir); do + echo $file + done + return 0 + fi + + # if a *.hch file is given start editing or creating it + if [ "$#" -eq "1" ]; then + if echo "$1" | grep -q -e ".*.hch$" ; then + $EDITOR ${hchtstoredir}/${1} + return 0 + else + $EDITOR ${hchtstoredir}/${1}.hch + return 0 + fi + fi + + # autocreate a new hch + if [ "$#" -gt "1" ]; then + echo -n "define a name: " ; read hchname + hchname=$(echo $hchname | sed -e 's/\ /_/g') + if [ -z "$hchname" ]; then + echo "$*" > $hchtstoredir/${1}-$(date +%Y%m%d%H%M%S).hch + else + echo "$*" > ${hchtstoredir}/${hchname}.hch + fi + return 0 + fi +}