aboutsummaryrefslogtreecommitdiffstats
path: root/gistfile1.txt
blob: 8ceec7d7a54b793a4f75b221e317d01bca957a5e (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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/sh
# terminal application launcher for sway, using fzf
# Based on: https://gitlab.com/FlyingWombat/my-scripts/blob/master/sway-launcher

HIST_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/sway-launcher-history.txt"

DIRS=(
/usr/share/applications
~/.local/share/applications
/usr/local/share/applications
)

GLYPH_COMMAND="  "
GLYPH_DESKTOP="  "

touch $HIST_FILE
# Filter DIRS array for directories that actually exist. Append *.desktop to remaining elements
for i in "${!DIRS[@]}" ; do [[ ! -d "${DIRS[i]}" ]] && unset -v 'DIRS[$i]' || DIRS[$i]="${DIRS[i]}/*.desktop" ; done
DIRS=("${DIRS[@]}")

HIST_FILE_CONTENT=$(cat "$HIST_FILE")
HIST_ENTRIES=$(echo "$HIST_FILE_CONTENT" | sed -n -e 's/^[0-9]* //p')

function createPreview(){
	DESCRIPTION='No description'
	if [[ $2 == 'command' ]]; then
		TITLE=$1
		DESCRIPTION=$(whatis -l $1 2>/dev/null | head -n1 | sed -n -e 's/^.*\(.*\).*\-//p')
	else
		TITLE=$(cat $1 | grep ^Name= | head -n1 | awk -F= '{print $2}')
		DESCRIPTION=$(cat $1 | grep Comment= | awk -F= '{print $2}')
    fi
    echo -e "\033[33m $TITLE \033[0m"
    echo "$DESCRIPTION"
}
export -f createPreview

FZFPIPE=$(mktemp)

# Append Launcher History, removing usage count
(echo "$HIST_FILE_CONTENT" | sed -n -e 's/^[0-9]* //p' >> $FZFPIPE )&

# Load and append Desktop entries
(grep -roP "Type=Application" ${DIRS[@]} |
 awk -F : '{print $1}' | 
 sort -u | 
 xargs -d "\n" grep -oP "(?<=Name=).*" | 
 awk -F : -v pre="$GLYPH_DESKTOP"  '{print $1 "|desktop|\033[33m" pre "\033[0m" $2}' >> $FZFPIPE )&

# Load and append command list
({ IFS=:; ls -H $PATH; } | grep -v '/.*' | sort -u | awk -v pre="$GLYPH_COMMAND" '{print $1 "|command|\033[31m" pre "\033[0m" $1 }' >> $FZFPIPE )&

command_str=$((tail -f $FZFPIPE & echo $! > pid) |
  fzf +x +s -d '\|' --nth ..3 --with-nth 3.. --preview 'createPreview {1} {2}' --preview-window=up:3:wrap --ansi ; kill -9 $(<pid) |
  tail -n1) || exit 1


[ -z "$command_str" ] && exit 1


# get full line from history (with count number)
HIST_LINE=$(echo "$HIST_FILE_CONTENT" | grep -Pe "^[0-9]+ \Q$command_str\E$")
# echo "Hist Line: $HIST_LINE"



if [ "$HIST_LINE" == "" ]; then
    HIST_COUNT=1
else
    # Increment usage count
    HIST_COUNT=$(echo "$HIST_LINE" | sed -E 's/^([0-9]+) .+$/\1/')
    ((HIST_COUNT++))
    # delete line, to add updated later
    HIST_FILE_CONTENT=$(echo "$HIST_FILE_CONTENT" | \
	grep --invert-match -Pe "^[0-9]+ \Q$command_str\E$")
fi

# update history
update_line="${HIST_COUNT} ${command_str}"
echo -e "${update_line}\n${HIST_FILE_CONTENT}" | \
    sort --numeric-sort --reverse > "$HIST_FILE"

command='echo "nope"'

case $(echo $command_str | awk -F'|' '{print $2}') in
desktop)
  file=$(echo $command_str | awk -F '|' '{print $1}')
  command=$(cat $file | grep Exec | awk -F'=' '{print $2}')
  ;;

command)
  command=$(echo $command_str | awk -F '|' '{print $1}')
  ;;

esac

swaymsg -t command exec "$command"