File: disk_analyzer.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 (127 lines) | stat: -rwxr-xr-x 3,638 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/sh

# Disk usage analyzer plugin for Clifm
# Written by L. Abramovich
# License: GPL2+
# Description: Navigate the filesystem with FZF and display directories size.
# Dependencies: du, fzf, grep

# Usage:
# Left: go to parent directory
# Right, Enter: cd into hovered directory or open hovered file and exit.
# Home/end: go to first/last file
# TAB: Select files
# Ctrl-s: Confirm selection: send selected files to Clifm's Selbox
# Shift+up/down: Move one line up/down in the preview window
# Alt+up/down: Move to the beginning/end in the preview window
# Press Esc or Ctrl-q to exit.

if [ -n "$1" ] && { [ "$1" = "--help" ] || [ "$1" = "-h" ]; }; then
	name="${CLIFM_PLUGIN_NAME:-$(basename "$0")}"
	printf "Analiyze disk usage via du and FZF\n"
	printf "\n\x1b[1mUSAGE\x1b[0m\n  %s [DIR]\n" "$name"
	exit 0
fi

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

HELP="Usage:
Type in the prompt to filter the current list of files. Regular expressions are \
allowed.

Keybindings:
Left: go to parent directory
Right or Enter: cd into hovered directory or open hovered file and exit
Home/end: go to first/last file in the files list
Shift-up/down: Move one line up/down in the preview window
Alt-up/down: Move to the beginning/end in the preview window
Esc/Ctrl-q: Exit"

fcd() {
	if [ "$#" -ne 0 ]; then
		cd "$@" || return
	fi

	if type dircolors > /dev/null 2>&1; then
		dir_color="$(dircolors -c | grep -o "[\':]di=....." | cut -d';' -f2)"
	fi
	[ -z "$dir_color" ] && dir_color="34"

	# Source our plugins helper
	if [ -z "$CLIFM_PLUGINS_HELPER" ] || ! [ -f "$CLIFM_PLUGINS_HELPER" ]; then
		printf "clifm: Unable to find plugins-helper file\n" >&2
		exit 1
	fi

	# shellcheck source=/dev/null
	. "$CLIFM_PLUGINS_HELPER"

	_borders="$(fzf_borders)"
	_colors="$(get_fzf_colors)"

	# Keep FZF running until the user presses Esc or C-q
	while true; do
		lsd=$(printf "\033[0;%sm..\n" "$dir_color"; ls --color=always --group-directories-first --indicator-style=none -A .)
		# shellcheck disable=SC2154
		file="$(printf "%s\n" "$lsd" | fzf \
			--color="$_colors" --height "$fzf_height" \
			--bind "right:accept,left:first+accept" \
			--bind "insert:clear-query" \
			--bind "home:first,end:last" \
			--bind "alt-h:preview(printf %s \"$HELP\")" \
			--bind "alt-p:toggle-preview" \
			--bind "shift-up:preview-up" \
			--bind "shift-down:preview-down" \
			--bind "alt-up:preview-page-up" \
			--bind "alt-down:preview-page-down" \
			--bind "esc:abort" \
			--bind "ctrl-q:abort" \
			--ansi --prompt="$fzf_prompt" --reverse --no-clear \
			--no-info --keep-right --multi --header="Press 'Alt-h' for help
$PWD
$FZF_HEADER" --marker="*" --preview-window=:wrap "$_borders" \
--preview="printf \"Total: \"; du $DU_OPTS1 {} 2>/dev/null | cut -f1; du $DU_OPTS2 {}/* 2>/dev/null | sort $SORT_OPTS")"

		# If FZF returned no file, exit
		[ ${#file} -eq 0 ] && return 0

		# If the returned file is a directory, just cd into it. Otherwise, exit
		if [ -d "${PWD}/$file" ]; then
			cd "$file" || return
		fi
	done
}

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

	OS="$(uname -s)"
	if [ "$OS" = "NetBSD" ]; then
		# NetBSD sort(1) cannot sort human readable sizes (-h)
		export DU_OPTS1="-s"
		export DU_OPTS2="-d0"
		export SORT_OPTS="-nr"
	else
		export DU_OPTS1="-hs"
		export DU_OPTS2="-hd0"
		export SORT_OPTS="-hr"
	fi
	fcd "$@"
}

main "$@"

# Erase the FZF window
_lines="${LINES:-100}"
printf "\033[%dM" "$_lines"

exit 0