Module: Cani

Defined in:
lib/cani.rb,
lib/cani/api.rb,
lib/cani/fzf.rb,
lib/cani/config.rb,
lib/cani/version.rb,
lib/cani/api/browser.rb,
lib/cani/api/feature.rb,
lib/cani/completions.rb,
lib/cani/api/feature/viewer.rb

Overview

Cani

Defined Under Namespace

Modules: Completions, Fzf Classes: Api, Config

Constant Summary collapse

VERSION =
'0.5.9'.freeze

Class Method Summary collapse

Class Method Details

.apiObject



19
20
21
# File 'lib/cani.rb', line 19

def self.api
  @api ||= Api.new
end

.completion_pathsObject



109
110
111
# File 'lib/cani.rb', line 109

def self.completion_paths
  Completions.paths
end

.configObject



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

def self.config
  @settings ||= Config.new
end

.editObject



127
128
129
# File 'lib/cani.rb', line 127

def self.edit
  system ENV.fetch('EDITOR', 'vim'), config.file
end

.exec!(command, *args_and_options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cani.rb', line 27

def self.exec!(command, *args_and_options)
  return config if command&.start_with? '-'

  args    = args_and_options.reject { |arg| arg.start_with? '-' }
  command = :help unless command && respond_to?(command)
  command = command.to_s.downcase.to_sym

  case command
  when :edit
    edit
  when :use
    use args[0]
  when :show
    show args[0], args[1]
  when :update, :purge, :help, :version,
       :install_completions, :completion_paths
    send command
  else
    help
  end
end

.helpObject



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cani.rb', line 49

def self.help
  String.disable_colorization true unless STDOUT.tty?
  puts "Cani ".light_yellow + VERSION.to_s + ' <https://github.com/SidOfc/cani>'.light_black
  puts ''
  puts 'This command provides a TUI interface to access caniuse.com data.'.light_black
  puts 'It allows one to search by browser / version using the \'show\' command'.light_black
  puts 'and by feature using the \'use\' command. Pressing <enter> on a feature'.light_black
  puts 'in the \'use\' overview or calling \'use some-feature\' will display a'.light_black
  puts 'table as seen on caniuse.com using curses.'.light_black
  puts ''
  puts 'cani is dependent on fzf (https://github.com/junegunn/fzf)'.light_black
  puts 'for the interactive TUI to work. Without fzf,'.light_black
  puts 'commands can still be piped to get the regular (colorless) output.'.light_black
  puts ''
  puts 'Cani requires at least 20 lines and 40 cols to work properly,'.light_black
  puts 'this is not a hard limit but below this width long lines could wrap'.light_black
  puts 'a lot and significantly reduce visible information.'.light_black
  puts ''
  puts 'Usage:'.red
  puts '   cani'.yellow + ' [COMMAND [ARGUMENTS] [OPTIONS]]'
  puts ''
  puts 'Commands:'.red
  puts '   use '.blue + ' [FEATURE]             ' + 'show browser support for FEATURE'.light_black
  puts '   show'.blue + ' [BROWSER [VERSION]]   ' + 'show information about specific BROWSER and VERSION'.light_black
  puts '   '
  puts '   install_completions        '.blue      + 'installs completions for bash, zsh and fish'.light_black
  puts '   completion_paths           '.blue      + 'prints completion paths that must be sourced in your shell configuration files'.light_black
  puts '   update                     '.blue      + 'force update api data and completions'.light_black
  puts '   edit                       '.blue      + 'edit configuration in $EDITOR'.light_black
  puts '   purge                      '.blue      + 'remove all completion, configuration and data'.light_black
  puts '                              '.blue      + 'stored by this cani'.light_black
  puts '   '
  puts '   help                       '.blue      + 'show this help'.light_black
  puts '   version                    '.blue      + 'print the version number'.light_black
  puts ''
  puts 'Options:'.red
  puts '   --[no-]modify              '.white + 'permanently enable/disable automatic adding/removing'.light_black
  puts '                              '.white + 'of source lines in shell configuration files'.light_black
  puts ''
  puts 'Examples:'.red
  puts '   cani'.yellow + ' use'.blue
  puts '   cani'.yellow + ' use'.blue  + ' \'box-shadow\''
  puts '   cani'.yellow + ' show'.blue + ' ie'
  puts '   cani'.yellow + ' show'.blue + ' ie 11'
  puts '   cani'.yellow + ' show'.blue + ' ie' + ' |'.light_black + ' cat'.yellow
  puts ''
  puts 'Statuses:'.red
  puts '   [ls]'.green   + '   WHATWG Living Standard'.light_black
  puts '   [rc]'.green   + '   W3C Recommendation'.light_black
  puts '   [pr]'.green   + '   W3C Proposed Recommendation'.light_black
  puts '   [cr]'.green   + '   W3C Candidate Recommendation'.light_black
  puts '   [wd]'.green   + '   W3C Working Draft'.light_black
  puts '   [un]'.yellow  + '   Unofficial, Editor\'s draft or W3C "Note"'.light_black
  puts '   [ot]'.magenta + '   Non-W3C, but reputable'.light_black
end

.install_completionsObject



113
114
115
# File 'lib/cani.rb', line 113

def self.install_completions
  Completions.install! || exit(1)
end

.purgeObject



117
118
119
120
121
# File 'lib/cani.rb', line 117

def self.purge
  Completions.remove!
  api.remove!
  config.remove!
end

.show(brws = nil, version = nil) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/cani.rb', line 152

def self.show(brws = nil, version = nil)
  browser           = api.find_browser brws
  @show_min_depth ||= 0 + (browser ? 1 : 0) + (version ? 1 : 0)

  if browser
    if version
      chosen = Fzf.pick Fzf.browser_feature_rows(browser, version),
                        header: "show:#{browser.title.downcase}:#{version}]   [#{Api::Feature.support_legend}",
                        colors: [:green, :light_black, :light_white]

      if chosen.any? && (feature = api.find_feature(chosen[2]))
        Api::Feature::Viewer.new(feature).render
        show browser.title, version
      else
        show browser.title
      end
    else
      exit if config.nav_type?('forward') && @show_min_depth > 1

      if (version = Fzf.pick(Fzf.browser_usage_rows(browser),
                             header: [:show, browser.title],
                             colors: %i[white light_black]).first)
        show browser.title, version
      else
        show
      end
    end
  else
    exit if config.nav_type?('forward') && @show_min_depth > 0

    browser = api.find_browser Fzf.pick(Fzf.browser_rows,
                                        header: [:show],
                                        colors: %i[white light_black]).first

    show browser.title unless browser.nil?
  end
end

.updateObject



123
124
125
# File 'lib/cani.rb', line 123

def self.update
  api.update! && install_completions unless api.updated?
end

.use(feature = nil) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/cani.rb', line 131

def self.use(feature = nil)
  @use_min_depth ||= feature ? 1 : 0
  can_go_back      = !(config.nav_type?('forward') && @use_min_depth > 0)
  matches          = api.find_features feature

  return use if can_go_back && matches.empty?
  return Api::Feature::Viewer.new(matches.first).render if matches.count == 1

  chosen = Fzf.pick Fzf.feature_rows, query: feature,
                        header: 'use]   [' + Api::Feature.support_legend,
                        colors: %i[green light_black light_white light_black]

  # chosen[2] is the title column of a row returned by Fzf.feature_rows
  if chosen && chosen.any? && (feature = api.find_feature(chosen[2]))
    Api::Feature::Viewer.new(feature).render
    use
  else
    exit 0
  end
end

.versionObject



105
106
107
# File 'lib/cani.rb', line 105

def self.version
  puts VERSION
end