Module: Cani::Completions
- Defined in:
- lib/cani/completions.rb
Class Method Summary collapse
- .compile_source_line(path) ⇒ Object
- .completion_path(shell) ⇒ Object
- .delete_source_lines! ⇒ Object
- .generate_bash ⇒ Object
- .generate_fish ⇒ Object
- .generate_zsh ⇒ Object
- .insert_source_lines! ⇒ Object
- .install! ⇒ Object
- .paths ⇒ Object
- .remove! ⇒ Object
Class Method Details
.compile_source_line(path) ⇒ Object
91 92 93 94 95 |
# File 'lib/cani/completions.rb', line 91 def self.compile_source_line(path) return "test -e #{path}; and source #{path}" if path.end_with? '.fish' "[ -f #{path} ] && source #{path}" end |
.completion_path(shell) ⇒ Object
85 86 87 88 89 |
# File 'lib/cani/completions.rb', line 85 def self.completion_path(shell) return File.join Cani.config.fish_comp_dir, 'cani.fish' if shell == :fish File.join Cani.config.comp_dir, "_cani.#{shell}" end |
.delete_source_lines! ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/cani/completions.rb', line 97 def self.delete_source_lines! return unless Cani.config.modify_shell_configs %w[bash zsh].each do |shell| shellrc = File.join Dir.home, ".#{shell}rc" next unless File.exist? shellrc lines = File.read(shellrc).split "\n" comp_path = completion_path shell comp_src = compile_source_line comp_path rm_idx = lines.find_index { |l| l.match comp_src } lines.delete_at rm_idx unless rm_idx.nil? File.write shellrc, lines.join("\n") end end |
.generate_bash ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/cani/completions.rb', line 38 def self.generate_bash gem_root = File.join File.dirname(__FILE__), '../../' tpl = File.read File.join(gem_root, 'shell/completions/functions.bash') indent = 10 versions = Cani.api.browsers.reduce String.new do |acc, browser| acc + (' ' * (indent - 2)) + '"' + browser.abbr + "\")\n" + (' ' * indent) + "COMPREPLY=($(compgen -W \"#{browser.versions.join(' ')}\" ${COMP_WORDS[COMP_CWORD]}))\n" + (' ' * indent) + ";;\n" end.strip tpl.gsub('{{names}}', Cani.api.browsers.map(&:abbr).join(' ')) .gsub('{{features}}', Cani.api.features.map(&:name).join(' ')) .gsub '{{versions}}', versions end |
.generate_fish ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/cani/completions.rb', line 3 def self.generate_fish gem_root = File.join File.dirname(__FILE__), '../../' tpl = File.read File.join(gem_root, 'shell/completions/functions.fish') shw = Cani.api.browsers.reduce String.new do |acc, browser| versions = browser.versions.reverse.join(' ') acc + "\ncomplete -f -c cani -n '__fish_cani_using_command show' -a '#{browser.abbr}' -d '#{browser.label}'" + "\ncomplete -f -c cani -n '__fish_cani_showing_browser #{browser.abbr}' -a '#{versions}'" end use = Cani.api.features.reduce String.new do |acc, feature| description = feature.title.size > 40 ? feature.title[0..28] + '..' : feature.title acc + "\ncomplete -f -c cani -n '__fish_cani_using_command use' -a '#{feature.name}' -d '#{description}'" end tpl + shw + "\n" + use end |
.generate_zsh ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/cani/completions.rb', line 23 def self.generate_zsh gem_root = File.join File.dirname(__FILE__), '../../' tpl = File.read File.join(gem_root, 'shell/completions/functions.zsh') indent = 10 versions = Cani.api.browsers.reduce String.new do |acc, browser| acc + (' ' * (indent - 2)) + browser.abbr + ")\n" + (' ' * indent) + "_arguments -C \"1: :(#{browser.versions.join(' ')})\"\n" + (' ' * indent) + ";;\n" end.strip tpl.gsub('{{names}}', Cani.api.browsers.map(&:abbr).join(' ')) .gsub('{{features}}', Cani.api.features.map(&:name).join(' ')) .gsub '{{versions}}', versions end |
.insert_source_lines! ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/cani/completions.rb', line 115 def self.insert_source_lines! return unless Cani.config.modify_shell_configs %w[bash zsh].each do |shell| shellrc = File.join Dir.home, ".#{shell}rc" next unless File.exist? shellrc lines = File.read(shellrc).split "\n" comp_path = completion_path shell comp_src = compile_source_line comp_path lines << comp_src \ unless lines.find_index { |l| l.match comp_src } File.write shellrc, lines.join("\n") end end |
.install! ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cani/completions.rb', line 53 def self.install! # create all parent folders FileUtils.mkdir_p Cani.config.fish_comp_dir FileUtils.mkdir_p Cani.config.comp_dir %w[bash zsh fish].each do |shell| File.open completion_path(shell), 'w' do |file| file << send("generate_#{shell}") end end # append source lines to configurations insert_source_lines! end |
.paths ⇒ Object
79 80 81 82 83 |
# File 'lib/cani/completions.rb', line 79 def self.paths puts "fish: #{completion_path(:fish)}" puts "bash: #{completion_path(:bash)}" puts "zsh: #{completion_path(:zsh)}" end |
.remove! ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/cani/completions.rb', line 68 def self.remove! %w[bash zsh fish].each do |shell| shell_comp = completion_path shell File.unlink shell_comp if File.exist? shell_comp end # remove source lines to configurations delete_source_lines! end |