Class: Coinpare::Command
- Inherits:
-
Object
- Object
- Coinpare::Command
- Defined in:
- lib/coinpare/command.rb
Direct Known Subclasses
Coinpare::Commands::Coins, Coinpare::Commands::Holdings, Coinpare::Commands::Markets
Constant Summary collapse
- SYMBOLS =
{ down_arrow: "▼", up_arrow: "▲" }.freeze
- DEFAULT_INTERVAL =
The default interval for auto updating data
5
Instance Method Summary collapse
- #add_color(str, color) ⇒ Object
-
#banner(settings) ⇒ Object
The exchange, currency & time banner.
-
#config ⇒ Object
Main configuration.
-
#cursor ⇒ Object
The cursor movement.
-
#editor ⇒ Object
Open a file or text in the user’s preferred editor.
-
#execute ⇒ Object
Execute this command.
- #number_to_currency(value) ⇒ Object
- #percent(value) ⇒ Object
- #percent_change(before, after) ⇒ Object
-
#pick_arrow(change) ⇒ Object
Provide arrow for marking value growth or decline.
- #pick_color(change) ⇒ Object
- #precision(value, decimals = 2) ⇒ Object
- #round_to(value, prec = nil) ⇒ Object
-
#screen ⇒ Object
Get terminal screen properties.
- #shorten_currency(value) ⇒ Object
-
#timestamp ⇒ Object
Time for when the data was fetched.
Instance Method Details
#add_color(str, color) ⇒ Object
50 51 52 |
# File 'lib/coinpare/command.rb', line 50 def add_color(str, color) @options["no-color"] || color == :none ? str : @pastel.decorate(str, color) end |
#banner(settings) ⇒ Object
The exchange, currency & time banner
36 37 38 39 40 |
# File 'lib/coinpare/command.rb', line 36 def (settings) "\n#{add_color('Exchange', :yellow)} #{settings['exchange']} " \ "#{add_color('Currency', :yellow)} #{settings['base'].upcase} " \ "#{add_color('Time', :yellow)} #{}\n\n" end |
#config ⇒ Object
Main configuration
17 18 19 20 21 22 23 24 25 26 |
# 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 ⇒ Object
The cursor movement
111 112 113 114 |
# File 'lib/coinpare/command.rb', line 111 def cursor require "tty-cursor" TTY::Cursor end |
#editor ⇒ Object
Open a file or text in the user’s preferred editor
121 122 123 124 |
# File 'lib/coinpare/command.rb', line 121 def editor require "tty-editor" TTY::Editor end |
#execute ⇒ Object
Execute this command
99 100 101 102 103 104 |
# File 'lib/coinpare/command.rb', line 99 def execute(*) raise( NotImplementedError, "#{self.class}##{__method__} must be implemented" ) end |
#number_to_currency(value) ⇒ Object
90 91 92 93 94 |
# 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) ⇒ Object
60 61 62 |
# File 'lib/coinpare/command.rb', line 60 def percent(value) (value * 100).round(2) end |
#percent_change(before, after) ⇒ Object
64 65 66 |
# File 'lib/coinpare/command.rb', line 64 def percent_change(before, after) (after - before) / before.to_f * 100 end |
#pick_arrow(change) ⇒ Object
Provide arrow for marking value growth or decline
44 45 46 47 48 |
# 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) ⇒ Object
54 55 56 57 58 |
# 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) ⇒ Object
78 79 80 81 82 83 |
# 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) ⇒ Object
85 86 87 88 |
# 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 ⇒ Object
Get terminal screen properties
131 132 133 134 |
# File 'lib/coinpare/command.rb', line 131 def screen require "tty-screen" TTY::Screen end |
#shorten_currency(value) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# 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 ⇒ Object
Time for when the data was fetched
30 31 32 |
# File 'lib/coinpare/command.rb', line 30 def "#{Time.now.strftime('%d %B %Y')} at #{Time.now.strftime('%I:%M:%S %p %Z')}" end |