Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moved the rendering of code to its own method (clearer and allows to …

…force re-rendering)
  • Loading branch information...
commit 48c824b781c8e2e756b23dd55cdf6ab38437f108 1 parent 4691946
Mon-Ouie authored
Showing with 47 additions and 41 deletions.
  1. +47 −41 lib/coolline/coolline.rb
88 lib/coolline/coolline.rb
View
@@ -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"
@@ -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 ""
Please sign in to comment.
Something went wrong with that request. Please try again.