lib/TokiCLI/view.rb in TokiCLI-0.2.1 vs lib/TokiCLI/view.rb in TokiCLI-0.3.0

- old
+ new

@@ -1,132 +1,179 @@ # encoding: utf-8 + module TokiCLI + class View - def total_table(data) - table = init_table - clear - if data['data']['apps'] - if data['data']['date'] - table.title = "Your apps monitored by Toki: #{data['data']['date']}" - else - table.title = "Your apps monitored by Toki" - end - puts apps_3(data, table) + require 'terminal-table' + + def initialize settings = {} + @settings = if settings.empty? + {"table"=>{"width"=>90}} # force default if no initialization else - table.title = "Your app monitored by Toki" - puts total_3(data, table) + settings end - puts "\n" end - def app_table(asked, app_data) - table = init_table - table.style = { :width => 100 } - name = name_of(app_data) - table.title = "Toki time tracking for #{name}" - table.headings = ['Start', 'Duration', 'Sync ID'] - lines = create_lines app_data - total = calc_total lines - format_lines lines + def version + table = init_table("TokiCLI for Toki.app") + table << ['Version', VERSION] + table << :separator + table << ['Infos', 'http://github.com/ericdke/TokiCLI'] + puts "\n#{table}\n" + end - #TODO: change this - display_limit = Time.now - (3600*24*7) # 7 days - lines.select! {|obj| Time.parse(obj[0]) > display_limit} - - sort_lines lines - - puts create_table(lines, table, total) + def apps_total(api_response, title = "Your apps monitored by Toki") + table, apps = make_apps_table(api_response, title) puts "\n" + display, total = populate_apps_table(table, apps) + display << :separator + display << insert_total_line(total) + puts "\e[H\e[2J" + puts display end - def max_30 text - text.length >= 30 ? "#{text[0..27]}..." : text + def apps(api_response, title = "Your apps monitored by Toki") + table, apps = make_apps_table(api_response, title) + puts "\n" + display, _ = populate_apps_table(table, apps) + puts "\e[H\e[2J" + puts display end - def force_name name - name.nil? ? ' ' : name + def log(api_response, title = "Your app monitored by Toki") + display, _ = make_log_table(api_response, title) + puts "\nRendering the view, please wait.\n\n" + lines = display.render + puts "\e[H\e[2J" + puts lines end - def humanized_date epoch - human = seconds_to_time epoch - "#{human[:hours]}h #{human[:minutes]}m #{human[:seconds]}s" + def log_total(api_response, title = "Your app monitored by Toki") + display, total = make_log_table(api_response, title) + display << :separator + display << insert_total_line(total) + puts "\nRendering the view, please wait.\n\n" + lines = display.render + puts "\e[H\e[2J" + puts lines end - def seconds_to_time epoch - hours = epoch / 3600 - minutes = (epoch / 60 - hours * 60) - seconds = (epoch - (minutes * 60 + hours * 3600)) - {hours: hours, minutes: minutes, seconds: seconds} + def log_activity(api_response) + log = JSON.parse(api_response)['data'] + table = init_table() + table.headings = ['Bundle', 'Start', 'Duration', 'Sync ID'] + lines = make_log_lines(log) + display, _ = populate_log_table(lines, table, true) + puts display end - def readable_time time - "#{'%.2d' % time['hours']}h #{'%.2d' % time['minutes']}m #{'%.2d' % time['seconds']}s" - end - - def clear - puts "\e[H\e[2J" - end - private - def create_table lines, table, total = 0 - index = 0 - length = lines.length - 1 - lines.each do |obj| - table << obj - table << :separator unless index == length - index += 1 + def init_table(title = 'TokiCLI') + Terminal::Table.new do |t| + t.style = { :width => @settings['table']['width'] } + t.title = title end - table << :separator - table << [{ :value => "Total: #{humanized_date(total)}", :colspan => 3, :alignment => :center }] - table end - def sort_lines lines - lines.sort_by! {|obj| obj[0]} + def make_log_table(api_response, title) + log = JSON.parse(api_response)['data'] + table = init_table(title) + table.headings = ['Start', 'Duration', 'Sync ID'] + lines = make_log_lines(log) + return populate_log_table(lines, table) end - def format_lines lines - lines.map! {|obj| [obj[0], obj[1], obj[2]]} + def make_apps_table(api_response, title) + apps = JSON.parse(api_response)['data'] + table = init_table(title) + table.headings = ['Bundle ID', 'Name', 'Total'] + return table, apps end - def calc_total lines - total = 0 - lines.each {|v| total += v[3]} - total + def insert_total_line(total, text = 'Total') + [{ :value => "#{text}: #{readable_time(sec_to_time(total))}", :colspan => 3, :alignment => :center }] end - def create_lines app_data - lines = [] - app_data['data']['log'].each {|k,v| lines << [v['start'], readable_time(v['duration']['time']), k, v['duration']['seconds']]} - lines + def make_log_lines(log) + log.map { |k, v| [v['start'], readable_time_log(v['duration']['time']), k, v['duration']['seconds'], v['bundle']] } end - def name_of app_data - if app_data['data']['name'] - app_data['data']['name'] + def populate_log_table(lines, table, with_bundle = false) + day = lines[0][0][0..9] + if with_bundle == false + table << [{ :value => "#{day}", :colspan => 3, :alignment => :center }] else - app_data['data']['bundle'] + table << [{ :value => "#{day}", :colspan => 4, :alignment => :center }] + table << :separator end + total = 0 + lines.each.with_index(1) do |line, index| + new_day = line[0][0..9] + unless day == new_day + table << :separator + if with_bundle == false + table << [{ :value => "#{new_day}", :colspan => 3, :alignment => :center }] + else + table << [{ :value => "#{new_day}", :colspan => 4, :alignment => :center }] + end + table << :separator + end + if with_bundle == false + table << [line[0][10..18], line[1], line[2]] + else + table << [line[4], line[0][10..18], line[1], line[2]] + # table << :separator unless index == lines.size + end + day = new_day + total += line[3] + end + return table, total end - def init_table - Terminal::Table.new do |t| - t.style = { :width => 100 } + def populate_apps_table(table, apps) + total = 0 + apps.each do |app| + total += app['total']['seconds'] + if app['name'] + table << app_row_with_name(app) + else + table << app_row(app) + end end + return table, total end - def apps_3 resp, table - resp['data']['apps'].each do |obj| - table << [max_30(obj['bundle']), max_30(force_name(obj['name'])), readable_time(obj['total']['time'])] - end - table + def app_row_with_name(obj) + max = @settings['table']['width'] / 3 + [width(max + 5, obj['bundle']), width(max - 5, obj['name']), readable_time(obj['total']['time'])] end - def total_3 resp, table - table << [max_30(resp['data']['bundle']), max_30(force_name(resp['data']['name'])), readable_time(resp['data']['total']['time'])] - table + def app_row(obj) + max = @settings['table']['width'] / 2 + [width(max, obj['bundle']), '(unknown)', readable_time(obj['total']['time'])] end + def width(width, text) + boundary = width - 3 + text.length >= width ? "#{text[0..boundary]}..." : text + end + + def readable_time(obj) + "#{obj['hours']}h #{'%.2d' % obj['minutes']}m #{'%.2d' % obj['seconds']}s" + end + + def readable_time_log(obj) + "#{'%.2d' % obj['minutes']}m #{'%.2d' % obj['seconds']}s" + end + + def sec_to_time(secs) + hours = secs / 3600 + minutes = (secs / 60 - hours * 60) + seconds = (secs - (minutes * 60 + hours * 3600)) + {'hours' => hours, 'minutes' => minutes, 'seconds' => seconds} + end + end + end