Skip to content

Commit 27cf3a9

Browse files
authored
Merge pull request #4371 from rouault/projinfo_bash_completion
Add bash completion script for projinfo
2 parents 98dcc57 + d887cad commit 27cf3a9

File tree

6 files changed

+471
-1
lines changed

6 files changed

+471
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@ if(BUILD_EXAMPLES)
415415
add_subdirectory(examples)
416416
endif()
417417

418+
add_subdirectory(scripts)
419+
418420
set(docfiles COPYING NEWS.md AUTHORS.md)
419421
install(FILES ${docfiles}
420422
DESTINATION ${CMAKE_INSTALL_DOCDIR})
@@ -459,7 +461,6 @@ set(CPACK_SOURCE_IGNORE_FILES
459461
/m4/libtool*
460462
/media/
461463
/schemas/
462-
/scripts/
463464
/test/fuzzers/
464465
/test/gigs/.*gie\\.failing
465466
/test/postinstall/

scripts/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
find_package(PkgConfig QUIET)
2+
if (PKG_CONFIG_FOUND)
3+
pkg_check_modules(PC_BASH_COMPLETION QUIET bash-completion)
4+
if (PC_BASH_COMPLETION_FOUND)
5+
pkg_get_variable(BASH_COMPLETIONS_FULL_DIR bash-completion completionsdir)
6+
pkg_get_variable(BASH_COMPLETIONS_PREFIX bash-completion prefix)
7+
if (BASH_COMPLETIONS_FULL_DIR
8+
AND BASH_COMPLETIONS_PREFIX
9+
AND BASH_COMPLETIONS_FULL_DIR MATCHES "^${BASH_COMPLETIONS_PREFIX}/")
10+
string(REGEX REPLACE "^${BASH_COMPLETIONS_PREFIX}/" "" BASH_COMPLETIONS_DIR_DEFAULT ${BASH_COMPLETIONS_FULL_DIR})
11+
endif ()
12+
endif ()
13+
endif ()
14+
15+
if (NOT DEFINED BASH_COMPLETIONS_DIR_DEFAULT)
16+
include(GNUInstallDirs)
17+
set(BASH_COMPLETIONS_DIR_DEFAULT ${CMAKE_INSTALL_DATADIR}/bash-completion/completions)
18+
endif ()
19+
20+
set(BASH_COMPLETIONS_DIR
21+
"${BASH_COMPLETIONS_DIR_DEFAULT}"
22+
CACHE PATH "Installation sub-directory for bash completion scripts")
23+
24+
if (NOT BASH_COMPLETIONS_DIR STREQUAL "")
25+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/install_bash_completions.cmake.in
26+
${CMAKE_CURRENT_BINARY_DIR}/install_bash_completions.cmake @ONLY)
27+
install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/install_bash_completions.cmake)
28+
endif ()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
set(PROGRAMS
2+
projinfo
3+
)
4+
5+
set(INSTALL_DIR "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/@BASH_COMPLETIONS_DIR@")
6+
7+
file(MAKE_DIRECTORY "${INSTALL_DIR}")
8+
9+
foreach (program IN LISTS PROGRAMS)
10+
message(STATUS "Installing ${INSTALL_DIR}/${program}")
11+
configure_file("@CMAKE_CURRENT_SOURCE_DIR@/${program}-bash-completion.sh" "${INSTALL_DIR}/${program}" COPYONLY)
12+
file(APPEND "@PROJECT_BINARY_DIR@/install_manifest_extra.txt" "${INSTALL_DIR}/${program}\n")
13+
endforeach ()
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
3+
function_exists() {
4+
declare -f -F "$1" > /dev/null
5+
return $?
6+
}
7+
8+
# Checks that bash-completion is recent enough
9+
function_exists _get_comp_words_by_ref || return 0
10+
11+
_projinfo()
12+
{
13+
local cur prev
14+
COMPREPLY=()
15+
_get_comp_words_by_ref cur prev
16+
choices=$(projinfo completion ${COMP_LINE})
17+
if [[ "$cur" == "=" ]]; then
18+
mapfile -t COMPREPLY < <(compgen -W "$choices" --)
19+
elif [[ "$cur" == ":" ]]; then
20+
mapfile -t COMPREPLY < <(compgen -W "$choices" --)
21+
elif [[ "${cur: -1}" == "/" ]]; then
22+
mapfile -t COMPREPLY < <(compgen -W "$choices" --)
23+
elif [[ "${cur: -2}" == "/ " ]]; then
24+
mapfile -t COMPREPLY < <(compgen -W "$choices" --)
25+
elif [[ "${cur: -1}" == "+" ]]; then
26+
mapfile -t COMPREPLY < <(compgen -W "$choices" --)
27+
elif [[ "${cur: -2}" == "+ " ]]; then
28+
mapfile -t COMPREPLY < <(compgen -W "$choices" --)
29+
elif [[ "$cur" == "!" ]]; then
30+
mapfile -t COMPREPLY < <(compgen -W "$choices" -P "! " --)
31+
else
32+
mapfile -t COMPREPLY < <(compgen -W "$choices" -- "$cur")
33+
fi
34+
for element in "${COMPREPLY[@]}"; do
35+
if [[ $element == */ ]]; then
36+
# Do not add a space if one of the suggestion ends with slash
37+
compopt -o nospace
38+
break
39+
elif [[ $element == *= ]]; then
40+
# Do not add a space if one of the suggestion ends with equal
41+
compopt -o nospace
42+
break
43+
elif [[ $element == *: ]]; then
44+
# Do not add a space if one of the suggestion ends with colon
45+
compopt -o nospace
46+
break
47+
fi
48+
done
49+
}
50+
complete -o default -F _projinfo projinfo
51+

0 commit comments

Comments
 (0)