-
Notifications
You must be signed in to change notification settings - Fork 2
/
selld8
executable file
·79 lines (73 loc) · 2.02 KB
/
selld8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env bash
SHOW_SESSION="" # will add ,visitno if non-zero
_selld8_query(){
[ -n $SHOW_SESSION ] && session=", visit.visitno" || session=""
lncddb "
select
l.id || '_' || to_char(vtimestamp, 'YYYYMMDD') as ld8,
round(age::numeric,1) as age,
sex,
vtype,
string_agg(study, ' ') as studies,
string_agg(distinct vee.id,' ') as veid
$session
from enroll l
natural join visit
natural join visit_study
natural join person
left join visit_enroll ve on ve.vid = visit.vid
left join enroll vee on ve.eid = vee.eid
where l.etype like 'LunaID'
group by l.id,sex,visit.vid $session
order by vtimestamp desc"
}
_selld8(){
_selld8_query $@ |
fzf |
cut -f1
}
# copy/paste from fuzzy_arg
# https://github.com/WillForan/fuzzy_arg
_bash_insert() { perl -le 'ioctl(STDIN,0x5412,$_) for split "", join " ", @ARGV' -- "$@";}
_zsh_insert() { LBUFFER+="$@"; }
# put it all together
_selld8_insert() { _${_SHELL}_insert $(_selld8); }
_selld8_bind(){
_SHELL=bash
[ $(basename $SHELL) = "zsh" ] && _SHELL=zsh
# bind to keys (alt+a, ctrl+x ctrl+a)
if [ $(basename $SHELL) = "zsh" ]; then
zle -N _selld8_insert
bindkey "^[l" _selld8_insert
else
bind -x '"\C-x\C-l":"_selld8_insert"'
fi
}
_selld8_usage(){
cat <<HERE
USAGE: $(basename $0) i[nit] | q[uery] | l[ist]
select id from database. optioanlly
- interactively with fzf
- with bash/zsh keybinding
EXAMPLE:
# add binding ctrl-x ctrl-l
eval "\$(selld8 init)"
# lookup and pipe
selld8 l | grep '2020.*PET'
HERE
exit 1
}
_selld8_main() {
[ $# -eq 0 ] && _selld8_usage
# show session
[ "$1" == "-ses" ] && SHOW_SESSION=1 && shift;
case $1 in
-h*|help) _selld8_usage;;
i*) echo "source $0; _selld8_bind";;
l*) shift; _selld8_query $@;;
*) shift; _selld8 $@;;
esac
}
# if not sourced, run main
([[ -n $ZSH_EVAL_CONTEXT && $ZSH_EVAL_CONTEXT =~ :file$ ]] ||
[[ -n $BASH_VERSION ]] && (return 0 2>/dev/null)) || _selld8_main $@