Commits

seydar  committed 65944ba Draft

trying to get read_char to work for coolline

  • Participants
  • Parent commits a3a6674

Comments (0)

Files changed (1)

File lib/chitin/core_ext/coolline.rb

       old_word_beginning_before(pos)
     end
   end
+
+  # In tests, this reads the first character of buffered input.
+  # In practice, this does not do anything differently than `@input.getch`
+  # C'est la vie.
+  def read_char
+    @input.raw { @input.getc }
+  end
+
+  # This is here because the line:
+  #   until (char = @input.getch) == "\r"
+  # was replaced with:
+  #   until (char = read_char) == "\r"
+  # because I'm trying and failing to fix it so that Coolline
+  # will read buffered characters from STDIN. For example:
+  #   def test; sleep 2; STDIN.getch; end
+  # if you run that and type a bunch of characters, it will only
+  # return the first character you typed AFTER STDIN.getch got a chance
+  # to run. Ideally, with `read_char` it will read the first character that
+  # you typed regardless.
+  def readline(prompt = ">> ")
+    @prompt = prompt
+
+    @history.delete_empty
+
+    @line        = ""
+    @pos         = 0
+    @accumulator = nil
+
+    @history_moved = false
+
+    @should_exit = false
+
+    reset_line
+    print @prompt
+
+    @history.index = @history.size - 1
+    @history << @line
+
+    until (char = read_char) == "\r"
+      @menu.erase
+
+      handle(char)
+      return if @should_exit
+
+      if @history_moved
+        @history_moved = false
+      end
+
+      render
+    end
+
+    @menu.erase
+
+    print "\n"
+
+    @history[-1] = @line if @history.size != 0
+    @history.index = @history.size
+
+    @history.save_line
+
+    @line
+  end
 end