Skip to content

Commit

Permalink
Merge pull request #4371 from rouault/projinfo_bash_completion
Browse files Browse the repository at this point in the history
Add bash completion script for projinfo
  • Loading branch information
rouault authored Jan 15, 2025
2 parents 98dcc57 + d887cad commit 27cf3a9
Show file tree
Hide file tree
Showing 6 changed files with 471 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ if(BUILD_EXAMPLES)
add_subdirectory(examples)
endif()

add_subdirectory(scripts)

set(docfiles COPYING NEWS.md AUTHORS.md)
install(FILES ${docfiles}
DESTINATION ${CMAKE_INSTALL_DOCDIR})
Expand Down Expand Up @@ -459,7 +461,6 @@ set(CPACK_SOURCE_IGNORE_FILES
/m4/libtool*
/media/
/schemas/
/scripts/
/test/fuzzers/
/test/gigs/.*gie\\.failing
/test/postinstall/
Expand Down
28 changes: 28 additions & 0 deletions scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
find_package(PkgConfig QUIET)
if (PKG_CONFIG_FOUND)
pkg_check_modules(PC_BASH_COMPLETION QUIET bash-completion)
if (PC_BASH_COMPLETION_FOUND)
pkg_get_variable(BASH_COMPLETIONS_FULL_DIR bash-completion completionsdir)
pkg_get_variable(BASH_COMPLETIONS_PREFIX bash-completion prefix)
if (BASH_COMPLETIONS_FULL_DIR
AND BASH_COMPLETIONS_PREFIX
AND BASH_COMPLETIONS_FULL_DIR MATCHES "^${BASH_COMPLETIONS_PREFIX}/")
string(REGEX REPLACE "^${BASH_COMPLETIONS_PREFIX}/" "" BASH_COMPLETIONS_DIR_DEFAULT ${BASH_COMPLETIONS_FULL_DIR})
endif ()
endif ()
endif ()

if (NOT DEFINED BASH_COMPLETIONS_DIR_DEFAULT)
include(GNUInstallDirs)
set(BASH_COMPLETIONS_DIR_DEFAULT ${CMAKE_INSTALL_DATADIR}/bash-completion/completions)
endif ()

set(BASH_COMPLETIONS_DIR
"${BASH_COMPLETIONS_DIR_DEFAULT}"
CACHE PATH "Installation sub-directory for bash completion scripts")

if (NOT BASH_COMPLETIONS_DIR STREQUAL "")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/install_bash_completions.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/install_bash_completions.cmake @ONLY)
install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/install_bash_completions.cmake)
endif ()
13 changes: 13 additions & 0 deletions scripts/install_bash_completions.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
set(PROGRAMS
projinfo
)

set(INSTALL_DIR "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/@BASH_COMPLETIONS_DIR@")

file(MAKE_DIRECTORY "${INSTALL_DIR}")

foreach (program IN LISTS PROGRAMS)
message(STATUS "Installing ${INSTALL_DIR}/${program}")
configure_file("@CMAKE_CURRENT_SOURCE_DIR@/${program}-bash-completion.sh" "${INSTALL_DIR}/${program}" COPYONLY)
file(APPEND "@PROJECT_BINARY_DIR@/install_manifest_extra.txt" "${INSTALL_DIR}/${program}\n")
endforeach ()
51 changes: 51 additions & 0 deletions scripts/projinfo-bash-completion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash

function_exists() {
declare -f -F "$1" > /dev/null
return $?
}

# Checks that bash-completion is recent enough
function_exists _get_comp_words_by_ref || return 0

_projinfo()
{
local cur prev
COMPREPLY=()
_get_comp_words_by_ref cur prev
choices=$(projinfo completion ${COMP_LINE})
if [[ "$cur" == "=" ]]; then
mapfile -t COMPREPLY < <(compgen -W "$choices" --)
elif [[ "$cur" == ":" ]]; then
mapfile -t COMPREPLY < <(compgen -W "$choices" --)
elif [[ "${cur: -1}" == "/" ]]; then
mapfile -t COMPREPLY < <(compgen -W "$choices" --)
elif [[ "${cur: -2}" == "/ " ]]; then
mapfile -t COMPREPLY < <(compgen -W "$choices" --)
elif [[ "${cur: -1}" == "+" ]]; then
mapfile -t COMPREPLY < <(compgen -W "$choices" --)
elif [[ "${cur: -2}" == "+ " ]]; then
mapfile -t COMPREPLY < <(compgen -W "$choices" --)
elif [[ "$cur" == "!" ]]; then
mapfile -t COMPREPLY < <(compgen -W "$choices" -P "! " --)
else
mapfile -t COMPREPLY < <(compgen -W "$choices" -- "$cur")
fi
for element in "${COMPREPLY[@]}"; do
if [[ $element == */ ]]; then
# Do not add a space if one of the suggestion ends with slash
compopt -o nospace
break
elif [[ $element == *= ]]; then
# Do not add a space if one of the suggestion ends with equal
compopt -o nospace
break
elif [[ $element == *: ]]; then
# Do not add a space if one of the suggestion ends with colon
compopt -o nospace
break
fi
done
}
complete -o default -F _projinfo projinfo

Loading

0 comments on commit 27cf3a9

Please sign in to comment.