lib/textbringer/window.rb in textbringer-0.1.8 vs lib/textbringer/window.rb in textbringer-0.1.9

- old
+ new

@@ -182,10 +182,11 @@ @@started = false end end def self.redisplay + return if Controller.current.executing_keyboard_macro? return if Window.current.has_input? @@windows.each do |window| window.redisplay unless window.current? end current.redisplay @@ -613,10 +614,29 @@ end window.resize(window.lines + diff, window.columns) end end + def shrink(n) + enlarge(-n) + end + + def shrink_if_larger_than_buffer + @buffer.save_point do + @buffer.end_of_buffer + @buffer.skip_re_backward(/\s/) + count = beginning_of_line_and_count(Window.lines) + 1 + while !@buffer.beginning_of_buffer? + @buffer.backward_char + count += beginning_of_line_and_count(Window.lines) + 1 + end + if lines - 1 > count + shrink(lines - 1 - count) + end + end + end + private def initialize_window(num_lines, num_columns, y, x) @window = Curses::Window.new(num_lines - 1, num_columns, y, x) @mode_line = Curses::Window.new(1, num_columns, y + num_lines - 1, x) @@ -800,10 +820,11 @@ @active end def clear @buffer.clear + @top_of_window.location = @buffer.point_min @message = nil @prompt = "" end def clear_message @@ -822,23 +843,29 @@ if @message @window.addstr(escape(@message)) else prompt = escape(@prompt) @window.addstr(prompt) + framer + @buffer.point_to_mark(@top_of_window) y = x = 0 - columns = @columns - Buffer.display_width(prompt) - beginning_of_line_and_count(1, columns) while !@buffer.end_of_buffer? + cury, curx = @window.cury, @window.curx if @buffer.point_at_mark?(saved) - y, x = @window.cury, @window.curx + y, x = cury, curx end c = @buffer.char_after if c == "\n" break end - @window.addstr(escape(c)) - break if @window.curx == @columns + s = escape(c) + newx = curx + Buffer.display_width(s) + if newx > @columns + break + end + @window.addstr(s) + break if newx >= @columns @buffer.forward_char end if @buffer.point_at_mark?(saved) y, x = @window.cury, @window.curx end @@ -866,8 +893,35 @@ private def initialize_window(num_lines, num_columns, y, x) @window = Curses::Window.new(num_lines, num_columns, y, x) + end + + def escape(s) + super(s).gsub(/\t/, "^I") + end + + def framer + @buffer.save_point do |saved| + max_width = @columns - @window.curx + width = 0 + loop do + c = @buffer.char_after + if c.nil? + width += 1 + else + width += Buffer.display_width(escape(c)) + end + if width > max_width + @buffer.forward_char + break + elsif width == max_width || @buffer.beginning_of_line? + break + end + @buffer.backward_char + end + @top_of_window.location = @buffer.point + end end end end