Class: GitPrepareBranch::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/git-prepare-branch/terminal.rb

Constant Summary collapse

CLEAR =
'clear'
DEFAULT_PROMPT =
''
DEFAULT_WIDTH =
20
HR_CHARACTER =
''
BLANK_CHARACTER =
' '
NUM_BLANK_LINES_BEFORE_PROMPT =
3
AUTOCOMPLETE_STRATEGIES =
{
  default: -> (_, _) {},
  sha: -> (variables, s) {
    `git rev-list #{variables[:onto]}...`
      .split(/\n/)
      .grep(/^#{s}/)
  },
  file: ->(variables, s) {
    `git diff --name-only --relative=#{variables[:prefix]} #{variables[:onto]}...`
      .split(/\n/)
      .grep(/#{s}/)
  }
}

Instance Method Summary collapse

Constructor Details

#initialize(out: $stdout, err: $stderr, prompt: DEFAULT_PROMPT, logger: nil, styles: nil) ⇒ Terminal

Returns a new instance of Terminal.



29
30
31
32
33
34
35
# 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

Instance Method Details

#ask(question, autocomplete_strategy: :default) ⇒ Object



37
38
39
40
# 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) ⇒ Object



42
43
44
# File 'lib/git-prepare-branch/terminal.rb', line 42

def blank_lines(quantity = 1)
  quantity.times { out.puts }
end

#call(command, values = {}) ⇒ Object



46
47
48
49
50
51
52
53
# 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 = {}) ⇒ Object



55
56
57
58
59
60
61
# 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

#clearObject



63
64
65
# File 'lib/git-prepare-branch/terminal.rb', line 63

def clear
  call CLEAR
end

#enable_rawObject



67
68
69
# File 'lib/git-prepare-branch/terminal.rb', line 67

def enable_raw
  system("stty raw -echo")
end

#hrObject



71
72
73
# File 'lib/git-prepare-branch/terminal.rb', line 71

def hr
  out.puts styles.hr(HR_CHARACTER * width)
end

#prompt_to_continueObject



75
76
77
78
79
80
# 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_rawObject



82
83
84
# File 'lib/git-prepare-branch/terminal.rb', line 82

def reset_raw
  system("stty -raw echo")
end

#say(output, style = :default) ⇒ Object



86
87
88
# File 'lib/git-prepare-branch/terminal.rb', line 86

def say(output, style = :default)
  puts styles.apply(output, style)
end

#wait_for_keypressObject



90
91
92
93
94
95
96
97
98
99
100
# 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) ⇒ Object



102
103
104
# File 'lib/git-prepare-branch/terminal.rb', line 102

def write_line(output, style = :default)
  puts styles.apply(make_full_width(output), style)
end