Class: RVGP::Dashboard
- Inherits:
-
Object
- Object
- RVGP::Dashboard
- Defined in:
- lib/rvgp/dashboard.rb
Overview
This class implements a basic graphical dashboard, for use on ansi terminals. These dashboards resemble tables, with stylized headers and footers. Here’s a rough example, of what these dashboards look like: “‘ ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ Personal Dashboard ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│ ├────────────────────────────────────────────────────┬─────────────┬─────────────┬─────────────┬─────────────┤ │ Account │ 01-23 │ 02-23 │ 03-23 │ 04-23 │ ├────────────────────────────────────────────────────┼─────────────┼─────────────┼─────────────┼─────────────┤ │ Personal:Expenses:Food:Groceries │ $ 500.00 │ $ 510.00 │ $ 520.00 │ $ 530.00 │ │ Personal:Expenses:Food:Restaurants │ $ 250.00 │ $ 260.00 │ $ 270.00 │ $ 280.00 │ │ Personal:Expenses:Phone:Service │ $ 75.00 │ $ 75.00 │ $ 75.00 │ $ 75.00 │ │ Personal:Expenses:Transportation:Gas │ $ 150.00 │ $ 180.00 │ $ 280.00 │ $ 175.00 │ ├────────────────────────────────────────────────────┼─────────────┼─────────────┼─────────────┼─────────────┤ │ Expenses │ $ 975.00 │ $ 1,025.00 │ $ 1,145.00 │ $ 1,060.00 │ │ Income │ $ -2,500.00 │ $ -2,500.00 │ $ -2,500.00 │ $ -2,500.00 │ ├────────────────────────────────────────────────────┼─────────────┼─────────────┼─────────────┼─────────────┤ │ Cash Flow │ $ -1,525.00 │ $ -1,475.00 │ $ -1,355.00 │ $ -1,440.00 │ └────────────────────────────────────────────────────┴─────────────┴─────────────┴─────────────┴─────────────┘ “`
There’s a lot of functionality here, but, it’s mostly unused at the moment, outside the cashflow command. Ultimately, this is probably going to end up becoming a Grid viewing tool on the cli.
Instance Attribute Summary collapse
-
#csv ⇒ RVGP::Utilities::GridQuery
readonly
The grid and data that this Dashboard will output.
-
#label ⇒ String
readonly
The label for this dashboard.
-
#series_column_label ⇒ String
readonly
The label, describing what our series represents.
Class Method Summary collapse
-
.table_width_given_column_widths(column_widths) ⇒ Integer
This helper is provided with the intention of being used with #column_data_widths.
Instance Method Summary collapse
-
#column_data_widths ⇒ Array<Integer>
Calculates the width requirements of each column, given the data that is present in that column Note that we’re not including the padding in this calculation.
-
#initialize(label, csv, options = {}) ⇒ Dashboard
constructor
Create a Dashboard, which can thereafter be output to the console via #to_s.
-
#to_a ⇒ Array<Array<Object>>
The goal here is to return the full table, without ansi decorators, and without any to_s output options that will mutate state.
-
#to_s(options = {}) ⇒ String
Render this Dashboard to a string.
Constructor Details
#initialize(label, csv, options = {}) ⇒ Dashboard
Create a Dashboard, which can thereafter be output to the console via #to_s
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rvgp/dashboard.rb', line 66 def initialize(label, csv, = {}) @label = label @csv = csv @pastel = [:pastel] || Pastel.new @series_column_label = [:series_column_label] || 'Series' @columns_ordered_by = [:columns_ordered_by] @format_data_cell = [:format_data_cell] @format_series_label = [:format_series_label] @summaries = [:summaries] unless @summaries.all? { |s| %w[label contents].all? { |k| s.key? k.to_sym } } raise StandardError, 'One or more summaries are incomplete' end end |
Instance Attribute Details
#csv ⇒ RVGP::Utilities::GridQuery (readonly)
The grid and data that this Dashboard will output
34 35 36 |
# File 'lib/rvgp/dashboard.rb', line 34 def csv @csv end |
#label ⇒ String (readonly)
The label for this dashboard. This is used in the first row, of the output
34 35 36 |
# File 'lib/rvgp/dashboard.rb', line 34 def label @label end |
#series_column_label ⇒ String (readonly)
The label, describing what our series represents. This is also known as the ‘keystone’.
34 35 36 |
# File 'lib/rvgp/dashboard.rb', line 34 def series_column_label @series_column_label end |
Class Method Details
.table_width_given_column_widths(column_widths) ⇒ Integer
This helper is provided with the intention of being used with #column_data_widths. Given the return value of #column_data_widths, this method will return the width of a rendered dashboard onto the console. That means we account for padding and cell separation character(s) in this calculation. calculate
148 149 150 151 152 153 |
# File 'lib/rvgp/dashboard.rb', line 148 def self.table_width_given_column_widths(column_widths) accumulated_width = 1 # One is the width of the left-most border '|' accumulated_width + column_widths.map do |w| [Dashboard::CELL_PADDING[1], w, Dashboard::CELL_PADDING[3], 1] # This one is the cell's right-most border '|' end.flatten.sum end |
Instance Method Details
#column_data_widths ⇒ Array<Integer>
Calculates the width requirements of each column, given the data that is present in that column Note that we’re not including the padding in this calculation.
86 87 88 89 90 91 92 93 94 |
# File 'lib/rvgp/dashboard.rb', line 86 def column_data_widths # Now compute the width of each cell's contents: to_a.inject([]) do |ret, row| row.map.with_index do |cell, col_i| cell_width = cell.respond_to?(:length) ? cell.length : 0 ret[col_i].nil? || ret[col_i] < cell_width ? cell_width : ret[col_i] end end end |
#to_a ⇒ Array<Array<Object>>
The goal here is to return the full table, without ansi decorators, and without any to_s output options that will mutate state. The returned object’s may or may not be String, depending on whether the :format_series_row was provided to #initialize
100 101 102 103 104 105 106 |
# File 'lib/rvgp/dashboard.rb', line 100 def to_a # This is the table in full, without ansi, ordering, or width modifiers. # More or less, this is a plain text representation, in full, of the data @to_a ||= [[series_column_label] + sorted_headers] + series_rows.dup.map!(&method(:format_series_row)) + summary_rows end |
#to_s(options = {}) ⇒ String
Render this Dashboard to a string. Presumably for printing to the console.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/rvgp/dashboard.rb', line 119 def to_s( = {}) column_widths = .key?(:column_widths) ? [:column_widths] : nil header_row = [series_column_label] + sorted_headers = summary_rows content_rows = series_rows if column_widths ([header_row] + content_rows + ).each { |row| row.pop row.length - column_widths.length } end # Now let's strip the rows we no longer need to show: content_rows.select!(&[:show_row]) if .key? :show_row # Sort the content: content_rows.sort_by!(&[:rows_ordered_by]) if .key? :rows_ordered_by # Then format the series and data cells: content_rows.map!(&method(:format_series_row)) prettify format('%s Dashboard', label.to_s), [header_row] + content_rows + , column_widths end |