Skip to content
Snippets Groups Projects
Select Git revision
  • 87f887d624b1001bb0933c820145b89885570da5
  • main default protected
  • jw_sonar
  • v6.0.0 protected
  • interactive-mode-preference
  • bedran_exercise-list
  • add_route_user
  • Jw_sonar_backup
  • exercise_list_filter
  • assignment_filter
  • add_route_assignments
  • move-to-esm-only
  • 6.0.0-dev
  • Pre-alpha
  • 5.0.0
  • Latest
  • 4.2.0
  • 4.1.1
  • 4.1.0
  • 4.0.1
  • 4.0.0
  • 3.5.0
  • 3.4.2
  • 3.4.1
  • 3.3.0
  • 3.2.3
  • 3.2.2
  • 3.2.0
  • 3.1.2
  • 3.1.1
  • 3.1.0
  • 3.0.1
32 results

CompletionScriptCommand.ts

Blame
  • CompletionScriptCommand.ts 1.78 KiB
    import CommanderCommand from '../../CommanderCommand';
    import { Argument }     from 'commander';
    
    
    class CompletionScriptCommand extends CommanderCommand {
        protected commandName: string = 'script';
    
        protected defineCommand() {
            this.command.description('generate script completion')
                .addArgument(new Argument('<shell>', 'shell completion format').choices([ 'bash', 'zsh' ]))
                .action(this.commandAction.bind(this));
        }
    
        private bashCompletionScript() {
            console.log(`
    #/usr/bin/env bash
    ###-begin-dojo-completions-###
    #
    # dojo command completion script for bash
    #
    # Installation: dojo completion bash
    #
    function _dojo_completions()
    {
        latest="\${COMP_WORDS[$COMP_CWORD]}"
        words=$(dojo completion get --shell bash \${COMP_WORDS[@]})
    
        COMPREPLY=($(compgen -W "$words" -- $latest))
        return 0
    }
    complete -F _dojo_completions dojo
    ###-end-dojo-completions-###
            `);
        }
    
        private zshCompletionScript() {
            console.log(`
    #compdef dojo
    ###-begin-dojo-completions-###
    #
    # dojo command completion script for zsh
    #
    # Installation: dojo completion zsh
    #
    _dojo_completions()
    {
      local reply
      local si=$IFS
      IFS=$'
    ' reply=($(dojo completion get --shell zsh \${words[@]}))
      IFS=$si
      _describe 'values' reply
    }
    compdef _dojo_completions dojo
    ###-end-dojo-completions-###
            `);
        }
    
        protected async commandAction(shell: 'bash' | 'zsh'): Promise<void> {
            switch ( shell ) {
                case 'bash':
                    this.bashCompletionScript();
                    break;
                case 'zsh':
                    this.zshCompletionScript();
                    break;
                default:
                    console.error('Unsupported shell completion format');
                    break;
            }
        }
    }
    
    
    export default new CompletionScriptCommand();