Class: TTY::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/drakkon/run/reader_shim.rb

Overview

A class responsible for reading character input from STDIN

Used internally to provide key and line reading functionality

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#breakerObject



15
16
17
# File 'lib/drakkon/run/reader_shim.rb', line 15

def breaker
  @breaker
end

#lineObject



15
16
17
# File 'lib/drakkon/run/reader_shim.rb', line 15

def line
  @line
end

Instance Method Details

#read_line(prompt = '', value: '', echo: true, raw: true, nonblock: false) ⇒ Object



17
18
19
20
21
22
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/drakkon/run/reader_shim.rb', line 17

def read_line(prompt = '', value: '', echo: true, raw: true, nonblock: false)
  #  Shim:: Store Line
  self.breaker = false
  self.line = Line.new(value, prompt: prompt)
  screen_width = TTY::Screen.width
  buffer = ''

  output.print(line)

  while (codes = get_codes(echo: echo, raw: raw, nonblock: nonblock)) &&
        (code = codes[0])
    char = codes.pack('U*')

    #  Shim / Back Tab Down Submodule
    if [:back_tab, :ctrl_c].include? console.keys[char] 
      clear_display(line, screen_width)
      trigger_key_event(char, line: line.to_s)
      break
    end

    #  Shim
    if EXIT_KEYS.include?(console.keys[char])
      trigger_key_event(char, line: line.to_s)
      raise TTY::Reader::InputInterrupt
    end

    clear_display(line, screen_width) if raw && echo

    if console.keys[char] == :backspace || code == BACKSPACE
      unless line.start?
        line.left
        line.delete
      end
    elsif console.keys[char] == :delete || code == DELETE
      line.delete
    elsif console.keys[char].to_s =~ /ctrl_/
      # skip
    elsif console.keys[char] == :up
      line.replace(history_previous) if history_previous?
    elsif console.keys[char] == :down
      line.replace(history_next? ? history_next : buffer) if track_history?
    elsif console.keys[char] == :left
      line.left
    elsif console.keys[char] == :right
      line.right
    elsif console.keys[char] == :home
      line.move_to_start
    elsif console.keys[char] == :end
      line.move_to_end
    else
      if raw && [CARRIAGE_RETURN, NEWLINE].include?(code)
        char = "\n"
        line.move_to_end
      end
      line.insert(char)
      buffer = line.text
    end

    if (console.keys[char] == :backspace || code == BACKSPACE) && echo
      if raw
        output.print("\e[1X") unless line.start?
      else
        output.print("\s" + (line.start? ? '' : "\b"))
      end
    end

    # trigger before line is printed to allow for line changes
    trigger_key_event(char, line: line.to_s)

    if raw && echo
      output.print(line.to_s)
      if char == "\n"
        line.move_to_start
      elsif !line.end? # readjust cursor position
        output.print(cursor.backward(line.text_size - line.cursor))
      end
    end

    #  Shim do things
    next unless [CARRIAGE_RETURN, NEWLINE].include?(code) || breaker

    buffer = ''
    output.puts unless echo
    break
  end

  add_to_history(line.text.rstrip) if track_history? && echo

  line.text
			# ===================================
		rescue 
			puts 'rescue'
end