Last active
April 7, 2022 00:33
-
-
Save jpouellet/6143329 to your computer and use it in GitHub Desktop.
Bash autocompletion plugin for TACC's module system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Bash autocompletion module for TACC's lmod | |
# http://www.tacc.utexas.edu/tacc-projects/mclay/lmod | |
# Written by Jean-Philippe Ouellet <jpo@vt.edu> at ARC | |
# http://www.arc.vt.edu/ | |
# Try to intelligently narrow the completion suggestions | |
# instead of showing all suggestions all the time. | |
[ -z "$_LMOD_INTELLIGENT_GUESSING" ] && _LMOD_INTELLIGENT_GUESSING=true | |
# Enable completion of alias commands | |
[ -z "$_LMOD_USE_ALIASES" ] && _LMOD_USE_ALIASES=true | |
_lmod_loadable() { | |
module avail "^$1" 2>&1 \ | |
| grep '^ .*/' \ | |
| tr ' ' '\n' \ | |
| grep / | |
return 0 | |
} | |
_lmod_unloadable() { | |
module list "^$1" 2>&1 \ | |
| grep -vF 'Currently Loaded Modules Matching: ' \ | |
| tr ' ' '\n' \ | |
| grep / | |
return 0 | |
} | |
_lmod_all() { | |
module spider "^$1" 2>&1 \ | |
| grep -E '^ [^ ]+:' \ | |
| sed 's/:.*$//; s/^[ ]*//' | |
} | |
_lmod_presets() { | |
[[ "system" == "$1"* ]] && echo system | |
module savelist 2>&1 \ | |
| grep -E '^ [0-9]+\) ' \ | |
| tr ' ' '\n' \ | |
| grep -vE '^[0-9]+\)$|^$' \ | |
| grep "^$1" | |
return 0 | |
} | |
_lmod() { | |
local primary_commands="help load try-load unload swap purge list avail spider whatis" | |
local uncommon_commands="save restore savelist show use unuse tablelist" | |
local alias_commands | |
if $_LMOD_USE_ALIASES; then | |
alias_commands="add try-add rm del switch sw av" | |
# aliased to: load try-load unload unload swap swap avail | |
fi | |
# This script ignores the following commands: | |
# reset deprecated in favor of 'restore system' | |
# getdefault deprecated in favor of 'restore' | |
# setdefault deprecated in favor of 'save' | |
# record isn't very useful and shorthand competes with 'restore' | |
this="${COMP_WORDS[COMP_CWORD]}" | |
if [ "$COMP_CWORD" -eq 1 ]; then | |
COMPREPLY=($(compgen -W "${primary_commands}" "$this")) | |
if ! $_LMOD_INTELLIGENT_GUESSING; then | |
COMPREPLY+=($(compgen -W "$uncommon_commands $alias_commands" "$this")) | |
elif [ -z "$this" -o "${#COMPREPLY[@]}" -gt 1 ]; then | |
COMPREPLY+=($(compgen -W "$uncommon_commands" "$this")) | |
elif [ "${#COMPREPLY[@]}" -eq 0 ]; then | |
COMPREPLY+=($(compgen -W "$uncommon_commands" "$this")) | |
if [ "${#COMPREPLY[@]}" -ne 1 ]; then | |
COMPREPLY+=($(compgen -W "$alias_commands" "$this")) | |
fi | |
fi | |
return 0 | |
fi | |
# not sure if try-load and try-add should complete to loadable or any | |
case "${COMP_WORDS[1]}" in | |
"load" | "add") | |
# <loadable module> ... | |
COMPREPLY=($(_lmod_loadable "$this")) | |
;; | |
"unload" | "rm" | "del") | |
# <unloadable module> ... | |
COMPREPLY=($(_lmod_unloadable "$this")) | |
;; | |
"spider" | "whatis" | "show" | "try-load" | "try-add") | |
# <any module> ... | |
COMPREPLY=($(_lmod_all "$this")) | |
;; | |
"swap" | "switch" | "sw") | |
# <loaded module> <unloaded module> | |
if [ "$COMP_CWORD" -eq 2 ]; then | |
COMPREPLY=($(_lmod_unloadable "$this")) | |
elif [ "$COMP_CWORD" -eq 3 ]; then | |
COMPREPLY=($(_lmod_loadable "$this")) | |
fi | |
;; | |
"restore" | "getdefault") | |
# preset | |
COMPREPLY=($(_lmod_presets "$this")) | |
;; | |
"save" | "setdefault") | |
# any arg, but still suggest presets in case you want to overwrite one | |
COMPREPLY=($(_lmod_presets "$this")) | |
;; | |
"use") | |
# [--append] path | |
if [ "$COMP_CWORD" -eq 2 ]; then | |
COMPREPLY=($(compgen -W "--append" "$this")) | |
fi | |
;; | |
"unuse") | |
# $MODULEPATH component | |
COMPREPLY=($(compgen -W "$(echo $MODULEPATH | tr ':' '\n')" "$this")) | |
;; | |
esac | |
# The following commands take no arguments: | |
# help purge reset tablelist record | |
# The following commands take free-form arguments: | |
# list avail av savelist | |
return 0 | |
} | |
complete -o dirnames -F _lmod module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment