Skip to content

Commit

Permalink
Colorise table columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Bates committed Feb 11, 2018
1 parent 50c872c commit ef26780
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ PATH
hodlmoon (0.1.0)
formatador
httparty
terminal-table
thor

GEM
Expand Down Expand Up @@ -56,6 +57,8 @@ GEM
ruby-progressbar (1.9.0)
safe_yaml (1.0.4)
slop (3.6.0)
terminal-table (1.8.0)
unicode-display_width (~> 1.1, >= 1.1.1)
thor (0.20.0)
unicode-display_width (1.3.0)
vcr (3.0.3)
Expand Down
1 change: 1 addition & 0 deletions hodlmoon.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Gem::Specification.new do |spec|

spec.add_dependency 'formatador'
spec.add_dependency 'httparty'
spec.add_dependency 'terminal-table'
spec.add_dependency 'thor'

spec.add_development_dependency 'bundler', '~> 1.16'
Expand Down
2 changes: 1 addition & 1 deletion lib/hodlmoon/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def price(coin, currency = 'gbp')
desc 'list LIMIT CURRENCY', 'get LIMIT of top coins in CURRENCY(optional)'
def list(limit = 5, currency = 'gbp')
info = Hodlmoon::Client::RetrieveList.call(limit, currency)
Hodlmoon::Table.build(info)
puts Hodlmoon::Table.build(info)
end
end
end
37 changes: 33 additions & 4 deletions lib/hodlmoon/table.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'formatador'
require 'terminal-table'

module Hodlmoon
class Table
Expand All @@ -11,17 +11,46 @@ def initialize(info)
end

def build
Formatador.display_table(filtered_info, headers)
Terminal::Table.new do |t|
t.headings = headers
t.rows = colorised_rows
t.style = { all_separators: true, border_x: '=', border_i: 'O', alignment: :center }
end
end

private

def headers
['name', 'symbol', 'price_gbp', 'market_cap_gbp', 'percent_change_1h', 'percent_change_24h', 'percent_change_7d']
end

def colorised_rows
rows.each do |row|
colorise_yellow(row[2])
row[-1].include?('-') ? colorise_red(row[-1]) : colorise_green(row[-1])
row[-2].include?('-') ? colorise_red(row[-2]) : colorise_green(row[-2])
row[-3].include?('-') ? colorise_red(row[-3]) : colorise_green(row[-3])
end
end

def rows
filtered_info.map(&:values)
end

def filtered_info
@info.map { |coin| coin.slice(*headers) }
end

def headers
['name', 'symbol', 'price_gbp', 'market_cap_gbp', 'percent_change_1h', 'percent_change_24h', 'percent_change_7d']
def colorise_yellow(string)
string.prepend("\e[33m").concat("\e[0m")
end

def colorise_red(string)
string.prepend("\e[31m").concat("\e[0m")
end

def colorise_green(string)
string.prepend("\e[32m+").concat("\e[0m")
end
end
end

0 comments on commit ef26780

Please sign in to comment.