From 72ec0a34082c3f7b1c87653e2a7b8878bb33ce1d Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Thu, 26 Jun 2014 19:40:29 +0900 Subject: [PATCH] Add test cases for result scroll --- fzf | 42 +++++++++++++++++++++++++++--------------- test/test_fzf.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 15 deletions(-) diff --git a/fzf b/fzf index 781df68..2c654a6 100644 --- a/fzf +++ b/fzf @@ -677,25 +677,37 @@ class FZF 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 render do offset, ycur, items = sync { - cnt = @matches.length - pos = @ycur - @yoff - @ycur = [0, [@ycur, cnt - 1].min].max + changed, @yoff, @ycur = + constrain(@yoff, @ycur, @matches.length, max_items) + 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]] } diff --git a/test/test_fzf.rb b/test/test_fzf.rb index e562f34..a67ebd5 100644 --- a/test/test_fzf.rb +++ b/test/test_fzf.rb @@ -648,5 +648,39 @@ class TestFZF < MiniTest::Unit::TestCase ['1 3 4 2', [[0, 24], [12, 17]]], ], FZF.sort(FZF::ExtendedFuzzyMatcher.new(nil).match(list, '12 34', '', '')) 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