-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathciu
executable file
·77 lines (64 loc) · 2.08 KB
/
ciu
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'colorize'
require 'json'
# Ciu, short for CanIUse
# A shell script that downloads JSON data and displays results
# in table form with colors
module Ciu
MAX_AGE = 86_400 # 1 day in seconds
STATUSSES = { 'rec' => 'rc', 'unoff' => 'un', 'other' => 'ot' }.freeze
COL = ' '.freeze
VER_BACK = 2
FETCH_URL = 'https://raw.githubusercontent.com/Fyrd/caniuse/master/data.json'.freeze
STAT_SYMS = { 'n' => '-', 'y' => '+', 'p' => '~' }.freeze
LIST = %w[ie edge safari ios_saf opera
op_mob firefox chrome android bb].freeze
NAMES = { 'firefox' => 'ff', 'chrome' => 'chr', 'safari' => 'saf',
'ios_saf' => 'saf_ios', 'opera' => 'opr', 'op_mob' => 'opr_mob',
'android' => 'andr' }.freeze
def self.features
data
end
def self.row(ft)
[
status(ft['status']).green,
support(ft['usage_perc_y']).light_black,
title(ft['title']),
stats(ft['stats']).light_black
].join(COL).bold
end
def self.title(str, maxw = 30, fill = '...')
return str[0..(maxw - fill.size - 1)] + fill if str.size >= maxw
str + (' ' * (maxw - str.size))
end
def self.status(status)
'[' + (STATUSSES[status] || status) + ']'
end
def self.support(percentage)
(percentage.to_s + '%').ljust(6)
end
def self.stats(stats)
LIST.map do |browser|
stats[browser].values.last(VER_BACK)
.map { |ypn| STAT_SYMS.fetch ypn, '-' }
.join + (NAMES[browser] || browser)
end.join COL
end
def self.data
@data ||= begin
path = File.expand_path '~/.caniuse-fzf-data'
exists = File.exist? path
update = exists && File.mtime(path).to_i < Time.now.to_i - MAX_AGE
exists && !update ? File.read(path) : download!
end
end
def self.download!
path = File.expand_path '~/.caniuse-fzf-data'
json = JSON.parse `curl --silent #{FETCH_URL}`
data = json['data'].values.map(&method(:row)).join "\n"
File.open(path, 'w') { |f| f.write data }
data
end
end
puts Ciu.features