Skip to content

Commit

Permalink
Moved the rendering of code to its own method (clearer and allows to …
Browse files Browse the repository at this point in the history
…force re-rendering)
  • Loading branch information
Mon-Ouie committed Aug 26, 2012
1 parent 4691946 commit 48c824b
Showing 1 changed file with 47 additions and 41 deletions.
88 changes: 47 additions & 41 deletions lib/coolline/coolline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,47 +202,7 @@ def readline(prompt = ">> ")
@history_moved = false
end

width = @input.winsize[1]
prompt_size = strip_ansi_codes(@prompt).size
line = transform(@line)

stripped_line_width = strip_ansi_codes(line).size
line << " " * [width - stripped_line_width - prompt_size, 0].max

# reset the color, and kill the line
print "\r\e[0m\e[0K"

if strip_ansi_codes(@prompt + line).size <= width
print @prompt + line
print "\e[#{prompt_size + @pos + 1}G"
else
print @prompt

left_width = width - prompt_size

start_index = [@pos - left_width + 1, 0].max
end_index = start_index + left_width - 1

i = 0
line.split(AnsiCode).each do |str|
if start_with_ansi_code? str
# always print ansi codes to ensure the color is right
print str
else
if i >= start_index
print str[0..(end_index - i)]
elsif i < start_index && i + str.size >= start_index
print str[(start_index - i), left_width]
end

i += str.size
break if i >= end_index
end
end
if @pos < left_width + 1
print "\e[#{prompt_size + @pos + 1}G"
end
end
render
end

print "\n"
Expand All @@ -255,6 +215,52 @@ def readline(prompt = ">> ")
@line
end

# Displays the current code on the terminal
def render
width = @input.winsize[1]
prompt_size = strip_ansi_codes(@prompt).size
line = transform(@line)

stripped_line_width = strip_ansi_codes(line).size
line += " " * [width - stripped_line_width - prompt_size, 0].max

# reset the color, and kill the line
print "\r\e[0m\e[0K"

if strip_ansi_codes(@prompt + line).size <= width
print @prompt + line
print "\e[#{prompt_size + @pos + 1}G"
else
print @prompt

left_width = width - prompt_size

start_index = [@pos - left_width + 1, 0].max
end_index = start_index + left_width - 1

i = 0
line.split(AnsiCode).each do |str|
if start_with_ansi_code? str
# always print ansi codes to ensure the color is right
print str
else
if i >= start_index
print str[0..(end_index - i)]
elsif i < start_index && i + str.size >= start_index
print str[(start_index - i), left_width]
end

i += str.size
break if i >= end_index
end
end

if @pos < left_width + 1
print "\e[#{prompt_size + @pos + 1}G"
end
end
end

# Reads a line with no prompt
def gets
readline ""
Expand Down

0 comments on commit 48c824b

Please sign in to comment.