Class: Sibori
- Inherits:
-
Object
- Object
- Sibori
- Defined in:
- lib/reline/sibori.rb
Overview
\ |
\ | <--- whipped cream
\ |
\ |
\-~~|
\ | <--- shibori kutigane (piping nozzle in Japanese)
\Ml
(\ __ __
( \--( ) )
(__(__)__) <--- compressed whipped cream
Instance Attribute Summary collapse
-
#output ⇒ Object
writeonly
Sets the attribute output.
Instance Method Summary collapse
- #clear_screen ⇒ Object
- #clone_screen ⇒ Object
- #deprep ⇒ Object
- #erase_after_cursor ⇒ Object
-
#initialize(width, height, cursor_pos) ⇒ Sibori
constructor
A new instance of Sibori.
- #move_cursor_column(col) ⇒ Object
- #move_cursor_down(val) ⇒ Object
- #move_cursor_up(val) ⇒ Object
- #prep ⇒ Object
- #print(str) ⇒ Object
- #render ⇒ Object
- #scroll_down(val) ⇒ Object
Constructor Details
#initialize(width, height, cursor_pos) ⇒ Sibori
Returns a new instance of Sibori.
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/reline/sibori.rb', line 20 def initialize(width, height, cursor_pos) @width = width @height = height @cursor_pos = cursor_pos @screen = [String.new] @line_index = 0 @byte_pointer_in_line = 0 @cleared = false clone_screen end |
Instance Attribute Details
#output=(value) ⇒ Object (writeonly)
Sets the attribute output
18 19 20 |
# File 'lib/reline/sibori.rb', line 18 def output=(value) @output = value end |
Instance Method Details
#clear_screen ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/reline/sibori.rb', line 107 def clear_screen #$stderr.puts "clear_screen" @screen = [String.new] @line_index = 0 @byte_pointer_in_line = 0 @cursor_pos.x = @cursor_pos.y = 0 @cleared = true Reline::IOGate.clear_screen end |
#clone_screen ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/reline/sibori.rb', line 31 def clone_screen @prev_screen = @screen.map { |line| line.dup } @prev_cursor_pos = @cursor_pos.dup @prev_line_index = @line_index end |
#deprep ⇒ Object
167 168 169 |
# File 'lib/reline/sibori.rb', line 167 def deprep Reline::IOGate.deprep end |
#erase_after_cursor ⇒ Object
102 103 104 105 |
# File 'lib/reline/sibori.rb', line 102 def erase_after_cursor #$stderr.puts "erase_after_cursor" @screen[@line_index] = @screen[@line_index].byteslice(0, @byte_pointer_in_line) end |
#move_cursor_column(col) ⇒ Object
56 57 58 59 60 |
# File 'lib/reline/sibori.rb', line 56 def move_cursor_column(col) #$stderr.puts "move_cursor_column(#{col})" @byte_pointer_in_line = width_to_bytesize(@screen[@line_index], col) @cursor_pos.x = col end |
#move_cursor_down(val) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/reline/sibori.rb', line 71 def move_cursor_down(val) #$stderr.puts "move_cursor_down(#{val})" if @line_index < @height - 1 #$stderr.puts "@line_index #{@line_index} @screen.size #{@screen.size} @height #{@height}" #$stderr.puts @screen.inspect @line_index += val @screen[@line_index] = String.new if @line_index == @screen.size @byte_pointer_in_line = width_to_bytesize(@screen[@line_index], @cursor_pos.x) @cursor_pos.y += val end end |
#move_cursor_up(val) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/reline/sibori.rb', line 62 def move_cursor_up(val) #$stderr.puts "move_cursor_up(#{val})" if @line_index.positive? @line_index -= val @byte_pointer_in_line = width_to_bytesize(@screen[@line_index], @cursor_pos.x) @cursor_pos.y -= val end end |
#print(str) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/reline/sibori.rb', line 39 def print(str) #$stderr.puts "print #{str.inspect}" line = @screen[@line_index] before = line.byteslice(0, @byte_pointer_in_line) str_width = Reline::Unicode.calculate_width(str, true) after_cursor = line.byteslice(@byte_pointer_in_line..-1) after_cursor_width = Reline::Unicode.calculate_width(after_cursor, true) rest = '' if after_cursor_width > str_width rest_byte_pointer = @byte_pointer_in_line + width_to_bytesize(after_cursor, str_width) rest = line.byteslice(rest_byte_pointer..-1) end @screen[@line_index] = before + str + rest @byte_pointer_in_line += str.bytesize @cursor_pos.x += Reline::Unicode.calculate_width(str, true) end |
#render ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/reline/sibori.rb', line 122 def render #$stderr.puts ?* * 100 Reline::IOGate.move_cursor_up(@prev_line_index) if @prev_line_index.positive? #$stderr.puts "! move_cursor_up(#{@prev_line_index})" if @prev_line_index.positive? #$stderr.puts "@prev_line_index #{@prev_line_index} @line_index #{@line_index}" if @screen.size > @prev_screen.size #$stderr.puts ?a * 100 down = @screen.size - @prev_screen.size #$stderr.puts "#{@prev_cursor_pos.y} #{down} #{@height}" if @prev_cursor_pos.y + down > (@height - 1) #$stderr.puts ?b * 100 scroll = (@prev_cursor_pos.y + down) - (@height - 1) Reline::IOGate.scroll_down(scroll) #$stderr.puts "! scroll_down(#{scroll})" #$stderr.puts "down #{down}" Reline::IOGate.move_cursor_up(@screen.size - 1 - scroll) #$stderr.puts "! move_cursor_up(#{@screen.size - 1})" else #$stderr.puts ?c * 100 end end @screen.size.times do |n| Reline::IOGate.move_cursor_column(0) #$stderr.puts "! move_cursor_column(0)" @output.write @screen[n] #$stderr.puts "! print #{@screen[n].inspect}" Reline::IOGate.erase_after_cursor #$stderr.puts "! erase_after_cursor" Reline::IOGate.move_cursor_down(1) if n != (@screen.size - 1) #$stderr.puts "! move_cursor_down(1)" if n != (@screen.size - 1) end up = @screen.size - 1 - @line_index Reline::IOGate.move_cursor_up(up) if up.positive? #$stderr.puts "! move_cursor_up(#{up})" if up.positive? column = Reline::Unicode.calculate_width(@screen[@line_index].byteslice(0, @byte_pointer_in_line), true) Reline::IOGate.move_cursor_column(column) #$stderr.puts "! move_cursor_column(#{column}) #{@byte_pointer_in_line}" clone_screen #$stderr.puts ?- * 10 end |
#scroll_down(val) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/reline/sibori.rb', line 83 def scroll_down(val) #$stderr.puts "scroll_down(#{val})" if val >= @height clear_screen @line_index = @screen.size - 1 return end @screen.size.times do |n| if n < @screen.size - val #$stderr.puts "A @screen[#{val} + #{n}] (#{@screen[val + n].inspect}) to @screen[#{n}]" @screen[n] = @screen[val + n] else #$stderr.puts "B String.new to @screen[#{n}]" @screen[n] = String.new end end @line_index += val end |