|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 |
| -require 'term/ansicolor' |
| 3 | +require 'git_topic/formatter/branches' |
| 4 | +require 'git_topic/formatter/topics' |
4 | 5 |
|
5 | 6 | module GitTopic
|
6 | 7 | module Commands
|
7 | 8 | # list command shows summarized topic information
|
8 | 9 | class List
|
9 |
| - include Term::ANSIColor |
10 |
| - def execute |
11 |
| - branches, current_branch = parse_branches |
12 |
| - print_header(branches.first) |
13 |
| - print_contents(branches, current_branch) |
14 |
| - end |
15 |
| - |
16 |
| - private |
17 |
| - |
18 |
| - Branch = Struct.new('Branch', :name, :rev) |
19 |
| - |
20 |
| - def print_header(branch) |
21 |
| - rev_length = branch.rev.length |
22 |
| - header_format = " %-20s %-#{rev_length}s %s" |
23 |
| - puts format(header_format, :Branch, :Rev, :Summary) |
24 |
| - puts '-' * 80 |
| 10 | + def initialize(options = {}) |
| 11 | + @options = options |
| 12 | + @all = options[:all] |
25 | 13 | end
|
26 | 14 |
|
27 |
| - def print_contents(branches, current_branch) |
28 |
| - branches.each do |branch| |
29 |
| - print_line(current_branch, branch) |
30 |
| - end |
31 |
| - end |
32 |
| - |
33 |
| - def parse_branches |
34 |
| - branches = [] |
35 |
| - current_branch = nil |
36 |
| - _stdin, stdout, _stderr, _wait_thr = *Open3.popen3('git branch -v') |
37 |
| - stdout.each do |line| |
38 |
| - branch_name, rev, current_candidate = parse_branch(line) |
39 |
| - current_branch ||= current_candidate |
40 |
| - branches << Branch.new(branch_name, rev) |
41 |
| - end |
42 |
| - [branches, current_branch] |
43 |
| - end |
44 |
| - |
45 |
| - BRANCH_FORMAT = / |
46 |
| - \s*(?<current_exp>\*\ )? |
47 |
| - (?<branch_name>\S+)\s+ |
48 |
| - (?<rev>\S+)\s+(.*) |
49 |
| - /x |
50 |
| - |
51 |
| - def parse_branch(line) |
52 |
| - matched = line.match(BRANCH_FORMAT) |
53 |
| - raise 'cannot parse branch' unless matched |
54 |
| - branch_name = matched[:branch_name] |
55 |
| - rev = matched[:rev] |
56 |
| - current_branch = matched[:current_exp] ? branch_name : nil |
57 |
| - [branch_name, rev, current_branch] |
58 |
| - end |
59 |
| - |
60 |
| - def print_line(current_branch, branch) |
61 |
| - branch_name = branch.name |
62 |
| - rev = branch.rev |
63 |
| - description = get_description_of branch |
64 |
| - return if description.nil? |
65 |
| - branch_format = branch_format(branch_name, current_branch) |
66 |
| - truncated_name = truncate(branch_name) |
67 |
| - puts format("#{branch_format} %s %s", truncated_name, rev, description) |
68 |
| - end |
69 |
| - |
70 |
| - def get_description_of(branch) |
71 |
| - config_key = "branch.#{branch.name}.description" |
72 |
| - command = "git config #{config_key}" |
73 |
| - _stdin, stdout, _stderr, _wait_thr = *Open3.popen3(command) |
74 |
| - return nil if stdout.eof? |
75 |
| - stdout.readline |
76 |
| - end |
77 |
| - |
78 |
| - def branch_format(branch_name, current_branch) |
79 |
| - if branch_name == current_branch |
80 |
| - "* #{green}#{bold}%-20s#{clear}" |
81 |
| - else |
82 |
| - " #{bold}%-20s#{clear}" |
83 |
| - end |
84 |
| - end |
85 |
| - |
86 |
| - def truncate(str, truncate_at: 20) |
87 |
| - omission = '...' |
88 |
| - length_with_room_for_omission = truncate_at - omission.length |
89 |
| - if str.length > truncate_at |
90 |
| - "#{str[0, length_with_room_for_omission]}#{omission}" |
91 |
| - else |
92 |
| - str |
93 |
| - end |
| 15 | + def execute |
| 16 | + branches = ::GitTopic::Formatter::Branches.new @options |
| 17 | + branches.print |
| 18 | + return unless @all |
| 19 | + puts '' |
| 20 | + topics = ::GitTopic::Formatter::Topics.new |
| 21 | + topics.print |
94 | 22 | end
|
95 | 23 | end
|
96 | 24 | end
|
|
0 commit comments