lib/githuh/cli/commands/base.rb in githuh-0.2.0 vs lib/githuh/cli/commands/base.rb in githuh-0.2.1

- old
+ new

@@ -7,10 +7,11 @@ module Githuh module CLI module Commands DEFAULT_PAGE_SIZE = 20 + DEFAULT_TITLE = 'Operation Progress' class Base < Dry::CLI::Command extend Forwardable def_delegators :context, :stdout, :stderr, :stdin, :kernel, :argv @@ -36,11 +37,11 @@ self.context = Githuh self.verbose = verbose self.info = info self.token = api_token || token_from_gitconfig - self.per_page = per_page || DEFAULT_PAGE_SIZE + self.per_page = per_page.to_i || DEFAULT_PAGE_SIZE self.client = Octokit::Client.new(access_token: token) print_userinfo if info end @@ -60,10 +61,30 @@ def ui_width 80 end + def bar(title = DEFAULT_TITLE) + @bar ||= create_progress_bar(title: title) + end + + # Overwrite me + def bar_size + 0 + end + + def create_progress_bar(size = bar_size, title: DEFAULT_TITLE) + return unless info || verbose + + TTY::ProgressBar.new("[:bar]", + title: title, + total: size.to_i, + width: ui_width - 2, + head: '', + complete: '▉'.magenta) + end + private def print_userinfo duration = DateTime.now - DateTime.parse(user_info[:created_at].to_s) years = (duration / 365).to_i @@ -75,26 +96,26 @@ lines << sprintf(" Current User: %s", h(user_info.login)) lines << sprintf(" Public Repos: %s", h(user_info.public_repos.to_s)) lines << sprintf(" Followers: %s", h(user_info.followers.to_s)) lines << sprintf(" Member For: %s", h(sprintf("%d years, %d months, %d days", years, months, days))) - self.box = TTY::Box.frame *lines, - padding: 1, + self.box = TTY::Box.frame(*lines, + padding: 0, width: ui_width, align: :left, - title: { top_center: Githuh::BANNER }, + title: { top_center: "┤ #{Githuh::BANNER} ├" }, style: { fg: :white, border: { fg: :bright_green } - } + }) Githuh.stdout.print box end def h(arg) - arg.to_s.bold.blue + arg.to_s end def token_from_gitconfig @token_from_gitconfig ||= `git config --global --get user.token`.chomp