Skip to content

Commit 68f02fc

Browse files
committed
Fix drawing of results list
1 parent c657294 commit 68f02fc

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

kittens/choose_files/results.go

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,7 @@ func icon_for(path string, x os.DirEntry) string {
115115
return ans
116116
}
117117

118-
func (h *Handler) draw_column_of_matches(matches []*ResultItem, current_idx int, x, available_width, num_before, num_after int) {
119-
if num_before > 0 {
120-
h.lp.QueueWriteString("\r")
121-
h.lp.MoveCursorHorizontally(x)
122-
h.lp.QueueWriteString("… ")
123-
text := h.lp.SprintStyled("italic", fmt.Sprintf("%d prev matches", num_before))
124-
h.render_match_with_positions(text, false, nil, false)
125-
}
118+
func (h *Handler) draw_column_of_matches(matches []*ResultItem, current_idx int, x, available_width int, is_last bool, num_before, num_after int) {
126119
for i, m := range matches {
127120
h.lp.QueueWriteString("\r")
128121
h.lp.MoveCursorHorizontally(x)
@@ -142,12 +135,19 @@ func (h *Handler) draw_column_of_matches(matches []*ResultItem, current_idx int,
142135
h.render_match_with_positions(text, add_ellipsis, m.positions, is_current)
143136
h.lp.MoveCursorVertically(1)
144137
}
145-
if num_after > 0 {
138+
if is_last && num_after+num_before > 0 {
146139
h.lp.QueueWriteString("\r")
147140
h.lp.MoveCursorHorizontally(x)
148141
h.lp.QueueWriteString("… ")
149-
text := h.lp.SprintStyled("italic", fmt.Sprintf("%d more matches", num_after))
142+
text := ""
143+
if num_before > 0 {
144+
text = fmt.Sprintf("%d before, %d after", num_before, num_after)
145+
} else {
146+
text = fmt.Sprintf("%d more matches", num_after)
147+
}
148+
text = h.lp.SprintStyled("italic", text)
150149
h.render_match_with_positions(text, false, nil, false)
150+
h.lp.MoveCursorVertically(1)
151151
}
152152
}
153153

@@ -166,40 +166,35 @@ func (h *Handler) draw_list_of_results(matches []*ResultItem, y, height int) {
166166
}
167167
col_width = available_width / num_cols
168168
}
169-
num_that_can_be_displayed := num_cols * height
170-
num_after, num_before := 0, 0
169+
num_of_slots := num_cols * height
171170
idx := min(h.state.CurrentIndex(), len(matches)-1)
172-
if idx == 0 {
173-
num_after = max(0, len(matches)-num_that_can_be_displayed)
174-
} else {
175-
num_after = max(0, len(matches)-num_that_can_be_displayed)
176-
last_idx := len(matches) - 1 - num_after
177-
if last_idx < idx {
178-
num_before = last_idx - idx
179-
num_after = max(0, num_after-num_before)
180-
}
181-
}
182-
pos := num_before
183-
x := 1
171+
has_more := len(matches) > num_of_slots
172+
if has_more {
173+
num_of_slots-- // need one slot for X more matches msg
174+
}
175+
pos := max(0, idx+1-num_of_slots)
176+
num_before := pos
177+
num_after := max(0, len(matches)-num_of_slots-num_before)
178+
x, limit, total := 1, 0, 0
184179
for i := range num_cols {
185-
is_last, is_first := i == num_cols-1, i == 0
180+
is_last := i == num_cols-1
186181
num := height
187-
if is_first && num_before > 0 {
188-
num--
189-
}
190-
if is_last && num_after > 0 {
182+
if is_last && has_more {
191183
num--
192184
}
193185
h.lp.MoveCursorTo(x, y)
194-
limit := min(len(matches), pos+num)
195-
h.draw_column_of_matches(matches[pos:limit], idx-pos, x, col_width-1, num_before, utils.IfElse(is_last, len(matches)-limit, 0))
186+
limit = min(len(matches), pos+num)
187+
total += limit - pos
188+
h.draw_column_of_matches(matches[pos:limit], idx-pos, x, col_width-1, is_last, num_before, num_after)
196189
x += col_width
197190
pos += num
198-
num_before = 0
199191
if pos >= len(matches) {
200192
break
201193
}
202194
}
195+
if total+num_before+num_after != len(matches) {
196+
panic(fmt.Sprintf("Did not account for all matches, internal error. total_drawn=%d num_before=%d num_after=%d total=%d", total, num_before, num_after, len(matches)))
197+
}
203198
}
204199

205200
func (h *Handler) draw_results(y, bottom_margin int, matches []*ResultItem, in_progress bool) (height int) {
@@ -223,7 +218,10 @@ func (h *Handler) draw_results(y, bottom_margin int, matches []*ResultItem, in_p
223218
func (h *Handler) next_result(amt int) {
224219
if h.state.num_of_matches_at_last_render > 0 {
225220
idx := h.state.CurrentIndex()
226-
idx += amt + h.state.num_of_matches_at_last_render
221+
idx += amt
222+
for idx < 0 {
223+
idx += h.state.num_of_matches_at_last_render
224+
}
227225
idx %= h.state.num_of_matches_at_last_render
228226
h.state.SetCurrentIndex(idx)
229227
}

0 commit comments

Comments
 (0)