scroll_page_fractional keybinding

This commit is contained in:
Mitchell Hashimoto
2023-08-09 07:37:18 -07:00
parent a8380e937d
commit 0ed76b4300
2 changed files with 34 additions and 0 deletions

View File

@ -2080,6 +2080,15 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !void
try self.io_thread.wakeup.notify();
},
.scroll_page_fractional => |fraction| {
const rows: f32 = @floatFromInt(self.grid_size.rows);
const delta: isize = @intFromFloat(@floor(fraction * rows));
_ = self.io_thread.mailbox.push(.{
.scroll_viewport = .{ .delta = delta },
}, .{ .forever = {} });
try self.io_thread.wakeup.notify();
},
.jump_to_prompt => |delta| {
_ = self.io_thread.mailbox.push(.{
.jump_to_prompt = @intCast(delta),

View File

@ -132,6 +132,14 @@ pub fn parse(input: []const u8) !Binding {
break :action @unionInit(Action, field.name, value);
},
.Float => {
const idx = colonIdx orelse return Error.InvalidFormat;
const param = actionRaw[idx + 1 ..];
const value = std.fmt.parseFloat(field.type, param) catch
return Error.InvalidFormat;
break :action @unionInit(Action, field.name, value);
},
else => unreachable,
},
}
@ -182,6 +190,7 @@ pub const Action = union(enum) {
scroll_to_bottom: void,
scroll_page_up: void,
scroll_page_down: void,
scroll_page_fractional: f32,
/// Jump the viewport forward or back by prompt. Positive
/// number is the number of prompts to jump forward, negative
@ -448,3 +457,19 @@ test "parse: action with int" {
try testing.expectEqual(@as(i16, 10), binding.action.jump_to_prompt);
}
}
test "parse: action with float" {
const testing = std.testing;
// parameter
{
const binding = try parse("a=scroll_page_fractional:-0.5");
try testing.expect(binding.action == .scroll_page_fractional);
try testing.expectEqual(@as(f32, -0.5), binding.action.scroll_page_fractional);
}
{
const binding = try parse("a=scroll_page_fractional:+0.5");
try testing.expect(binding.action == .scroll_page_fractional);
try testing.expectEqual(@as(f32, 0.5), binding.action.scroll_page_fractional);
}
}