Module: Cani::Fzf

Defined in:
lib/cani/fzf.rb

Class Method Summary collapse

Class Method Details

.browser_feature_rows(brwsr, version) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cani/fzf.rb', line 70

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]}]", { content: feature[:title], color: :default }]
      end
    end
  end.compact
end

.browser_rowsObject



59
60
61
62
63
64
# File 'lib/cani/fzf.rb', line 59

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

.browser_usage_rows(brwsr) ⇒ Object



66
67
68
# File 'lib/cani/fzf.rb', line 66

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

.dimensionsObject



33
34
35
# File 'lib/cani/fzf.rb', line 33

def self.dimensions
  @dimensions ||= [TTY::Screen.columns, TTY::Screen.rows]
end

.executable?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/cani/fzf.rb', line 29

def self.executable?
  @exe ||= system 'fzf --version > /dev/null 2>&1'
end

.feature_rowsObject



41
42
43
# File 'lib/cani/fzf.rb', line 41

def self.feature_rows
  @feature_rows ||= Cani.api.features.map(&Fzf.method(:to_feature_row))
end

.longest_title_sizeObject



37
38
39
# File 'lib/cani/fzf.rb', line 37

def self.longest_title_size
  @longest_title_size ||= Cani.api.features.map(&:title).map(&:size).max
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
23
24
25
26
27
# 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, []
    query  = "--query=\"#{opts[:query]}\"" if opts[:query]
    header = ohdr.is_a?(Array) ? [:cani, *ohdr].map { |v| v.to_s.downcase }.join(':')
                               : 'cani:' + ohdr.to_s

    IO.popen("fzf --ansi --no-preview --header=\"[#{header}]\" #{query}", 'r+') do |io|
      io.write rows
      io.close_write
      io.read
    end.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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cani/fzf.rb', line 84

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

.to_feature_row(ft) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cani/fzf.rb', line 45

def self.to_feature_row(ft)
  pc = format('%.2f%%', ft.percent).rjust 6
  cl = { 'un' => :yellow, 'ot' => :magenta }.fetch ft.status, :green

  total_len = ft.current_support.map(&:size).reduce(&:+) + pc.size + 6 + (ft.current_support.size + 2) * 3
  rem_len   = [longest_title_size, 50, [dimensions.first - total_len, 24].max].min

  tt = format("%-#{rem_len}s", ft.title.size > rem_len ? ft.title[0...rem_len].strip + '..'
                                          : ft.title)

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