Module: Twig::Display
- Included in:
- Twig
- Defined in:
- lib/twig/display.rb
Overview
Handles displaying matching branches as a command-line table or as serialized data.
Constant Summary collapse
- COLORS =
{ :black => 30, :red => 31, :green => 32, :yellow => 33, :blue => 34, :purple => 35, :cyan => 36, :white => 37 }
- WEIGHTS =
{ :normal => 0, :bold => 1 }
- DEFAULT_PROPERTY_COLUMN_WIDTH =
16- DEFAULT_BRANCH_COLUMN_WIDTH =
48- CURRENT_BRANCH_INDICATOR =
'* '- EMPTY_BRANCH_PROPERTY_INDICATOR =
'-'
Class Method Summary collapse
Instance Method Summary collapse
- #branch_list_headers(header_options = {}) ⇒ Object
- #branch_list_line(branch) ⇒ Object
- #branches_json ⇒ Object
- #column(string, options = {}) ⇒ Object
- #column_gutter ⇒ Object
- #date_time_column_width ⇒ Object
- #format_string(string, options) ⇒ Object
- #format_strings? ⇒ Boolean
- #property_column_width(property_name = nil) ⇒ Object
Class Method Details
.unformat_string(string) ⇒ Object
25 26 27 28 |
# File 'lib/twig/display.rb', line 25 def self.unformat_string(string) # Returns a copy of the given string without color/weight markers. string.gsub(/\e\[[0-9]+(;[0-9]+)?m/, '') end |
Instance Method Details
#branch_list_headers(header_options = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/twig/display.rb', line 76 def branch_list_headers( = {}) branch_indicator_padding = ' ' * CURRENT_BRANCH_INDICATOR.size .merge!( .inject({}) do |opts, (key, value)| if key == :header_color opts[:color] = value elsif key == :header_weight opts[:weight] = value end opts end ) out = column(' ', :width => date_time_column_width) << column_gutter out << property_names.map do |property| width = property_column_width(property) column(property, .merge(:width => width)) << column_gutter end.join out << column(branch_indicator_padding + 'branch', ) out << "\n" out << column(' ', :width => date_time_column_width) << column_gutter out << property_names.map do |property| width = property_column_width(property) underline = '-' * property.size column(underline, .merge(:width => width)) << column_gutter end.join out << column(branch_indicator_padding + '------', ) out << "\n" out end |
#branch_list_line(branch) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/twig/display.rb', line 110 def branch_list_line(branch) is_current_branch = branch.name == current_branch_name properties = branch.get_properties(property_names) properties = property_names.inject({}) do |result, property_name| property_value = (properties[property_name] || '').strip property_value = EMPTY_BRANCH_PROPERTY_INDICATOR if property_value.empty? property_value.gsub!(/[\n\r]+/, ' ') result.merge(property_name => property_value) end line = column(branch.last_commit_time.to_s, :width => date_time_column_width) line << column_gutter line << property_names.map do |property_name| property_value = properties[property_name] || '' width = property_column_width(property_name) column(property_value, :width => width) << column_gutter end.join branch_column_width = property_column_width(:branch) branch_column = column(branch.to_s, :width => branch_column_width) branch_column.strip! # Strip final column line << if is_current_branch CURRENT_BRANCH_INDICATOR + branch_column else (' ' * CURRENT_BRANCH_INDICATOR.size) + branch_column end line = format_string(line, :weight => :bold) if is_current_branch line end |
#branches_json ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/twig/display.rb', line 146 def branches_json require 'json' data = { 'branches' => branches.map { |branch| branch.to_hash(property_names) } } data.to_json end |
#column(string, options = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/twig/display.rb', line 30 def column(string, = {}) # Returns `string` with an exact fixed width. If `string` is too wide, it # is truncated with an ellipsis and a trailing space to separate columns. # # `options`: # - `:color`: `nil` by default. Accepts a key from `COLORS`. # - `:weight`: `nil` by default. Accepts a key from `WEIGHTS`. # - `:width`: 8 (characters) by default. string ||= ' ' width = [:width] || 8 new_string = string[0, width] omission = '...' if string.size > width new_string[-omission.size, omission.size] = omission else new_string = ' ' * width new_string[0, string.size] = string end new_string = format_string( new_string, .reject { |key, value| ![:color, :weight].include?(key) } ) new_string end |
#column_gutter ⇒ Object
60 |
# File 'lib/twig/display.rb', line 60 def column_gutter; ' '; end |
#date_time_column_width ⇒ Object
59 |
# File 'lib/twig/display.rb', line 59 def date_time_column_width; 35; end |
#format_string(string, options) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/twig/display.rb', line 159 def format_string(string, ) # Options: # - `:color`: `nil` by default. Accepts a key from `COLORS`. # - `:weight`: `nil` by default. Accepts a key from `WEIGHTS`. # Unlike `::unformat_string`, this is an instance method so that it can # handle config options, e.g., globally disabling color. return string unless format_strings? = [] << COLORS[[:color]] if [:color] << WEIGHTS[[:weight]] if [:weight] return string if .empty? open_format = "\e[#{.join(';')}m" close_format = "\e[0m" open_format + string.to_s + close_format end |
#format_strings? ⇒ Boolean
155 156 157 |
# File 'lib/twig/display.rb', line 155 def format_strings? !Twig::System.windows? end |
#property_column_width(property_name = nil) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/twig/display.rb', line 62 def property_column_width(property_name = nil) if property_name && [:property_width] width = [:property_width][property_name.to_sym] end if width width elsif property_name == :branch Twig::Display::DEFAULT_BRANCH_COLUMN_WIDTH else Twig::Display::DEFAULT_PROPERTY_COLUMN_WIDTH end end |