summaryrefslogtreecommitdiff
path: root/open_man
diff options
context:
space:
mode:
author0scar <qgt268@alumni.ku.dk>2021-12-11 18:11:29 +0000
committer0scar <qgt268@alumni.ku.dk>2021-12-11 18:11:29 +0000
commitdeadbead206ff49b3ecf31176b5904d1adadfaad (patch)
treec90abdea3bfeac323713841063cfec0ba9ab5c07 /open_man
parenta6f0a2f4c34f5158355100d7df4ae7398ade37d3 (diff)
Add open_man script
Diffstat (limited to 'open_man')
-rwxr-xr-xopen_man58
1 files changed, 58 insertions, 0 deletions
diff --git a/open_man b/open_man
new file mode 100755
index 0000000..f86f1f9
--- /dev/null
+++ b/open_man
@@ -0,0 +1,58 @@
+#!/usr/bin/env bash
+
+cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"}
+if [ -d "$cachedir" ]; then
+ cache=$cachedir/dmenu_run_man
+ historyfile=$cachedir/dmenu_man_history
+else # if no xdg dir, fall back to dotfiles in ~
+ cache=$HOME/.dmenu_cache_man
+ historyfile=$HOME/.dmenu_man_history
+fi
+
+FONT="DejaVuSansMono Nerd Font Mono:pixelsize=16:antialias=true:autohint=true:style=book"
+
+# Generate a new cache if necessary
+if stest -dqr -n "$cache" /usr/share/man/**/*; then
+ ls /usr/share/man/**/* \
+ | sed -Ee 's/^.*\///g;s/(\.gz)?//g;/:$/d;/^ *$/d;s/(.*)\.([0-9].*)$/\1 (\2)/g' \
+ | sort -u > $cache
+fi
+
+awk -v histfile=$historyfile '
+ BEGIN {
+ while( (getline < histfile) > 0 ) {
+ sub("^[0-9]+\t","")
+ print
+ x[$0]=1
+ }
+ } !x[$0]++ ' "$cache" \
+ | dmenu "$@" \
+ | awk -v histfile=$historyfile '
+ BEGIN {
+ FS=OFS="\t"
+ while ( (getline < histfile) > 0 ) {
+ count=$1
+ sub("^[0-9]+\t","")
+ fname=$0
+ history[fname]=count
+ }
+ close(histfile)
+ }
+
+ {
+ history[$0]++
+ print
+ }
+
+ END {
+ if(!NR) exit
+ for (f in history)
+ print history[f],f | "sort -t '\t' -k1rn >" histfile
+ }
+ ' \
+ | while read MAN; do
+ VERSION=$(echo $MAN | sed -Ee 's/.* \(([0-9].*)\)/\1/g')
+ PAGE=$(echo $MAN | sed -Ee 's/(.*) \([0-9].*\)/\1/g')
+ #echo "Man: $VERSION $PAGE"
+ st -e man $VERSION $PAGE
+ done