Module: Cani::Completions
- Defined in:
- lib/cani/completions.rb
Class Method Summary collapse
- .delete_source_lines! ⇒ Object
- .generate_bash ⇒ Object
- .generate_fish ⇒ Object
- .generate_zsh ⇒ Object
- .insert_source_lines! ⇒ Object
- .install! ⇒ Object
- .remove! ⇒ Object
Class Method Details
.delete_source_lines! ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/cani/completions.rb', line 88 def self.delete_source_lines! %w[bash zsh].each do |shell| shellrc = File.join Dir.home, ".#{shell}rc" lines = File.read(shellrc).split "\n" comp_path = File.join Cani.config.comp_dir, "_cani.#{shell}" rm_idx = lines.find_index { |l| l.match comp_path } 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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/cani/completions.rb', line 100 def self.insert_source_lines! %w[bash zsh].each do |shell| shellrc = File.join Dir.home, ".#{shell}rc" lines = File.read(shellrc).split "\n" comp_path = File.join Cani.config.comp_dir, "_cani.#{shell}" slidx = lines.find_index { |l| l.match comp_path } if slidx lines[slidx] = "#{lines[slidx][/^\s+/]}[ -f #{comp_path} ] && source #{comp_path}" else lines << "[ -f #{comp_path} ] && source #{comp_path}" end File.write shellrc, lines.join("\n") end end |
.install! ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# 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 # write each completion file File.open File.join(Cani.config.fish_comp_dir, 'cani.fish'), 'w' do |file| file << generate_fish end %w[bash zsh].each do |shell| File.open File.join(Cani.config.comp_dir, "_cani.#{shell}"), 'w' do |file| file << send("generate_#{shell}") end end # append source lines to configurations insert_source_lines! end |
.remove! ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/cani/completions.rb', line 73 def self.remove! fish_comp = File.join Cani.config.fish_comp_dir, 'cani.fish' File.unlink fish_comp if File.exist? fish_comp %w[bash zsh].each do |shell| shell_comp = File.join Cani.config.comp_dir, "_cani.#{shell}" File.unlink shell_comp if File.exist? shell_comp end # remove source lines to configurations delete_source_lines! end |