-
Notifications
You must be signed in to change notification settings - Fork 57
/
lib-bwmenu
36 lines (31 loc) · 980 Bytes
/
lib-bwmenu
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
#!/bin/bash
# Helper functions
# Escape the argument into a valid jq string literal (with quotes included)
# $1: string to escape
jq_escape() {
echo -n "$1" | jq -Rs
}
# Extract item or items matching .name, including deduplication
# $1: item name, prepended or not with deduplication mark
array_from_name() {
item_name=$(jq_escape "${1#$DEDUP_MARK }")
echo "$ITEMS" | jq -r "map(select((.name == $item_name) and (.type == $TYPE_LOGIN)))"
}
# Extract item matching .id
# $1: string starting with ".id:"
array_from_id() {
echo "$ITEMS" | jq -r ". | map(select(.id == $(jq_escape $1)))"
}
# Count the number of items in an array. Return true if more than 1 or none
# $1: Array of items
not_unique() {
item_count=$(echo "$1" | jq -r '. | length')
! [[ $item_count -eq 1 ]]
}
# Pipe a document and deduplicate lines.
# Mark those duplicated by prepending $DEDUP_MARK
dedup_lines() {
sort | uniq -c \
| sed "s/^\s*1 //" \
| sed -r "s/^\s*[0-9]+ /$DEDUP_MARK /"
}