Switch experiment README to .md
[khome.git] / home / bin / p
1 #! /bin/bash
2
3 set -eo pipefail
4
5 PROG=$0
6
7 find() {
8 local -r name="$1"
9 local -r file="$2"
10
11 awk \
12 -v _s="$name" \
13 '
14 BEGIN {_s = tolower(_s)}
15
16 /^[^ \t]/ && tolower($1) ~ _s && NF >= 2 {
17 n++
18 s = $1
19 p = $NF
20 if (NF == 2) {
21 e = ""
22 u = ""
23 } else if (NF == 3) {
24 e = $2
25 u = ""
26 } else {
27 e = $2
28 u = $3
29 } # TODO What would NF > 4 mean?
30
31 printf("%d [O] s:\"%s\", e:\"%s\", u:\"%s\"\n", n, s, e, u) > "/dev/stderr"
32 if (match(u, "@")) {
33 tmp = e
34 e = u
35 u = tmp
36 printf("%d [C] s:\"%s\", e:\"%s\", u:\"%s\"\n", n, s, e, u) > "/dev/stderr"
37 }
38 printf("%d s:\"%s\" e:\"%s\" u:\"%s\" %s\n", n, s, e, u, p)
39 }
40 ' \
41 "$file"
42 }
43
44 main() {
45 local -r default_file="$HOME"/._p/p
46 local -r usage="Usage: $PROG [FILE] NAME"
47 local name file records i meta data datum
48
49 case "$#" in
50 0)
51 echo "$usage" >&2
52 return 1
53 ;;
54 1)
55 file="$default_file"
56 name="$1"
57 ;;
58 2)
59 file="$1"
60 name="$2"
61 ;;
62 *)
63 echo "$usage" >&2
64 return 1
65 ;;
66 esac
67
68 records=$(find "$name" "$file")
69 meta=$(echo "$records" | awk '{print($1, $2, $3, $4)}')
70 data=$(echo "$records" | awk '{print($1, $5)}')
71
72 # XXX Even an empty result will count as one line after echo, so need to filter:
73 case "$(echo "$records" | grep -vc '^$')" in
74 0)
75 echo '[ERROR] Found nothing.' >&2
76 exit 1
77 ;;
78 1)
79 datum="$data"
80 ;;
81 *)
82 i=$(echo "$meta" | column -t | fzf | awk '{print $1}')
83 datum=$(echo "$data" | awk -v i="$i" 'NR == i')
84 ;;
85 esac
86
87 # XXX Intentionally avoiding newline:
88 echo "$datum" | awk '{printf "%s", $2}' | xsel -i -b -t 30000
89
90 }
91
92 main "$@"
This page took 0.126886 seconds and 4 git commands to generate.