Module: StackFu::Rendering
- Included in:
- App, Commands::Command
- Defined in:
- lib/stackfu/helpers/rendering.rb
Constant Summary collapse
- LEFT_MARGIN =
2
Instance Method Summary collapse
- #done(message, more_info = nil) ⇒ Object
- #error(message, more_info = nil) ⇒ Object
- #fill_values_from_options(stack, options) ⇒ Object
- #menu_for(name, collection, convert = false) ⇒ Object
- #render_target(params, target, options) ⇒ Object
- #spinner(&code) ⇒ Object
- #table(opts) ⇒ Object
- #warning(question) ⇒ Object
Instance Method Details
#done(message, more_info = nil) ⇒ Object
57 58 59 60 |
# File 'lib/stackfu/helpers/rendering.rb', line 57 def done(, more_info=nil) puts "#{"Success:".foreground(:green).bright} #{message}" puts "\n#{more_info}" if more_info end |
#error(message, more_info = nil) ⇒ Object
62 63 64 65 |
# File 'lib/stackfu/helpers/rendering.rb', line 62 def error(, more_info=nil) puts "#{"Error:".foreground(:red).bright} #{message}" puts "\n#{more_info}" if more_info end |
#fill_values_from_options(stack, options) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/stackfu/helpers/rendering.rb', line 10 def (stack, ) params = {} return params unless stack.controls stack.controls.each do |c| if (opt = [c.name.to_sym]) case c._type when "Textbox", "Password" params[c.name] = opt if opt when "Numericbox" begin params[c.name] = Float(opt) rescue ArgumentError error "Value for #{c.name} should be numeric" end end end end params end |
#menu_for(name, collection, convert = false) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/stackfu/helpers/rendering.rb', line 67 def (name, collection, convert=false) choice = choose do || .header = "Available #{name.pluralize}" .prompt = "\nSelect the #{name}:" .shell = true collection.each { |p| .choice(p.name) } end if convert collection.select { |i| i.name == choice }.first.id else choice end end |
#render_target(params, target, options) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/stackfu/helpers/rendering.rb', line 35 def render_target(params, target, ) max_length = target.controls.map { |c| c.label.size }.max target.controls.each do |c| unless params[c.name] case c._type when "Textbox" params[c.name] = ask(" #{c.label.rjust(max_length)}: ") when "Numericbox" params[c.name] = ask(" #{c.label.rjust(max_length)}: ", Integer) when "Password" params[c.name] = ask(" #{c.label.rjust(max_length)}: ") { |q| q.echo = "*" } end end end params end |
#spinner(&code) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/stackfu/helpers/rendering.rb', line 193 def spinner(&code) chars = %w{ | / - \\ } result = nil t = Thread.new { result = code.call } while t.alive? print chars[0] STDOUT.flush sleep 0.1 print "\b" STDOUT.flush chars.push chars.shift end print " \b" STDOUT.flush t.join result end |
#table(opts) ⇒ Object
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 109 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/stackfu/helpers/rendering.rb', line 83 def table(opts) collection = opts[:collection] display = opts[:display] if (classes = opts[:class]).is_a?(Array) single = {} plural = {} opts[:class].each do |kls| single[kls] = kls.name.demodulize.humanize.downcase plural[kls] = single[kls].pluralize end else single = opts[:class].name.demodulize.humanize.downcase plural = single.pluralize end labels = opts[:labels] main_column = opts[:main_column] ascii = opts[:ansi] == false header = opts[:header] if collection.empty? msg = opts[:empty] msg ||= "No #{plural}." return msg end columns = display.inject([]) do |arr, item| label = labels ? labels[item] : item.to_s.titleize arr << { :name => item.to_s, :label => label } end display.each_with_index do |column, i| max_value_size = collection.map do |item| if block_given? value = yield(item)[i] else if item.respond_to?(column) value = item.send(column) else value = "" end end value.to_s.size end.max size = [max_value_size, column.to_s.size].max columns[i][:size] = size end title = header || if classes.is_a?(Array) counter = Hash.new(0) collection.each do |item| counter[item.class] += 1 end parts = [] collection.map(&:class).uniq.each do |k| v = counter[k] parts << "#{v} #{v > 1 ? plural[k] : single[k]}" end "Listing #{parts.to_phrase}:\n" else "Listing #{collection.size} #{collection.size > 1 ? plural : single}:\n" end table = [] if ascii table << columns.inject("") { |str, col| str << "#{col[:label].ljust(col[:size])} " } table << columns.inject("") { |str, col| str << "#{"-" * (col[:size]+1)} " } else # table << columns.inject("") { |str, col| str << "#{col[:label].titleize.ljust(col[:size]+1).underline.foreground(:yellow)} " } table << columns.inject("") { |str, col| str << "#{col[:label].ljust(col[:size]+1).underline.foreground(:yellow)} " } end collection.each do |item| values = block_given? ? yield(item) : nil idx = 0 table << columns.inject("") do |str, col| if block_given? value = values[idx] idx += 1 else if item.respond_to?(col[:name]) value = item.send(col[:name]) else value = "" end end just_method = value.is_a?(Numeric) ? :rjust : :ljust if ascii str << "#{value.to_s.send(just_method, col[:size])} " else if col[:name].to_sym == main_column str << "#{value.to_s.send(just_method, col[:size]).foreground(:blue)} " else str << "#{value.to_s.send(just_method, col[:size])} " end end end end table.map! { |line| "#{" " * LEFT_MARGIN}#{line}"} [title, table].flatten.join("\n") end |
#warning(question) ⇒ Object
5 6 7 8 |
# File 'lib/stackfu/helpers/rendering.rb', line 5 def warning(question) puts "== WARNING ==".foreground(:red) agree(question) end |