Class: Layout
- Inherits:
-
Object
- Object
- Layout
- Defined in:
- lib/moneymanager/layout.rb
Class Method Summary collapse
- .clear ⇒ Object
- .formatted_amount(amount) ⇒ Object
- .print_generic_rows(rows) ⇒ Object
- .print_multiple(entries) ⇒ Object
- .print_single(entry) ⇒ Object
- .print_single_value(total) ⇒ Object
Class Method Details
.clear ⇒ Object
61 62 63 |
# File 'lib/moneymanager/layout.rb', line 61 def self.clear print "\e[H\e[2J" end |
.formatted_amount(amount) ⇒ Object
32 33 34 35 |
# File 'lib/moneymanager/layout.rb', line 32 def self.formatted_amount(amount) s = format('%.2f €', amount) amount < 0 ? s.red : s.green end |
.print_generic_rows(rows) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/moneymanager/layout.rb', line 45 def self.print_generic_rows(rows) return unless rows.count > 0 sum = rows.reduce(0) { |tot, row| tot + row.last } table = Terminal::Table.new rows.each do |row| table.add_row [ { value: row[0], alignment: :left }, { value: formatted_amount(row.last), alignment: :right } ] end table.add_separator table.add_row [{ value: formatted_amount(sum), colspan: 2, alignment: :right }] puts table end |
.print_multiple(entries) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/moneymanager/layout.rb', line 2 def self.print_multiple(entries) if entries.count == 1 print_single(entries.first) else clear rows = entries.map { |entry| [entry.formatted_approved, entry.formatted_is_bank_tranfer, entry.date.strftime('%d/%m/%y'), entry.tag, entry.reason[0...50], entry.formatted_amount, entry.digest[0...6]] } table = Terminal::Table.new headings: ['✔/✖︎', '♻︎', 'Date', 'Tag', 'Reason', 'Amount', 'SHA1'], rows: rows table.align_column(5, :right) table.align_column(0, :center) puts table end end |
.print_single(entry) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/moneymanager/layout.rb', line 17 def self.print_single(entry) clear hash = { date: entry.date, reason: entry.reason, amount: entry.formatted_amount, company: entry.company, approved: entry.formatted_approved, bank_transfer: entry.formatted_is_bank_tranfer, tag: entry.tag } rows = hash.map { |k, v| [k.to_s.capitalize, v] } puts Terminal::Table.new rows: rows end |
.print_single_value(total) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/moneymanager/layout.rb', line 37 def self.print_single_value(total) table = Terminal::Table.new do |t| t << [formatted_amount(total)] end table.align_column(0, :right) puts table end |