Class: Sibori

Inherits:
Object
  • Object
show all
Defined in:
lib/reline/sibori.rb

Overview

\ |

\      | <--- whipped cream
 \     |
  \    |
   \-~~|
    \  | <--- shibori kutigane (piping nozzle in Japanese)
     \Ml
      (\    __ __
      ( \--(  )  )
       (__(__)__)  <--- compressed whipped cream

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • value

    the value to set the attribute output to.



18
19
20
# File 'lib/reline/sibori.rb', line 18

def output=(value)
  @output = value
end

Instance Method Details

#clear_screenObject



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_screenObject



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

#deprepObject



167
168
169
# File 'lib/reline/sibori.rb', line 167

def deprep
  Reline::IOGate.deprep
end

#erase_after_cursorObject



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

#prepObject



163
164
165
# File 'lib/reline/sibori.rb', line 163

def prep
  Reline::IOGate.prep
end


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

#renderObject



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