-
Notifications
You must be signed in to change notification settings - Fork 1
/
nnn-kdialog
executable file
·87 lines (72 loc) · 2.07 KB
/
nnn-kdialog
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
#!/bin/sh
## Instructions
# - Place this file as /usr/local/bin/kdialog
# - Set XDG_CURRENT_DESKTOP=KDE
# - Copy quitd to .config/nnn/plugins, make it executable
# To pick a directory in nnn, press Space or Ctrl-J.
# Or simply type ;l to apply the quitd plugin to select pwd
## Debugging
# args="$@"
# notify-send "kdialog $args"
# echo "kdialog $args" >> $HOME/kdialog-log
# dl_dir="$HOME/dl"
export NNN_PLUG='l:quitd'
# $1 is the window title
# $2 is the default directory
getopenfilename() {
! [ -d "$2" ] && dir="$HOME" || dir="$2"
$TERMINAL -t "$1" -e nnn -A -p /tmp/fileselect "$dir"
path=$(tail -n 1 /tmp/fileselect)
[ -f "$path" ] && [ -f /tmp/fileselect ] && echo $path
rm /tmp/fileselect
}
getexistingdirectory() {
$TERMINAL -t "$1" -e nnn -A -p /tmp/fileselect "$2"
path=$(tail -n 1 /tmp/fileselect)
[ -d "$path" ] && [ -f /tmp/fileselect ] && echo $path
rm /tmp/fileselect
}
# $2 is the default filename
getsavefilename() {
[ -n dl_dir ] && dir="$(getexistingdirectory "$1")" || dir="$dl_dir"
fname=$(echo "$2" | dmenu)
# If user did not choose default, add extension
! [ "$fname" == "$2" ] && [ -n "$fname" ] && [ -n "$2" ] && fname="$fname"."${2#*.}"
# Replace spaces with underscores
fname=$(echo "$fname" | tr " " "_")
[ -n "$dir" ] && [ -n "$fname" ] && echo "$dir"/"$fname"
}
version() {
# impersonate the latest version
echo "kdialog 21.12.1"
}
## Parse options
for (( i=1; i<=$#; i++ ));
do
case ${!i} in
--getopenfilename | --getexistingdirectory )
# get rid of the leading dashes
fun=${!i##--}
(( i++ ))
secondary="${!i}"
;;
--version )
fun=${!i##--}
;;
--getsavefilename )
fun=${!i##--}
(( i++ ))
save_path="${!i}"
secondary="${save_path##/*/}"
;;
--title )
(( i++ ))
title=${!i}
;;
esac
done 2> /dev/null;
title=${title:-nnn-kdialog}
## Epic functional programming trick
if ! [ -z "$fun" ]; then
echo "$($fun "$title" "$secondary")" # | tee -a $HOME/kdialog-log
fi