Sha256: 5a94a264ff2453143014c1b13ce4a09bf4729993533f6615dcf068a8a6863781

Contents?: true

Size: 1.65 KB

Versions: 3

Compression:

Stored size: 1.65 KB

Contents

require 'term/ansicolor'

module GitTopic
  module Commands
    # list command shows summarized topic information
    class List
      include Term::ANSIColor
      def execute
        print_header
        print_contents
      end

      private

      def print_header
        printf "  %-20s %s\n", 'Branch', 'Summary'
        puts '-' * 80
      end

      def print_contents
        branches, current_branch = parse_branch
        branches.each do |branch_name|
          begin
            print_line(current_branch, branch_name)
          rescue EOFError => _ex
            nop
          end
        end
      end

      def parse_branch
        branches = []
        current_branch = nil
        _stdin, stdout, _stderr, _wait_thr = *Open3.popen3('git branch')
        stdout.each do |line|
          matched = line.match(/\s*(\* )?(.*)/)
          next unless matched
          branches << branch_name = matched[2]
          current_branch = branch_name if matched[1]
        end
        [branches, current_branch]
      end

      def print_line(current_branch, branch_name)
        description = get_description_of branch_name
        branch_format = if branch_name == current_branch
                          "* #{green}#{bold}%-20s#{clear}"
                        else
                          "  #{bold}%-20s#{clear}"
                        end
        printf "#{branch_format} %s", branch_name, description
      end

      def get_description_of(branch)
        config_key = "branch.#{branch}.description"
        command = "git config #{config_key}"
        _stdin, stdout, _stderr, _wait_thr = *Open3.popen3(command)
        stdout.readline
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
git_topic-0.2.1 lib/git_topic/commands/list.rb
git_topic-0.2.0 lib/git_topic/commands/list.rb
git_topic-0.1.0 lib/git_topic/commands/list.rb