Class: DocoptCompgen::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/docopt_compgen/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(command, node, command_name: nil, namespace: nil, header: nil) ⇒ Generator

Returns a new instance of Generator.



3
4
5
6
7
8
9
# File 'lib/docopt_compgen/generator.rb', line 3

def initialize(command, node, command_name: nil, namespace: nil, header: nil)
    @command = command ? File.basename(command) : command_name
    @node = node
    @command_name = command_name || Util.slugify(@command)
    @namespace = '_' + namespace
    @header = header
end

Instance Method Details

#get_op(node) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/docopt_compgen/generator.rb', line 15

def get_op(node)
    if node.subcommands.length > 0
        'eq'
    else
        'ge'
    end
end

#include_files?(node) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/docopt_compgen/generator.rb', line 31

def include_files?(node)
    return path_arguments?(node.arguments) || path_options?(node.options)
end

#indent(str, level) ⇒ Object



11
12
13
# File 'lib/docopt_compgen/generator.rb', line 11

def indent(str, level)
    "\n" + str.strip.lines.map { |s| (' ' * level) + s }.join
end

#make(command_name, node, level) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/docopt_compgen/generator.rb', line 47

def make(command_name, node, level)
    subcommand_switch = make_subcommand_switch(
        command_name,
        level,
        node.subcommands,
    )

    op = get_op(node)
    include_files, words = make_compreply(node)

    functions = [
        make_function(
            command_name,
            op,
            level,
            words,
            include_files,
            subcommand_switch,
        ),
    ]

    node.subcommands.each do |subcommand_name, subcommand_node|
        functions << make(
            '%s_%s' % [command_name, subcommand_name],
            subcommand_node,
            level + 1,
        )
    end

    return functions.join("\n")
end

#make_compreply(node) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/docopt_compgen/generator.rb', line 35

def make_compreply(node)
    words = []

    if node.options.length > 0
        words << node.options
    end

    words << node.subcommands.keys

    return [include_files?(node), words.flatten.join(' ')]
end

#make_function(command_name, op, level, words, include_files, subcommand_switch) ⇒ Object

rubocop:disable Metrics/ParameterLists



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/docopt_compgen/generator.rb', line 79

def make_function(command_name, op, level, words, include_files, subcommand_switch) # rubocop:disable Metrics/ParameterLists
    files = ''
    if include_files
        files = indent(<<~EOF, 8)
            local IFS=$'\\n' # Handle filenames with spaces.
            COMPREPLY+=($(compgen -f -- "$cur"))
        EOF
    end

    return <<~EOF
        function #{@namespace}_#{command_name} {
            local cur
            cur="${COMP_WORDS[COMP_CWORD]}"

            if [ "$COMP_CWORD" -#{op} #{level} ]; then
                COMPREPLY=($(compgen -W '#{words}' -- "$cur"))#{files}#{subcommand_switch}
            fi
        }
    EOF
end

#make_subcommand_switch(command_name, level, subcommands) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/docopt_compgen/generator.rb', line 100

def make_subcommand_switch(command_name, level, subcommands)
    if subcommands.length == 0
        return ''
    end

    cases = subcommands.map do |subcommand_name, _node|
        "#{subcommand_name}) #{@namespace}_#{command_name}_#{subcommand_name} ;;"
    end.join("\n        ")

    return indent(<<~EOF, 4)
        else
            case ${COMP_WORDS[#{level}]} in
                #{cases}
            esac
    EOF
end

#path_arguments?(arguments) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/docopt_compgen/generator.rb', line 23

def path_arguments?(arguments)
    arguments.any? { |a| %w[<file> <dir> <path>].include?(a) }
end

#path_options?(options) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/docopt_compgen/generator.rb', line 27

def path_options?(options)
    options.any? { |a| %w[--file --dir --path].include?(a) }
end

#to_sObject



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/docopt_compgen/generator.rb', line 117

def to_s
    content = make(@command_name, @node, 1)

    return <<~EOF
        #!/bin/bash
        # shellcheck disable=SC2207
        #{@header ? @header + "\n" : ''}
        #{content}
        complete -o bashdefault -o default -o filenames -F #{@namespace}_#{@command_name} #{@command}
    EOF
end