Skip to content

Commit 60103cd

Browse files
committed
Add shell completion for mkdwarfs (bash, zsh)
- Basic completion for common shells for mkdwarfs command. - Installed only when WITH_TOOLS=ON and NOT WIN32. - Potentially other commands could be added, but mkdwarfs is the most complicated of them. - It's not perfect completion and could use some improvements to "compression" options that allow a sophisticated syntax including category/algo/algo_options.
1 parent 343d1f8 commit 60103cd

File tree

3 files changed

+274
-0
lines changed

3 files changed

+274
-0
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,14 @@ if(WITH_TOOLS)
295295
endif()
296296
endforeach()
297297

298+
if(NOT WIN32)
299+
file(GLOB SHELLCOMP_BASH_FILES ${CMAKE_CURRENT_SOURCE_DIR}/doc/completions/bash/*)
300+
file(GLOB SHELLCOMP_ZSH_FILES ${CMAKE_CURRENT_SOURCE_DIR}/doc/completions/zsh/*)
301+
install(FILES "${SHELLCOMP_BASH_FILES}" DESTINATION share/bash-completion/completions)
302+
install(FILES "${SHELLCOMP_ZSH_FILES}" DESTINATION share/zsh/vendor-completions)
303+
endif()
304+
305+
298306
target_link_libraries(mkdwarfs_main PRIVATE dwarfs_reader dwarfs_writer dwarfs_rewrite)
299307
target_link_libraries(dwarfsck_main PRIVATE dwarfs_reader)
300308
target_link_libraries(dwarfsextract_main PRIVATE dwarfs_extractor)

doc/completions/bash/_mkdwarfs

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# SPDX-License-Identifier: MIT
2+
# Author: Ahmad Khalifa
3+
#
4+
# bash completion for mkdwarfs
5+
#
6+
# synopsis
7+
# mkdwarfs [OPTIONS...]
8+
#
9+
10+
# TODO: unreliable? maybe not if app keeps the two groups intact.
11+
__mkdwarfs_list_comp_algos()
12+
{
13+
# extract algorithms block
14+
# trim algo leading space, delete algo args, remove empty line
15+
# delete algo descriptions
16+
# double print with trailing ':' except 'null' - they complete with no space
17+
mkdwarfs -H | \
18+
sed -e '1,/Compression algorithms/d;/Categories:/,$d' \
19+
-e 's/^[ ]\{1,2\}//;/^ /d;/^$/d' \
20+
-e 's/ .*$//' \
21+
-ne 'p;/null/!s/$/:/p'
22+
}
23+
24+
__mkdwarfs_additional_options()
25+
{
26+
if [[ "$(mkdwarfs -h | grep -e ' *--man')" ]]; then
27+
echo "--man"
28+
fi
29+
}
30+
31+
_mkdwarfs_completion()
32+
{
33+
local cur prev words cword
34+
_comp_initialize || return
35+
36+
local OPTIONS_GENERAL=(
37+
--bloom-filter-size
38+
--categorize
39+
--change-block-size
40+
--chmod
41+
--compress-niceness
42+
--debug-filter
43+
--file-hash
44+
--header
45+
--history-compression
46+
--hotness-list
47+
--incompressible-block-size
48+
--incompressible-fragments
49+
--incompressible-min-input-size
50+
--incompressible-ratio
51+
--incompressible-zstd-level
52+
--input-list
53+
--keep-all-times
54+
--log-level
55+
--log-with-context
56+
--man
57+
--max-similarity-size
58+
--metadata-compression
59+
--no-category-metadata
60+
--no-category-names
61+
--no-create-timestamp
62+
--no-history
63+
--no-history-command-line
64+
--no-history-timestamps
65+
--no-metadata-version-history
66+
--no-progress
67+
--no-section-index
68+
--num-scanner-workers
69+
--num-segmenter-workers
70+
--order
71+
--progress
72+
--rebuild-metadata
73+
--recompress
74+
--recompress-categories
75+
--remove-empty-dirs
76+
--remove-header
77+
--schema-compression
78+
--set-group
79+
--set-owner
80+
--set-time
81+
--time-resolution
82+
--with-devices
83+
--with-specials
84+
-B --max-lookback-blocks
85+
-C --compression
86+
-F --filter
87+
-f --force
88+
-h --help
89+
-H --long-help
90+
-i --input
91+
-l --compress-level
92+
-L --memory-limit
93+
-N --num-workers
94+
-o --output
95+
-P --pack-metadata
96+
-S --block-size-bits
97+
-W --window-size
98+
-w --window-step
99+
$(__mkdwarfs_additional_options)
100+
)
101+
102+
local OPTION_ARG__log_level=( error warn info verbose debug trace )
103+
local OPTION_ARG__compress_level=( 0 1 2 3 4 5 6 7 8 9 )
104+
local OPTION_ARG__recompress=( none block metadata all )
105+
local OPTION_ARG__categorize=( fits pcmaudio incompressible )
106+
# TODO: find a better way to extract these at runtime
107+
local OPTION_ARG__file_hash=( )
108+
local OPTION_ARG__progress=( ascii none simple unicode )
109+
local OPTION_ARG__pack_metadata=( auto all none chunk_table directories
110+
shared_files names names_index symlinks symlinks_index force plain )
111+
112+
# catch option with known arguments first
113+
case $prev in
114+
--log-level | --compress-level | --recompress | \
115+
--categorize | --file-hash | --progress | --pack-metadata)
116+
prevoption=${prev//-/_}
117+
_comp_compgen -- -W '"${OPTION_ARG'$prevoption'[@]}"'
118+
return 0
119+
;;
120+
-P)
121+
_comp_compgen -- -W '"${OPTION_ARG__pack_metadata[@]}"'
122+
return 0
123+
;;
124+
-l)
125+
_comp_compgen -- -W '"${OPTION_ARG__compress_level[@]}"'
126+
return 0
127+
;;
128+
--set-owner)
129+
_comp_compgen -- uids
130+
return 0
131+
;;
132+
--set-group)
133+
_comp_compgen -- gids
134+
return 0
135+
;;
136+
-i | --input)
137+
_comp_compgen -a filedir -d
138+
return 0
139+
;;
140+
--input-list | -o | --output)
141+
_comp_compgen -a filedir
142+
return 0
143+
;;
144+
--compression | --schema-compression | \
145+
--metadata-compression | --history-compression)
146+
# TODO: complete algo args based $prev and ':' or ','
147+
# --prev <algo>:<arg1>=<_>,<arg2>=<_>
148+
_comp_compgen -- -W '$(__mkdwarfs_list_comp_algos)'
149+
return 0
150+
;;
151+
esac
152+
153+
154+
if [[ $cur == -* ]]; then
155+
# cursor on an option, show options only
156+
_comp_compgen -- -W '"${OPTIONS_GENERAL[@]}"'
157+
else
158+
# show all options and files
159+
_comp_compgen -- -W '"${OPTIONS_GENERAL[@]}"'
160+
_comp_compgen -a filedir
161+
fi
162+
163+
return 0
164+
} &&
165+
complete -F _mkdwarfs_completion mkdwarfs

doc/completions/zsh/_mkdwarfs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#compdef mkdwarfs
2+
#
3+
# SPDX-License-Identifier: MIT
4+
# Author: Ahmad Khalifa
5+
#
6+
# zsh completion for mkdwarfs
7+
#
8+
# synopsis
9+
# mkdwarfs [OPTIONS...]
10+
#
11+
12+
local context state line ret=1
13+
14+
# TODO: unreliable? maybe not if app keeps the two groups intact.
15+
__mkdwarfs_list_comp_algos()
16+
{
17+
# extract algorithms block
18+
# trim algo leading space, delete algo args, remove empty line
19+
# delete algo descriptions
20+
# double print with trailing '\:' except 'null'
21+
mkdwarfs -H | \
22+
sed -e '1,/Compression algorithms/d;/Categories:/,$d' \
23+
-e 's/^[ ]\{1,2\}//;/^ /d;/^$/d' \
24+
-e 's/ .*$//' \
25+
-ne 'p;/null/!s/$/\\:/p'
26+
}
27+
28+
__mkdwarfs_additional_options()
29+
{
30+
if [[ "$(mkdwarfs -h | grep -e ' *--man')" ]]; then
31+
echo "--man"
32+
fi
33+
}
34+
35+
_arguments -S \
36+
"--bloom-filter-size" \
37+
"--categorize=-:cattype:_values -s , cattype fits pcmaudio incompressible" \
38+
"--change-block-size" \
39+
"--chmod" \
40+
"--compress-niceness" \
41+
"--debug-filter" \
42+
"--file-hash:hashfnc:" \
43+
"--header" \
44+
"--history-compression:algos:($(__mkdwarfs_list_comp_algos))" \
45+
"--hotness-list" \
46+
"--incompressible-block-size" \
47+
"--incompressible-fragments" \
48+
"--incompressible-min-input-size" \
49+
"--incompressible-ratio" \
50+
"--incompressible-zstd-level" \
51+
"--input-list[file containing list of file paths relative to root directory or - for stdin]:filename:_files" \
52+
"--keep-all-times[save atime and ctime in addition to mtime]" \
53+
"--log-level:level:(error warn info verbose debug trace)" \
54+
"--log-with-context" \
55+
"--max-similarity-size" \
56+
"--metadata-compression:algos:($(__mkdwarfs_list_comp_algos))" \
57+
"--no-category-metadata" \
58+
"--no-category-names" \
59+
"--no-create-timestamp" \
60+
"--no-history" \
61+
"--no-history-command-line" \
62+
"--no-history-timestamps" \
63+
"--no-metadata-version-history" \
64+
"--no-progress" \
65+
"--no-section-index" \
66+
"--num-scanner-workers" \
67+
"--num-segmenter-workers" \
68+
"--order" \
69+
"--progress=-:progress:(ascii none simple unicode)" \
70+
"--rebuild-metadata" \
71+
"--recompress:level:(none block metadata all)" \
72+
"--recompress-categories" \
73+
"--remove-empty-dirs" \
74+
"--remove-header" \
75+
"--schema-compression:algos:($(__mkdwarfs_list_comp_algos))" \
76+
"--set-group[group (gid) for whole file system]" \
77+
"--set-owner[owner (uid) for whole file system]" \
78+
"--set-time[timestamp for whole file system (unixtime or 'now')]" \
79+
"--time-resolution" \
80+
"--with-devices" \
81+
"--with-specials" \
82+
{-B,--max-lookback-blocks}"[blocks to scan for segments]" \
83+
{-C,--compression}"[block compression algorithm]:algos:($(__mkdwarfs_list_comp_algos))" \
84+
{-F,--filter} \
85+
{-f,--force}"[force overwrite of existing output image]" \
86+
{-h,--help}"[help message]" \
87+
{-H,--long-help}"[full help message]" \
88+
{-i,--input}"[path to root directory or source filesystem]:directory:_files -/" \
89+
{-l,--compress-level}"[compression level (0=fast, 9=best)]:level:(0 1 2 3 4 5 6 7 8 9)" \
90+
{-L,--memory-limit} \
91+
{-N,--num-workers}"[number of writer (compression) worker threads]" \
92+
{-o,--output}"[filesystem output name or - for stdout]:filename:_files" \
93+
{-P,--pack-metadata}":packtype:_values packtype auto all none \
94+
chunk_table directories shared_files names names_index symlinks \
95+
symlinks_index force plain" \
96+
{-S,--block-size-bits} \
97+
{-W,--window-size} \
98+
{-w,--window-step} \
99+
$(__mkdwarfs_additional_options) && ret=0
100+
101+
return ret

0 commit comments

Comments
 (0)