mirror of
https://github.com/Llewellynvdm/fzf.git
synced 2024-12-23 03:19:01 +00:00
Add test cases for result scroll
This commit is contained in:
parent
05118cc440
commit
72ec0a3408
42
fzf
42
fzf
@ -677,25 +677,37 @@ class FZF
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def constrain offset, cursor, count, height
|
||||||
|
original = [offset, cursor]
|
||||||
|
diffpos = cursor - offset
|
||||||
|
|
||||||
|
# Constrain cursor
|
||||||
|
cursor = [0, [cursor, count - 1].min].max
|
||||||
|
|
||||||
|
# Ceil
|
||||||
|
if cursor > offset + (height - 1)
|
||||||
|
offset = cursor - (height - 1)
|
||||||
|
# Floor
|
||||||
|
elsif offset > cursor
|
||||||
|
offset = cursor
|
||||||
|
end
|
||||||
|
|
||||||
|
# Adjustment
|
||||||
|
if count - offset < height
|
||||||
|
offset = [0, count - height].max
|
||||||
|
cursor = [0, [offset + diffpos, count - 1].min].max
|
||||||
|
end
|
||||||
|
|
||||||
|
[[offset, cursor] != original, offset, cursor]
|
||||||
|
end
|
||||||
|
|
||||||
def update_list wipe
|
def update_list wipe
|
||||||
render do
|
render do
|
||||||
offset, ycur, items = sync {
|
offset, ycur, items = sync {
|
||||||
cnt = @matches.length
|
changed, @yoff, @ycur =
|
||||||
pos = @ycur - @yoff
|
constrain(@yoff, @ycur, @matches.length, max_items)
|
||||||
@ycur = [0, [@ycur, cnt - 1].min].max
|
wipe ||= changed
|
||||||
|
|
||||||
if @ycur - @yoff >= max_items
|
|
||||||
@yoff = @ycur - max_items + 1
|
|
||||||
wipe = true
|
|
||||||
elsif @yoff >= @ycur
|
|
||||||
@yoff = @ycur
|
|
||||||
wipe = true
|
|
||||||
end
|
|
||||||
if cnt - @yoff < max_items
|
|
||||||
@yoff = [0, cnt - max_items].max
|
|
||||||
wipe = true
|
|
||||||
@ycur = [0, [@yoff + pos, cnt - 1].min].max
|
|
||||||
end
|
|
||||||
[@yoff, @ycur, @matches[@yoff, max_items]]
|
[@yoff, @ycur, @matches[@yoff, max_items]]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -648,5 +648,39 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
['1 3 4 2', [[0, 24], [12, 17]]],
|
['1 3 4 2', [[0, 24], [12, 17]]],
|
||||||
], FZF.sort(FZF::ExtendedFuzzyMatcher.new(nil).match(list, '12 34', '', ''))
|
], FZF.sort(FZF::ExtendedFuzzyMatcher.new(nil).match(list, '12 34', '', ''))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_constrain
|
||||||
|
fzf = FZF.new []
|
||||||
|
|
||||||
|
# [#**** ]
|
||||||
|
assert_equal [false, 0, 0], fzf.constrain(0, 0, 5, 100)
|
||||||
|
|
||||||
|
# *****[**#** ... ] => [**#******* ... ]
|
||||||
|
assert_equal [true, 0, 2], fzf.constrain(5, 7, 10, 100)
|
||||||
|
|
||||||
|
# [**********]**#** => ***[*********#]**
|
||||||
|
assert_equal [true, 3, 12], fzf.constrain(0, 12, 15, 10)
|
||||||
|
|
||||||
|
# *****[**#** ] => ***[**#****]
|
||||||
|
assert_equal [true, 3, 5], fzf.constrain(5, 7, 10, 7)
|
||||||
|
|
||||||
|
# *****[**#** ] => ****[**#***]
|
||||||
|
assert_equal [true, 4, 6], fzf.constrain(5, 7, 10, 6)
|
||||||
|
|
||||||
|
# ***** [#] => ****[#]
|
||||||
|
assert_equal [true, 4, 4], fzf.constrain(10, 10, 5, 1)
|
||||||
|
|
||||||
|
# [ ] #**** => [#]****
|
||||||
|
assert_equal [true, 0, 0], fzf.constrain(-5, 0, 5, 1)
|
||||||
|
|
||||||
|
# [ ] **#** => **[#]**
|
||||||
|
assert_equal [true, 2, 2], fzf.constrain(-5, 2, 5, 1)
|
||||||
|
|
||||||
|
# [***** #] => [****# ]
|
||||||
|
assert_equal [true, 0, 4], fzf.constrain(0, 7, 5, 10)
|
||||||
|
|
||||||
|
# **[***** #] => [******# ]
|
||||||
|
assert_equal [true, 0, 6], fzf.constrain(2, 10, 7, 10)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user