Class: RCommand::StandardIOMethod

Inherits:
IOMethod
  • Object
show all
Defined in:
lib/rcommand/io_methods.rb

Instance Method Summary collapse

Constructor Details

#initialize(command_line) ⇒ StandardIOMethod

Returns a new instance of StandardIOMethod.



72
73
74
75
76
# File 'lib/rcommand/io_methods.rb', line 72

def initialize(command_line)
  super(command_line)
  @stty_set = false
  @tab_count = 0
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rcommand/io_methods.rb', line 199

def eof?
  begin
    require 'timeout'
    Timeout.timeout(0.01) do
      return $stdin.eof?
    end
  rescue LoadError
    return $stdin.eof?
  rescue Timeout::Error
    return true
  end
end

#readcharObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rcommand/io_methods.rb', line 166

def readchar
  char = nil
  begin
    if @command_line.io_read.object_id == $stdin.object_id
      if RUBY_PLATFORM =~ /mswin/
        require "Win32API"
        char = Win32API.new("crtdll", "_getch", [], "L").Call
      else
        begin
          if @stty_set != true
            system("stty cbreak -echo")
            @stty_set = true
          end
          char = @command_line.io_read.readchar()
        ensure
          if @stty_set != true
            system("stty -cbreak -echo")
            @stty_set = true
          end
        end
      end
    else
      char = @command_line.io_read.readchar()
    end
  rescue Interrupt
    return 3
  rescue Exception
    return nil
  end
  return char
end

#readlineObject



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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rcommand/io_methods.rb', line 78

def readline
  if @command_line.prompt != nil && @command_line.io_write != nil
    @command_line.io_write.print(@command_line.prompt)
  end
  
  # RESTORE THIS CODE!
  command = ""
  char = nil
  loop do
    char = self.readchar()
    next if char.nil?
    next if char == 0
    if char != 9
      @tab_count = 0
    end
    if char == 3
      raise Interrupt
    elsif char == 10
      @command_line.io_write.print("\n")
      self << command
      break
    elsif char == 9
      @tab_count += 1
      replacement = nil
      if @tab_count == 1
        unless @command_line.procs[:single_tab].nil?
          replacement = @command_line.procs[:single_tab].call(command)
        end
      elsif @tab_count > 1
        unless @command_line.procs[:double_tab].nil?
          replacement = @command_line.procs[:double_tab].call(command)
        end
      end
      if replacement != nil && replacement.size > command.size
        @command_line.io_write.print(
          8.chr * command.size +
          " " * command.size +
          8.chr * command.size)
        @command_line.io_write.print(replacement)
        command = replacement
      end
    elsif char == 8 || char == 127
      if command.size > 0
        command = command[0..-2]
        @command_line.io_write.print(8.chr + " " + 8.chr)
      end
    else
      command << char.chr
      replacement = nil
      if command.index("\e[A") != nil
        command.gsub!("\e[A", "")
        unless @command_line.procs[:key_up].nil?
          replacement = @command_line.procs[:key_up].call(command)
        end
      elsif command.index("\e[B") != nil
        command.gsub!("\e[B", "")
        unless @command_line.procs[:key_down].nil?
          replacement = @command_line.procs[:key_down].call(command)
        end
      elsif command.index("\e[C") != nil
        command.gsub!("\e[C", "")
        unless @command_line.procs[:key_right].nil?
          replacement = @command_line.procs[:key_right].call(command)
        end
      elsif command.index("\e[D") != nil
        command.gsub!("\e[D", "")
        unless @command_line.procs[:key_left].nil?
          replacement = @command_line.procs[:key_left].call(command)
        end
      else
        if (char == "["[0] && command[-2] != "\e"[0]) ||
            (char != "\e"[0] && char != "["[0])
          @command_line.io_write.print(char.chr)
        end
      end
      if replacement != nil
        @command_line.io_write.print(
          8.chr * command.size +
          " " * command.size +
          8.chr * command.size)
        @command_line.io_write.print(replacement)
        command = replacement
      end
    end
  end
  return command + "\n"
end