class GitPrepareBranch::Terminal

Constants

AUTOCOMPLETE_STRATEGIES
BLANK_CHARACTER
CLEAR
DEFAULT_PROMPT
DEFAULT_WIDTH
HR_CHARACTER
NUM_BLANK_LINES_BEFORE_PROMPT

Attributes

err[R]
logger[R]
out[R]
prompt[R]
styles[R]

Public Class Methods

new(out: $stdout, err: $stderr, prompt: DEFAULT_PROMPT, logger: nil, styles: nil) click to toggle source
# File lib/git-prepare-branch/terminal.rb, line 29
def initialize(out: $stdout, err: $stderr, prompt: DEFAULT_PROMPT, logger: nil, styles: nil)
  @out = out
  @err = err
  @prompt = prompt
  @logger = logger || Logger.new
  @styles = styles || Styles.new
end

Public Instance Methods

ask(question, autocomplete_strategy: :default) click to toggle source
# File lib/git-prepare-branch/terminal.rb, line 37
def ask(question, autocomplete_strategy: :default)
  set_autocomplete_strategy autocomplete_strategy
  Readline.readline("#{question}#{prompt}", true).chomp.strip
end
blank_lines(quantity = 1) click to toggle source
# File lib/git-prepare-branch/terminal.rb, line 42
def blank_lines(quantity = 1)
  quantity.times { out.puts }
end
call(command, values = {}) click to toggle source
# File lib/git-prepare-branch/terminal.rb, line 46
def call(command, values = {})
  command = normalise_command(command, values)
  logger.log "CMD: #{command}"
  result = system command, out: out, err: err
  logger.log "OUT: #{out}"
  logger.log "ERR: #{err}"
  result
end
capture(command, values = {}) click to toggle source
# File lib/git-prepare-branch/terminal.rb, line 55
def capture(command, values = {})
  command = normalise_command(command, values)
  logger.log "CAP: #{command}"
  result = `#{command}`.chomp.strip
  logger.log "OUT: #{result}"
  result
end
clear() click to toggle source
# File lib/git-prepare-branch/terminal.rb, line 63
def clear
  call CLEAR
end
enable_raw() click to toggle source
# File lib/git-prepare-branch/terminal.rb, line 67
def enable_raw
  system("stty raw -echo")
end
hr() click to toggle source
# File lib/git-prepare-branch/terminal.rb, line 71
def hr
  out.puts styles.hr(HR_CHARACTER * width)
end
prompt_to_continue() click to toggle source
# File lib/git-prepare-branch/terminal.rb, line 75
def prompt_to_continue
  blank_lines NUM_BLANK_LINES_BEFORE_PROMPT
  hr
  say styles.footer('Press any key to continue')
  wait_for_keypress {}
end
reset_raw() click to toggle source
# File lib/git-prepare-branch/terminal.rb, line 82
def reset_raw
  system("stty -raw echo")
end
say(output, style = :default) click to toggle source
# File lib/git-prepare-branch/terminal.rb, line 86
def say(output, style = :default)
  puts styles.apply(output, style)
end
wait_for_keypress() click to toggle source
# File lib/git-prepare-branch/terminal.rb, line 90
def wait_for_keypress
  while true do
    begin
      enable_raw
      char = STDIN.getc
      return char
    ensure
      reset_raw
    end
  end
end
write_line(output, style = :default) click to toggle source
# File lib/git-prepare-branch/terminal.rb, line 102
def write_line(output, style = :default)
  puts styles.apply(make_full_width(output), style)
end

Private Instance Methods

make_full_width(text) click to toggle source
# File lib/git-prepare-branch/terminal.rb, line 128
def make_full_width(text)
  text + (BLANK_CHARACTER * [width - text.length, 0].max)
end
normalise_command(command, values = {}) click to toggle source
# File lib/git-prepare-branch/terminal.rb, line 110
def normalise_command(command, values = {})
  format(command, values)
end
set_autocomplete_strategy(strategy) click to toggle source
# File lib/git-prepare-branch/terminal.rb, line 114
def set_autocomplete_strategy strategy
  Readline.completion_append_character = " "
  strategy = Array(strategy)
  Readline.completion_proc = AUTOCOMPLETE_STRATEGIES[strategy[0]].curry.call(strategy[1])
end
width() click to toggle source
# File lib/git-prepare-branch/terminal.rb, line 120
def width
  begin
    IO.console.winsize[1]
  rescue NotImplementedError
    DEFAULT_WIDTH
  end
end