commit 89943d7c16b24f5f1139f3605f07871b72ab1d48
parent c7a885b589df7abacde1a838424c872321ada45e
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Sat, 13 Feb 2021 15:01:15 -0800
Greatly simplify feed script
- Add copywrite and license header
- Commands are no longer needlessly prefixed with a hyphen (`-')
- Commands must be explicitly specified, expr is not defaulted to
- Commands now have a long form name aswell
- Documentation is now more terse or omitted entirely.
- Single quotes are prefered to double quotes
- Use the `/bin/sh' shebang rather than `/usr/bin/env dash'
Diffstat:
1 file changed, 30 insertions(+), 43 deletions(-)
diff --git a/modules/scripts/.local/bin/feed b/modules/scripts/.local/bin/feed
@@ -1,58 +1,45 @@
-#!/usr/bin/env dash
-#! feed -- search and display items from sfeed(5) formatted files
-#!
-#! There are three commands, `-h' which displays a brief usage, `-l'
-#! which lists the given feeds items, and `-n' which displays the given
-#! feeds `nth' item.
-#!
-#! If none of the above are given the command is passed to egrep(1) and
-#! matching items are displayed.
+#!/bin/sh
+# Copywrite 2020, 2021 Jacob R. Edwards
+# License: GPLv3
+#
+# feed -- sfeed(5) file user interface
+#
+# l[ist] List items
+# i[tem] Display the nth item
+# e[xpr] Display items matching an ERE
-set -eu
-
-# display usage
-# usage(void)
-usage()
+fmt()
{
- echo "usage: `basename $0` [-hln]"
+ awk -F'\t' '{ printf "From %s: %s\n\n%s", $7, $2, $4 }' $* |
+ sed -e 's/\\t//g' -e 's/\\n//g' | w3m -T text/html -dump -O ASCII |
+ command fmt
}
-# return html or plain-text from specified files or stdin
-# format([file], ...)
-format()
+list()
{
- awk -F'\t' '{ printf "From %s: %s\n\n%s", $7, $2, $4 }' |
- sed -e 's/\\t//g' -e 's/\\n//g' $* | w3m -T text/html -dump -O ASCII |
- fmt
+ awk -vOFS='\t' -F'\t' '{ print FILENAME, FNR, $2 }' $*
}
-# return specified feeds, or stdin, formatted in 'filename index: title`
-# show([file], ...)
-show()
+item()
{
- awk -vOFS="\t" -F"\t" '{ print FILENAME, FNR, $2 }' $@
+ local item=${1:?no item index}; shift
+ for feed in ${*-'/dev/stdin'}
+ do
+ sed -n "$n { p
+ q
+ }" $feed
+ done | fmt
}
-# return the nth feed in specified files or stdin
-# nth(n, [file], ...)
-nth()
+expr()
{
- local n=$1
- shift
- for feed in ${@:-''}
- do
- head -$n $feed | tail -n1
- done
+ egrep -i $* | fmt
}
-if [ $# -gt 0 ]; then
- cmd=$1
- shift
-fi
-case "${cmd:=}"
+case $1
in
- -h) usage; exit 2;;
- -l) show $@;;
- -n) nth $@ | format;;
- *) egrep -i "$cmd" $@ | format;;
+ l|list) shift; list $*;;
+ i|item) shift; item $*;;
+ e|expr) shift; expr $*;;
+ *) printf 'usage: %s [command] [arg...]\n' `basename $0` 1>&2; exit 1;;
esac