class Coinpare::Command
Constants
- DEFAULT_INTERVAL
The default interval for auto updating data
- SYMBOLS
Public Instance Methods
add_color(str, color)
click to toggle source
# File lib/coinpare/command.rb, line 50 def add_color(str, color) @options["no-color"] || color == :none ? str : @pastel.decorate(str, color) end
config()
click to toggle source
Main configuration @api public
# File lib/coinpare/command.rb, line 17 def config @config ||= begin config = TTY::Config.new config.filename = "coinpare" config.extname = ".toml" config.append_path Dir.pwd config.append_path Dir.home config end end
cursor()
click to toggle source
The cursor movement
@see www.rubydoc.info/gems/tty-cursor
@api public
# File lib/coinpare/command.rb, line 111 def cursor require "tty-cursor" TTY::Cursor end
editor()
click to toggle source
Open a file or text in the user's preferred editor
@see www.rubydoc.info/gems/tty-editor
@api public
# File lib/coinpare/command.rb, line 121 def editor require "tty-editor" TTY::Editor end
execute(*)
click to toggle source
Execute this command
@api public
# File lib/coinpare/command.rb, line 99 def execute(*) raise( NotImplementedError, "#{self.class}##{__method__} must be implemented" ) end
number_to_currency(value)
click to toggle source
# File lib/coinpare/command.rb, line 90 def number_to_currency(value) whole, part = value.to_s.split(".") part = "." + part unless part.nil? "#{whole.gsub(/(\d)(?=(\d{3})+(?!\d))/, '\\1,')}#{part}" end
percent(value)
click to toggle source
# File lib/coinpare/command.rb, line 60 def percent(value) (value * 100).round(2) end
percent_change(before, after)
click to toggle source
# File lib/coinpare/command.rb, line 64 def percent_change(before, after) (after - before) / before.to_f * 100 end
pick_arrow(change)
click to toggle source
Provide arrow for marking value growth or decline @api public
# File lib/coinpare/command.rb, line 44 def pick_arrow(change) return if change.zero? change > 0 ? SYMBOLS[:up_arrow] : SYMBOLS[:down_arrow] end
pick_color(change)
click to toggle source
# File lib/coinpare/command.rb, line 54 def pick_color(change) return :none if change.zero? change > 0 ? :green : :red end
precision(value, decimals = 2)
click to toggle source
# File lib/coinpare/command.rb, line 78 def precision(value, decimals = 2) part = value.to_s.split(".")[1] return 0 if part.nil? value.between?(0, 1) ? (part.index(/[^0]/) + decimals) : decimals end
round_to(value, prec = nil)
click to toggle source
# File lib/coinpare/command.rb, line 85 def round_to(value, prec = nil) prec = precision(value) if prec.nil? format("%.#{prec}f", value) end
screen()
click to toggle source
Get terminal screen properties
@see www.rubydoc.info/gems/tty-screen
@api public
# File lib/coinpare/command.rb, line 131 def screen require "tty-screen" TTY::Screen end
shorten_currency(value)
click to toggle source
# File lib/coinpare/command.rb, line 68 def shorten_currency(value) if value > 10**9 "#{(value / 10**9).to_f.round(2)} B" elsif value > 10**6 "#{(value / 10**6).to_f.round(2)} M" else value end end
timestamp()
click to toggle source
Time for when the data was fetched @api public
# File lib/coinpare/command.rb, line 30 def timestamp "#{Time.now.strftime('%d %B %Y')} at #{Time.now.strftime('%I:%M:%S %p %Z')}" end