class TreeTrimmer::Base

Attributes

multi_select_selector[R]
selected_color[R]
stdin[R]
stdout[R]

Public Class Methods

new(multi_select_selector: "x", selected_color: :red, stdin: $stdin, stdout: $stdout) click to toggle source

@param multi_select_selector [String] selector for Downup Menu of when

chosing branches to delete

@param selected_color [Symbol] color of selector when chosing a branch to delete

# File lib/tree_trimmer/base.rb, line 10
def initialize(multi_select_selector: "x",
               selected_color: :red,
               stdin: $stdin,
               stdout: $stdout)

  @stdin                 = stdin
  @stdout                = stdout
  @multi_select_selector = multi_select_selector
  @selected_color        = selected_color
  sanitize_branches!
end

Public Instance Methods

trim_branches() click to toggle source

Uses the git branches of the current folder to create a Menu with Downup

Once branches are choosen, users are prompted to confirm delete the choosen branches.

The appropiate action is taken based on the user's input and the deleted or undeleted branches are returned.

# File lib/tree_trimmer/base.rb, line 30
def trim_branches
  @selection = Downup::Base.new(
    options: branch_options,
    type: :multi_select,
    multi_select_selector: multi_select_selector,
    selected_color: selected_color,
    header_proc: header_proc
  ).prompt

  delete_branches_confirmation
  @selection
end

Private Instance Methods

branch_keys() click to toggle source
# File lib/tree_trimmer/base.rb, line 53
def branch_keys
  ("a".."z").take(branches.count)
end
branch_options() click to toggle source
# File lib/tree_trimmer/base.rb, line 47
def branch_options
  branch_keys.zip(branches).each_with_object({}) do |option, hash|
    hash[option.first] = option.last
  end
end
branches() click to toggle source
# File lib/tree_trimmer/base.rb, line 57
def branches
  IO.popen("git branch").each_line.map(&:chomp).map(&:lstrip)
end
delete_branches!() click to toggle source
# File lib/tree_trimmer/base.rb, line 78
def delete_branches!
  @selection.each do |branch|
    cmd = "git branch -D #{branch}"
    stdout.puts "\n...running " + cmd.red + "\n\n"
    system(cmd)
  end
  quit_or_continue
end
delete_branches_confirmation() click to toggle source
# File lib/tree_trimmer/base.rb, line 61
def delete_branches_confirmation
  stdout.puts "\n\nDelete Branches?\n".red
  stdout.puts @selection
  stdout.print "\n(y/n) > ".light_black
  process_input(stdin.gets.chomp)
end
header_proc() click to toggle source
# File lib/tree_trimmer/base.rb, line 113
def header_proc
  proc {
    stdout.puts "\n------------------"
    stdout.puts "-- Tree Trimmer --"
    stdout.puts "------------------\n\n"
  }
end
process_input(input) click to toggle source
# File lib/tree_trimmer/base.rb, line 68
def process_input(input)
  case input
  when "y" then delete_branches!
  when "n" then quit_or_continue
  else
    stdout.puts "please choose y or n"
    delete_branches_confirmation
  end
end
quit_or_continue() click to toggle source
# File lib/tree_trimmer/base.rb, line 87
def quit_or_continue
  quit_or_continue_prompt
  case stdin.gets.chomp
  when "q", "quit"
    stdout.puts "\n...thanks for using tree trimmer!".light_cyan
  when "c", "continue"
    trim_branches
  else
    stdout.puts "please choose a relevant option"
    quit_or_continue
  end
end
quit_or_continue_prompt() click to toggle source
# File lib/tree_trimmer/base.rb, line 106
def quit_or_continue_prompt
  stdout.puts ("-" * 80).light_black
  stdout.puts "\nq or quit to abort".light_red
  stdout.puts "c or continue to continue\n".light_yellow
  stdout.print "> "
end
sanitize_branches!() click to toggle source
# File lib/tree_trimmer/base.rb, line 100
def sanitize_branches!
  branches.each do |branch|
    branches.delete(branch) if branch.include?("master")
  end
end