Skip to content

Commit

Permalink
array-like style map
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Feb 6, 2011
1 parent 2e4eeab commit e334f08
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 18 deletions.
31 changes: 22 additions & 9 deletions bin/ruco
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ end

def display(lines, offset, color_mask)
@screen ||= [] # current screen is used as cache
color_mask = color_mask.flatten

lines.each_with_index do |content, line|
colors = color_mask[line] || []
colors = color_mask[line]

# expand line with whitespace
line += offset
Expand All @@ -83,13 +84,25 @@ def display(lines, offset, color_mask)
Curses.attrset(Curses::A_NORMAL)

# write colored string
colors.each do |start, color|
Curses.addstr(content[index...start])
Curses.attrset color
index = start
# colors.each do |start, color|
# Curses.addstr(content[index...start])
# Curses.attrset color
# index = start
# end
# Curses.addstr(content[index...-1])
if colors
colors.each_with_index do |color,i|
if color
color = (color.empty? ? Curses::A_NORMAL : Curses::A_REVERSE)
Curses.attrset color
end
Curses.addstr(content.slice(i,1))
end
else
Curses.addstr(content)
end
Curses.addstr(content[index...-1])

# Curses.addstr(content[index...-1])
write(line, 0, (rand(899)+100).to_s) if @options[:debug_cache]
end
end
Expand All @@ -99,9 +112,9 @@ def show_app(app)
color_mask = app.color_mask

# TODO move this logic into application
display([lines.first], 0, [color_mask.first])
display(lines[1..-2], 1, color_mask[1..-2])
display([lines.last], Curses.stdscr.maxy - 1, [color_mask.last])
# display([lines.first], 0, [color_mask.first])
display(lines[1..-2], 1, color_mask)#[1..-2])
# display([lines.last], Curses.stdscr.maxy - 1, [color_mask.last])

Curses.setpos(app.cursor.line, app.cursor.column)
end
Expand Down
5 changes: 4 additions & 1 deletion lib/ruco/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ def view
end

def color_mask
reverse = [[[0,Curses::A_REVERSE]]]
reverse = StyleMap.new(1)
reverse.add(:reverse, 0, 0..@options[:columns])
# reverse = [[[0,Curses::A_REVERSE]]]
# reverse + editor.color_mask + reverse
reverse + editor.color_mask + reverse
end

Expand Down
29 changes: 27 additions & 2 deletions lib/ruco/style_map.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
module Ruco
class StyleMap
attr_accessor :lines

def initialize(lines)
@lines = Array.new(lines).fill{[]}
@lines = Array.new(lines)
end

def add(style, line, columns)
@lines[line] ||= []
@lines[line] << [style, columns]
end

def flatten
@lines.map do |styles|
next if styles.empty?
next unless styles
flat = []

# add style info to every column
Expand All @@ -25,5 +28,27 @@ def flatten
flat
end
end

def +(other)
lines = self.lines + other.lines
new = StyleMap.new(0)
new.lines = lines
new
end

def slice!(*args)
sliced = lines.slice!(*args)
new = StyleMap.new(0)
new.lines = sliced
new
end

def shift
slice!(0, 1)
end

def pop
slice!(-1, 1)
end
end
end
1 change: 1 addition & 0 deletions lib/ruco/text_area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def view
@window.crop(lines) * "\n" + "\n"
end

<<<<<<< HEAD
def cursor
adjust_window
@window.cursor
Expand Down
13 changes: 7 additions & 6 deletions lib/ruco/window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,22 @@ def scroll_column_into_view(column)
end

def color_mask(selection)
mask = Array.new(lines)
mask = StyleMap.new(lines)
return mask unless selection

mask.map_with_index do |_,line|
lines.times do |line|
visible = visible_area(line)
next unless selection.overlap?(visible)

first = [selection.first, visible.first].max
first = first[1] - left
last = [selection.last, visible.last].min
last = last[1] - left

[
[first[1]-left, Curses::A_REVERSE],
[last[1]-left, Curses::A_NORMAL]
]
mask.add(:reverse, line, first..last)
end

mask
end

def left=(x)
Expand Down
30 changes: 30 additions & 0 deletions spec/ruco/style_map_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,34 @@
]
end
end

describe 'array style operations' do
it "adds two maps" do
s1 = Ruco::StyleMap.new(1)
s1.add(:reverse, 0, 0..1)
s2 = Ruco::StyleMap.new(2)
s2.add(:reverse, 0, 2..3)
(s1 + s2).flatten.should == [
[[:reverse], [:reverse], []],
[nil, nil, [:reverse], [:reverse], []],
nil
]
end

it "can shift" do
s = Ruco::StyleMap.new(2)
s.add(:reverse, 0, 0..1)
s.add(:reverse, 1, 1..2)
s.shift.flatten.should == [[[:reverse],[:reverse],[]]]
s.flatten.should == [[nil, [:reverse],[:reverse],[]]]
end

it "can pop" do
s = Ruco::StyleMap.new(2)
s.add(:reverse, 0, 0..1)
s.add(:reverse, 1, 1..2)
s.pop.flatten.should == [[nil, [:reverse],[:reverse],[]]]
s.flatten.should == [[[:reverse],[:reverse],[]]]
end
end
end

0 comments on commit e334f08

Please sign in to comment.