Permalink
Browse files

Allowed coolline to read input from non-tty source

When #tty? returns false, the input is read line by line with gets. It
is still passed to the transform block before being printed out.
  • Loading branch information...
Mon-Ouie committed Jan 18, 2014
1 parent cb4a3b3 commit 0ab67bff641468b7f8f0222f000583b26f233c41
Showing with 33 additions and 1 deletion.
  1. +19 −1 lib/coolline/coolline.rb
  2. +13 −0 test/coolline_test.rb
  3. +1 −0 test/helpers.rb
View
@@ -193,6 +193,23 @@ def self.readline(*args)
# Reads a line from the terminal
# @param [String] prompt Characters to print before each line
def readline(prompt = ">> ", default_line = "")
if @input.tty?
readline_full(prompt, default_line)
else
readline_dumb(prompt)
end
end
def readline_dumb(prompt)
print prompt
line = @input.gets
self.line = line ? line.chomp : ""
print transform(line), "\n"
line.chomp if line
end
def readline_full(prompt = ">> ", default_line = "")
@prompt = prompt
@history.delete_empty
@@ -221,7 +238,6 @@ def readline(prompt = ">> ", default_line = "")
render
end
end
@menu.erase
@@ -238,6 +254,8 @@ def readline(prompt = ">> ", default_line = "")
# Displays the current code on the terminal
def render
return unless @input.tty?
width = @input.winsize[1]
prompt_size = ansi_length(@prompt)
line = transform(@line)
View
@@ -15,3 +15,16 @@
asserts(:strip_ansi_codes, "\e[38;5;232mHate\e[0m").equals "Hate"
end
end
context "a user uses non-tty input" do
setup do
Coolline.new { |c|
c.input = StringIO.new("p 42\np 82\n")
c.output = StringIO.new
}
end
denies(:readline).raises_kind_of Exception
asserts(:readline).equals "p 82"
asserts(:readline).nil
end
View
@@ -2,6 +2,7 @@
require 'riot'
require 'coolline'
require 'stringio'
Riot.reporter = Riot::PrettyDotMatrixReporter

0 comments on commit 0ab67bf

Please sign in to comment.