Module: Cani::Fzf

Defined in:
lib/cani/fzf.rb

Class Method Summary collapse

Class Method Details

.browser_feature_rows(brwsr, version) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cani/fzf.rb', line 52

def self.browser_feature_rows(brwsr, version)
  features_by_support = brwsr.features_for version

  Api::Feature::TYPES.flat_map do |(status, type)|
    if (features = features_by_support.fetch(type[:name], nil))
      features.map do |feature|
        color = {'un' => :yellow, 'ot' => :magenta}.fetch feature[:status], :green
        [{content: "[#{feature[:status]}]", color: color},
         "[#{type[:symbol]}]", feature[:title]]
      end
    end
  end.compact
end

.browser_rowsObject



42
43
44
45
46
# File 'lib/cani/fzf.rb', line 42

def self.browser_rows
  @browser_rows ||= Cani.api.browsers.map do |bwsr|
    [bwsr.title, 'usage: ' + format('%.4f%%', bwsr.usage.values.reduce(0) { |total, add| total + add })]
  end
end

.browser_usage_rows(brwsr) ⇒ Object



48
49
50
# File 'lib/cani/fzf.rb', line 48

def self.browser_usage_rows(brwsr)
  brwsr.usage.map { |(v, u)| [v, 'usage: ' + format('%.4f%%', u)] }.reverse
end

.executable?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
# File 'lib/cani/fzf.rb', line 24

def self.executable?
  @exe ||= begin
    `command -v fzf`
    $?.success?
  end
end

.feature_rowsObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/cani/fzf.rb', line 31

def self.feature_rows
  @feature_rows ||= Cani.api.features.map do |ft|
    pc = format('%.2f%%', ft.percent).rjust 6
    cl = {'un' => :yellow, 'ot' => :magenta}.fetch ft.status, :green
    tt = format('%-24s', ft.title.size > 24 ? ft.title[0..23].strip + '..'
                                            : ft.title)

    [{content: "[#{ft.status}]", color: cl}, pc, tt, *ft.current_support]
  end
end

.pick(rows, **opts) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cani/fzf.rb', line 3

def self.pick(rows, **opts)
  if STDOUT.tty?
    unless executable?
      puts 'Cani fatal: command "fzf" not found, is it installed?'
      exit 1
    end

    rows   = tableize_rows(rows, **opts).join "\n"
    ohdr   = opts.fetch :header, []
    header = ohdr.is_a?(Array) ? [:cani, *ohdr].map { |v| v.to_s.downcase }.join(':')
                               : 'cani:' + ohdr.to_s

    `echo "#{rows}" | fzf --ansi --header="[#{header}]"`.split '   '
  else
    # when output of any initial command is being piped
    # print results and exit this command.
    puts tableize_rows(rows).join "\n"
    exit
  end
end

.tableize_rows(rows, **opts) ⇒ Object



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
# File 'lib/cani/fzf.rb', line 66

def self.tableize_rows(rows, **opts)
  col_widths = []
  colors     = opts.fetch :colors, []

  rows.each do |row|
    row.each.with_index do |column, i|
      column = column[:content] if column.is_a? Hash
      col_width     = column.size
      col_widths[i] = col_width if col_width > col_widths[i].to_i
    end
  end

  rows.map do |row|
    row.map.with_index do |col, i|
      color  = col[:color] if col.is_a? Hash
      col    = col[:content] if col.is_a? Hash
      result = col.to_s.ljust col_widths[i]

      if STDOUT.tty?
        result.colorize(color || colors[i] || colors[-1] || :default)
              .gsub '"', '\"'
      else
        result
      end
    end.join('   ').rstrip
  end
end