Skip to content

Commit

Permalink
Merge pull request #16 from equintana/terminal-table
Browse files Browse the repository at this point in the history
💎 🎨 Using terminal-table to generate show table
  • Loading branch information
erlinis committed Dec 26, 2016
2 parents 8b5584b + 9474e0e commit 73649cb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
1 change: 1 addition & 0 deletions lib/lita-service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
File.join('..', '..', 'locales', '*.yml'), __FILE__
)]

require 'terminal-table'
require 'lita/handlers/service'
require 'lita/handlers/customer'
require 'lita/interactors/base_interactor'
Expand Down
1 change: 1 addition & 0 deletions lita-service.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'simplecov', '>=0.9.2'
spec.add_development_dependency 'coveralls'
spec.add_development_dependency 'rubocop', '~> 0.44.1'
spec.add_development_dependency 'terminal-table', '~> 1.5'
end
16 changes: 8 additions & 8 deletions spec/lita/handlers/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@
it 'shows the service' do
service_data = "\n*TheService*\n\n" \
"```\n" \
"-------------------------------------------------------------\n" \
" # | Name | Quantity | Value | Total \n" \
"-----+----------------------+----------+----------+----------\n" \
" 1 | erlinis | 1 | 2000 | 2000 \n" \
" 2 | khal | 2 | 2000 | 4000 \n" \
"-----+----------------------+----------+----------+----------\n" \
" | Total | 3 | *** | 6000 \n" \
"-------------------------------------------------------------\n" \
"+---+---------+----------+-------+-------+\n" \
"| # | Name | Quantity | Value | Total |\n" \
"+---+---------+----------+-------+-------+\n" \
"| 1 | erlinis | 1 | 2000 | 2000 |\n" \
"| 2 | khal | 2 | 2000 | 4000 |\n" \
"+---+---------+----------+-------+-------+\n" \
"| | Total | 3 | *** | 6000 |\n" \
"+---+---------+----------+-------+-------+\n" \
'```'

send_command('service show TheService')
Expand Down
32 changes: 16 additions & 16 deletions templates/service_show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ _<%= I18n.t("lita.handlers.service.show.empty") %>_
<% else %>
<% total = 0
total_quantity = 0
headings = [ '#', 'Name', 'Quantity', 'Value', 'Total' ]

table = Terminal::Table.new :headings => headings do |t|
@service[:customers].each_with_index do |(name, customer), i|
number = i + 1
quantity = customer[:quantity]
value = customer[:value]
subtotal = quantity * value
total = total + subtotal
total_quantity = total_quantity + quantity
t.add_row [ number, name.to_s, quantity, value, subtotal ]
end
t << :separator
t.add_row [ '', 'Total', total_quantity, '***', total ]
end
%>
<%= '```' %>
-------------------------------------------------------------
# | Name | Quantity | Value | Total
-----+----------------------+----------+----------+----------
<% @service[:customers].each_with_index do |(name, customer), i| %>
<%
number = i + 1
quantity = customer[:quantity]
value = customer[:value]
subtotal = quantity * value
total = total + subtotal
total_quantity = total_quantity + quantity
%><%= " %3s | %-20s | %-8i | %-8i | %-8i " % [number, name.to_sym, quantity, value, subtotal] %>
<% end %>
-----+----------------------+----------+----------+----------
<%= " %3s | %-20s | %-8i | %-8s | %-8i " % ['', 'Total', total_quantity, '***', total] %>
<%= table %>
-------------------------------------------------------------
<%= '```' %>
<% end %>

0 comments on commit 73649cb

Please sign in to comment.