summaryrefslogtreecommitdiffstats
path: root/dmemu
blob: 7233c0b60431e5aac8b35e4dcf34f635b1da9d89 (plain)
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
#!/bin/bash

PRIORITY_FACTOR=99/100
BASE_PRIORITY=10000
SAVE_FILE="$HOME/.dmemu"


INPUT="$(cat)"
DMENUINPUT="$INPUT"

if [ -r "$SAVE_FILE" ]; then
    SAVED_STRINGS="$(sort -rn $SAVE_FILE | cut -d ' ' -f 2- | grep -xF "$INPUT")"
    if [ "$SAVED_STRINGS" ]; then
	DMENUINPUT=$(echo "$SAVED_STRINGS"; echo "$INPUT" | grep -vxF "$SAVED_STRINGS")
    fi
fi

RET=$(echo "$DMENUINPUT" | dmenu "$@")

if [ -z "$RET" ]; then
    exit 0
fi

touch "$SAVE_FILE" || true

if [ -w "$SAVE_FILE" ]; then
    (
	HANDLED=0
    
	IFS="
"
	for LINE in $(cat "$SAVE_FILE"); do
	    VAL="$(echo -n "$LINE" | cut -d ' ' -f 1)"
	    COMMAND="$(echo -n "$LINE" | cut -d ' ' -f 2-)"
	    if [ "$COMMAND" = "$RET" ]; then
		echo $(($VAL+$BASE_PRIORITY)) "$COMMAND"
		HANDLED=1
	    else
		echo $(($VAL*$PRIORITY_FACTOR)) "$COMMAND"
	    fi
    	done
	if [ "$HANDLED" -eq 0 ]; then
	    echo $BASE_PRIORITY "$RET"
	fi
    ) > "$SAVE_FILE.new"
    
    mv "$SAVE_FILE.new" "$SAVE_FILE"
fi

echo -n "$RET"