Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix off by one with default value in pagination #207

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions lib/tty/prompt/paginator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Paginator
#
# @api private
def initialize(**options)
@last_index = Array(options[:default]).flatten.first || 0
@last_index = Array(options[:default]).flatten.first
@per_page = options[:per_page]
@start_index = Array(options[:default]).flatten.first
end
Expand Down Expand Up @@ -75,15 +75,23 @@ def paginate(list, active, per_page = nil, &block)
end
end

step = (current_index - @last_index).abs
if current_index > @last_index # going up
if current_index >= @end_index && current_index < list.size - 1
last_page = list.size - @per_page
@start_index = [@start_index + step, last_page].min
if @last_index.nil?
if current_index == @start_index && current_index > 0
@start_index -= 1
elsif current_index == @end_index && current_index < list.size - 1
@start_index += 1
end
elsif current_index < @last_index # going down
if current_index <= @start_index && current_index > 0
@start_index = [@start_index - step, 0].max
else
step = (current_index - @last_index).abs
if current_index > @last_index # going up
if current_index >= @end_index && current_index < list.size - 1
last_page = list.size - @per_page
@start_index = [@start_index + step, last_page].min
end
elsif current_index < @last_index # going down
if current_index <= @start_index && current_index > 0
@start_index = [@start_index - step, 0].max
end
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/unit/multi_select_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def exit_message(prompt, choices)
expect(answer).to eq(["D"])

expected_output =
output_helper("What letter?", choices[3..5], "D", %w[D], init: true,
output_helper("What letter?", choices[2..4], "D", %w[D], init: true,
hint: "Press #{up_down}/#{left_right} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
exit_message("What letter?", %w[D])

Expand All @@ -396,7 +396,7 @@ def exit_message(prompt, choices)
expect(answer).to eq([4])

expected_output =
output_helper("What letter?", choices.keys[3..5], :D, [:D], init: true,
output_helper("What letter?", choices.keys[2..4], :D, [:D], init: true,
hint: "Press #{up_down}/#{left_right} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
exit_message("What letter?", %w[D])

Expand All @@ -416,7 +416,7 @@ def exit_message(prompt, choices)
expect(answer).to eq(["D"])

expected_output =
output_helper("What letter?", choices[3..5], "D", %w[D], init: true,
output_helper("What letter?", choices[2..4], "D", %w[D], init: true,
hint: "Press #{up_down}/#{left_right} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
exit_message("What letter?", %w[D])

Expand Down Expand Up @@ -472,7 +472,7 @@ def exit_message(prompt, choices)
expect(answer).to eq(%w[4 10])

expected_output =
output_helper("What number?", choices[3..6], "4", ["4"], init: true,
output_helper("What number?", choices[1..4], "4", ["4"], init: true,
hint: "Press #{up_down}/#{left_right} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
output_helper("What number?", choices[4..7], "8", ["4"]) +
output_helper("What number?", choices[8..9], "10", ["4"]) +
Expand Down
9 changes: 9 additions & 0 deletions spec/unit/paginator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@
expect(paginator.end_index).to eq(6)
end

it "always returns a page that includes the current selection" do
list = (1..100).to_a
list.each do |one_based_index|
zero_based_page_indexes = described_class.new.paginate(list, one_based_index, 5).map(&:last)
zero_based_index = one_based_index - 1
expect(zero_based_page_indexes).to include(zero_based_index)
end
end

it "starts with default selection" do
list = %w[a b c d e f g]
paginator = described_class.new(per_page: 3, default: 3)
Expand Down
14 changes: 7 additions & 7 deletions spec/unit/select_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ def exit_message(prompt, choice)
expect(answer).to eq("D")
expected_output = [
"\e[?25lWhat letter? \e[90m(Press #{up_down}/#{left_right} arrow to move and Enter to select)\e[0m\n",
" C\n",
"\e[32m#{symbols[:marker]} D\e[0m\n",
" E\n",
" F",
" E",
"\e[2K\e[1G\e[1A" * 3,
"\e[2K\e[1G",
"What letter? \e[32mD\e[0m\n\e[?25h"
Expand All @@ -416,9 +416,9 @@ def exit_message(prompt, choice)
expect(answer).to eq(4)
expected_output = [
"\e[?25lWhat letter? \e[90m(Press #{up_down}/#{left_right} arrow to move and Enter to select)\e[0m\n",
" C\n",
"\e[32m#{symbols[:marker]} D\e[0m\n",
" E\n",
" F",
" E",
"\e[2K\e[1G\e[1A" * 3,
"\e[2K\e[1G",
"What letter? \e[32mD\e[0m\n\e[?25h"
Expand All @@ -441,9 +441,9 @@ def exit_message(prompt, choice)

expected_output = [
"\e[?25lWhat letter? \e[90m(Press #{up_down}/#{left_right} arrow to move and Enter to select)\e[0m\n",
" C\n",
"\e[32m#{symbols[:marker]} D\e[0m\n",
" E\n",
" F",
" E",
"\e[2K\e[1G\e[1A" * 3,
"\e[2K\e[1G",
"What letter? \e[32mD\e[0m\n\e[?25h"
Expand Down Expand Up @@ -484,7 +484,7 @@ def exit_message(prompt, choice)
expect(answer).to eq("10")

expected_output = [
output_helper("What number?", choices[3..6], "4", init: true,
output_helper("What number?", choices[1..4], "4", init: true,
hint: "Press #{up_down}/#{left_right} arrow to move and Enter to select"),
output_helper("What number?", choices[4..7], "8"),
output_helper("What number?", choices[8..9], "10"),
Expand Down