@@ -128,6 +128,10 @@ pub const State = struct {
128
128
pub fn init (allocator : Allocator , config : Config ) ! State {
129
129
const vx = try vaxis .init (allocator , .{});
130
130
131
+ const preview = if (config .preview ) | cmd | blk : {
132
+ break :blk Previewer .init (allocator , cmd );
133
+ } else null ;
134
+
131
135
return .{
132
136
.allocator = allocator ,
133
137
.config = config ,
@@ -139,6 +143,8 @@ pub const State = struct {
139
143
.selected_rows = ArrayToggleSet (usize ).init (allocator ),
140
144
.offset = 0 ,
141
145
.query = EditBuffer .init (allocator ),
146
+
147
+ .preview = preview ,
142
148
};
143
149
}
144
150
@@ -198,12 +204,12 @@ pub const State = struct {
198
204
}
199
205
200
206
// The selection changed and the child process should be respawned
201
- // if (state.selection_changed) if (state.preview) |*preview| {
202
- // state.selection_changed = false;
203
- // if (filtered.len > 0) {
204
- // try preview.spawn(filtered[state.selected + state.offset].str);
205
- // } else try preview.reset();
206
- // };
207
+ if (state .selection_changed or true ) if (state .preview ) | * preview | {
208
+ state .selection_changed = false ;
209
+ if (filtered .len > 0 ) {
210
+ try preview .spawn (filtered [state .selected + state .offset ].str );
211
+ } else try preview .reset ();
212
+ };
207
213
208
214
try state .draw (tokens , filtered , candidates .len );
209
215
try state .vx .render (state .tty .anyWriter ());
@@ -300,23 +306,22 @@ pub const State = struct {
300
306
const win = state .vx .window ();
301
307
win .clear ();
302
308
303
- const child = win .child (.{ .height = .{ .limit = state .config .height } });
304
-
305
309
const width = state .vx .screen .width ;
306
310
const preview_width : usize = if (state .preview ) | _ |
307
311
@intFromFloat (@as (f64 , @floatFromInt (width )) * state .preview_width )
308
312
else
309
313
0 ;
310
- const items_width = width - preview_width - @as (usize , if (state .preview ) | _ | 2 else 0 );
311
- _ = items_width ;
314
+
315
+ const items_width = width - preview_width ;
316
+ const items = win .child (.{ .height = .{ .limit = state .config .height }, .width = .{ .limit = items_width } });
312
317
313
318
const height = @min (state .vx .screen .height , state .config .height );
314
319
315
320
// draw the candidates
316
321
var line : usize = 0 ;
317
322
while (line < height - 1 ) : (line += 1 ) {
318
323
if (line < candidates .len ) state .drawCandidate (
319
- child ,
324
+ items ,
320
325
line + 1 ,
321
326
candidates [line + state .offset ],
322
327
tokens ,
@@ -332,40 +337,45 @@ pub const State = struct {
332
337
if (num_selected > 0 ) {
333
338
const stats = try std .fmt .bufPrint (& buf , "{}/{} [{}]" , .{ candidates .len , total_candidates , num_selected });
334
339
const stats_width = numDigits (candidates .len ) + numDigits (total_candidates ) + numDigits (num_selected ) + 4 ;
335
- _ = try child .printSegment (.{ .text = stats }, .{ .col_offset = width - stats_width , .row_offset = 0 });
340
+ _ = try items .printSegment (.{ .text = stats }, .{ .col_offset = items_width - stats_width , .row_offset = 0 });
336
341
} else {
337
342
const stats = try std .fmt .bufPrint (& buf , "{}/{}" , .{ candidates .len , total_candidates });
338
343
const stats_width = numDigits (candidates .len ) + numDigits (total_candidates ) + 1 ;
339
- _ = try child .printSegment (.{ .text = stats }, .{ .col_offset = width - stats_width , .row_offset = 0 });
344
+ _ = try items .printSegment (.{ .text = stats }, .{ .col_offset = items_width - stats_width , .row_offset = 0 });
340
345
}
341
346
}
342
347
343
348
// draw the prompt
344
349
// TODO: handle display of queries longer than the screen width
345
350
// const query_width = state.query.slice().len;
346
- _ = try child .print (&.{
351
+ _ = try items .print (&.{
347
352
.{ .text = state .config .prompt },
348
353
.{ .text = state .query .slice () },
349
354
}, .{ .col_offset = 0 , .row_offset = 0 });
350
355
351
- // // draw a preview window if requested
352
- // if (state.preview) |*preview| {
353
- // var lines = preview.lines();
354
-
355
- // for (0..height) |_| {
356
- // terminal.cursorCol(items_width + 2);
357
- // terminal.write("│ ");
356
+ // draw a preview window if requested
357
+ if (state .preview ) | * preview | {
358
+ const preview_win = win .child (.{
359
+ .x_off = items_width ,
360
+ .y_off = 0 ,
361
+ .height = .{ .limit = state .config .height },
362
+ .width = .{ .limit = preview_width },
363
+ .border = .{ .where = .left },
364
+ });
358
365
359
- // if (lines.next()) |preview_line| {
360
- // terminal.write(preview_line[0..@min(preview_line.len, preview_width - 1)]);
361
- // }
366
+ var lines = preview .lines ();
362
367
363
- // terminal.cursorDown(1);
364
- // }
365
- // terminal.sgr(.reset);
366
- // } else terminal.cursorDown(height);
368
+ for (0.. height ) | l | {
369
+ if (lines .next ()) | preview_line | {
370
+ _ = try preview_win .printSegment (
371
+ .{ .text = preview_line },
372
+ .{ .row_offset = l , .wrap = .none },
373
+ );
374
+ }
375
+ }
376
+ }
367
377
368
- child .showCursor (state .config .prompt .len + state .query .cursor , 0 );
378
+ items .showCursor (state .config .prompt .len + state .query .cursor , 0 );
369
379
}
370
380
371
381
fn drawCandidate (
0 commit comments