Class: GitCurate::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/git_curate/runner.rb

Overview

Contains the main logic of the application.

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Runner

Accepts a Hash of options passed from the parsed command line. Currently there is only one option, :list, which is treated as a boolean, and determines whether the branches will simply be listed non-interactively (list: true), or interactively with opportunities for the user to select branches for deletion (list: false).



14
15
16
# File 'lib/git_curate/runner.rb', line 14

def initialize(opts)
  @opts = opts
end

Instance Method Details

#run(args) ⇒ Object

Runs the application, listing branches either interactively or non-interactively. Returns an exit status, suitable for passing to ‘exit()`. `args` should be passed an array of non-option/non-flag arguments received from the command line. If this array is of inappropriate length, EXIT_FAILURE will be returned. (The appropriate length may be 0.)



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/git_curate/runner.rb', line 23

def run(args)
  if args.length != 0
    $stderr.puts "This script does not accept any arguments."
    return EXIT_FAILURE
  end

  branches = Branch.local(@opts[:merged_opt])
  branches.reject!(&:current?) if interactive?

  table = Tabulo::Table.new(branches, border: :reduced_ascii, column_padding: 0, align_header: :left) do |t|
    t.add_column("Branch") { |b| b.displayable_name(pad: !interactive?) }
    t.add_column("Last commit:#{$/}Date", &:last_commit_date)
    t.add_column("#{$/}Hash", &:hash)
    t.add_column("#{$/}Author", &:last_author)
    t.add_column("#{$/}Subject", &:last_subject)
    t.add_column("Merged#{$/}into HEAD?") { |b| b.merged? ? "Merged" : "Not merged" }
    t.add_column("Status vs#{$/}upstream", &:upstream_info)
  end

  prompt = " d/[k]/e/a ? "
  prompt_and_response_width = (interactive? ? (prompt.length + 2) : 0)
  max_table_width = TTY::Screen.width - prompt_and_response_width
  table.pack(max_table_width: max_table_width)

  branches_to_delete = []

  if !interactive?
    puts table if branches.any?
    return EXIT_SUCCESS
  end

  if branches.empty?
    puts "There are no local branches that can be deleted."
    return EXIT_SUCCESS
  end

  puts
  print_help
  puts

  table.each_with_index do |row, index|
    case HighLine.ask("#{row} #{prompt}").downcase
    when "d"
      branches_to_delete << row.source
    when "k", ""
      ;  # do nothing
    when "e"
      puts table.horizontal_rule
      finalize(branches_to_delete)
      return EXIT_SUCCESS
    when "a"
      puts table.horizontal_rule
      puts "#{$/}Aborting. No branches deleted."
      return EXIT_SUCCESS
    else
      puts table.horizontal_rule
      print_help
      puts table.horizontal_rule unless index == 0
      redo
    end
  end
  puts table.horizontal_rule

  finalize(branches_to_delete)
  return EXIT_SUCCESS
end