File: virtualize.sh

package info (click to toggle)
clifm 1.26.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,816 kB
  • sloc: ansic: 64,595; sh: 3,133; python: 1,851; makefile: 567
file content (67 lines) | stat: -rwxr-xr-x 1,849 bytes parent folder | download
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
#!/bin/sh

# Virtual directories plugin for Clifm
# Dependencies: sed

# Author: L. Abramovich
# License: GPL2+

# Note: A new instance of Clifm will be spawned on a new terminal window
# using $term_cmd, which defaults to 'xterm -e'. Edit this variable to
# use your favorite terminal emulator instead, for example:
# term_cmd="kitty sh -c"

term_cmd="xterm -e"
clifm_bin="clifm"
clifm_opts=""

if [ -z "$1" ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
	name="${CLIFM_PLUGIN_NAME:-$(basename "$0")}"
	printf "Create a virtual directory for a set or collection of files
\n\x1b[1mUSAGE\x1b[0m\n  %s [-d] [FILE...] \n\
\n\x1b[1mEXAMPLES\x1b[0m
  'vt sel'          Send all selected files to a virtual directory\n\
  'vt file1 file2'  Send file1 and file2 to a virtual directory\n\
  'vt -d'           If navigating the file system, use the -d option to change back to the virtual directory\n" "$name" >&2

	exit 0
fi

if ! type sed > /dev/null 2>&1; then
	printf "clifm: sed: Command not found\n" >&2
	exit 127
fi

if [ -n "$CLIFM_VT_RUNNING" ]; then
	if [ -n "$1" ] && [ "$1" = "-d" ]; then
		if [ -n "$CLIFM_VIRTUAL_DIR" ]; then
			echo "$CLIFM_VIRTUAL_DIR" > "$CLIFM_BUS"
			exit 0
		fi
	fi

	printf "clifm: Only one virtual directory can be created at a time\n" >&2
	exit 1
fi

if [ -n "$1" ] && [ "$1" = "-d" ]; then
	printf "clifm: No virtual directory found\n" >&2
	exit 1
fi

export CLIFM_VT_RUNNING=1

if [ -n "$CLIFM_VIRTUAL_DIR" ]; then
	clifm_opts="$clifm_opts --virtual-dir=\"$CLIFM_VIRTUAL_DIR\""
fi

# 1. Replace escaped spaces by tabs
# 2. Replace non-escaped spaces by new line chars
# 3. Replace tabs (first step) by spaces
# 4. Remove remaining escape chars
files="$(echo "$*" | sed 's/\\ /\t/g;s/ /\n/g;s/\t/ /g;s/\\//g')"
cmd="$term_cmd 'echo \"$files\" | $clifm_bin $clifm_opts --no-restore-last-path'"

eval "$cmd"

exit 0