Skip to content

Commit

Permalink
corrected bug where escape codes didn't end properly
Browse files Browse the repository at this point in the history
bug slipped through because that part of the spec had been commented out
while focusing on other development
  • Loading branch information
Kay Rhodes committed Oct 1, 2018
1 parent bd3227e commit 74a4458
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
1 change: 1 addition & 0 deletions .changelog_entries/6123d01cfe6b33654e12794527ec5118.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"Fixed","tickets":[],"description":"Corrected bug where the ends of escape sequences were not being correctly detected","tags":["bug"]}
41 changes: 25 additions & 16 deletions spec/oho/converter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ describe Oho::Converter do
# TODO: Write tests
default_options = {:background_color => "white",
:foreground_color => "black"}
# it "creates inline styles" do
# c = Oho::Converter.new(default_options)
# # STDERR.puts("\\033[31mhi\\033[0m")
# test_string = "\033[31mhi\033[0m"
# response, escape_code = c.process(test_string, nil)
# # c.process(test_string).should(eq("<span class=\"red\">hi</span>"))
# response.should(eq("<span style=\"color: red; \">hi</span>"))
#
# end
#
# it "handles escape codes that terminate on subsequent lines" do
# c = Oho::Converter.new(default_options)
# test_string = "\033[36mfoo\nbar\033[0m baz"
# response, escape_code = c.process(test_string, nil)
# response.should(eq("<span style=\"color: aqua; \">foo\n<br />bar</span><span style=\"\"> baz</span>"))
# end
it "creates inline styles" do
c = Oho::Converter.new(default_options)
# STDERR.puts("\\033[31mhi\\033[0m")
test_string = "\033[31mhi\033[0m"
response, escape_code = c.process(test_string, nil)
# c.process(test_string).should(eq("<span class=\"red\">hi</span>"))
response.should(eq("<span style=\"color: red; \">hi</span>"))

end

it "handles escape codes that terminate on subsequent lines" do
c = Oho::Converter.new(default_options)
test_string = "\033[36mfoo\nbar\033[0m baz"
response, escape_code = c.process(test_string, nil)
response.should(eq("<span style=\"color: aqua; \">foo\n<br />bar</span><span style=\"\"> baz</span>"))
end
describe "#extract_next_escape_code" do
# there are too damn many options to do a unit test for each one
# looping over grouped arrays of them to make sure all are tested
Expand All @@ -34,6 +34,15 @@ describe Oho::Converter do
code.class.should(eq(Oho::ColorEscapeCode))
end
end
it "stops at end of escape code" do
# test_string = "\033[31mhi\033[0m"
reader = Char::Reader.new("[31mhi\033[0m")
code, reader = c.extract_next_escape_code('[', reader)
reader.has_next?().should(eq(true))
reader.next_char.should(eq('h'))
code.as(Oho::EscapeCode).to_span(nil).should(eq("<span style=\"color: red; \">"))

end
it "returs ColorEscapeCode for color codes" do
seqs = [
"[m", # same as 0n
Expand Down
7 changes: 4 additions & 3 deletions src/oho/converter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,12 @@ module Oho

if first_char == '['
zero_val_enders = ['H', 'f', 's', 'u', 'K']
val_enders = ['A', 'B', 'C', 'D', 's', 'h', 'l', 'p']
if seq_length = 2
val_enders = ['A', 'B', 'C', 'D', 's', 'h', 'l', 'p', 'm']
if seq_length == 2
return true if zero_val_enders.includes? current_char
elsif seq_length == 3
return true if current_char == 'j' # assuming prior was '2'
return true if ['j', 'm'].includes? current_char
# if j assuming prior was '2'
else # must be > 3
return true if val_enders.includes? current_char
end
Expand Down

0 comments on commit 74a4458

Please sign in to comment.