Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

feat(files): add incremental type-to-select

Petar Margetić0d52a4309ba8
diff --git a/src/files/app.zig b/src/files/app.zig
index 226d467..7e3033c 100644
--- a/src/files/app.zig
+++ b/src/files/app.zig
@@ -175,6 +175,9 @@ pub const App = struct {
// Selection & scroll
selection_anchor: ?usize = null,
focused_index: ?usize = null,
+ type_select_buffer: [256]u8 = undefined,
+ type_select_len: usize = 0,
+ type_select_last_ms: i64 = 0,
scroll_y: i32 = 0,
wheel_glide: anim.Anim = .{},
wheel_sidebar: bool = false,
@@ -854,6 +857,7 @@ pub const App = struct {
// app_keyboard.zig
pub const keyRepeats = app_keyboard.keyRepeats;
pub const handleKey = app_keyboard.handleKey;
+ pub const typeSelectAt = app_keyboard.typeSelectAt;
pub const selectIndex = app_keyboard.selectIndex;
pub const handleLocationKey = app_keyboard.handleLocationKey;
pub const activeEditor = app_keyboard.activeEditor;
diff --git a/src/files/app_keyboard.zig b/src/files/app_keyboard.zig
index 91c6467..6cad204 100644
--- a/src/files/app_keyboard.zig
+++ b/src/files/app_keyboard.zig
@@ -5,6 +5,8 @@ const App = @import("app.zig").App;
const c = @import("c.zig").api;
const recent = @import("recent.zig");
const shell_key_repeat = @import("ui").key_repeat;
+const fold = @import("ui").fold;
+const nowMs = @import("app_draw.zig").nowMs;
const open_with = @import("open_with.zig");
const properties = @import("properties.zig");
const Editor = @import("editor.zig").Editor;
@@ -29,6 +31,68 @@ pub fn keyRepeats(self: *const App, sym: u32, utf8: []const u8) bool {
sym == c.XKB_KEY_Page_Up or sym == c.XKB_KEY_Page_Down;
}
+const type_select_timeout_ms: i64 = 1000;
+
+fn sameFoldedText(a: []const u8, b: []const u8) bool {
+ const options: fold.Options = .{ .turkish = @import("i18n").isTurkish() };
+ return fold.startsWith(a, b, options) and fold.startsWith(b, a, options);
+}
+
+/// Select a matching visible name without opening it. The timestamp parameter
+/// keeps timeout and cycling behavior deterministic in tests.
+pub fn typeSelectAt(self: *App, utf8: []const u8, now_ms: i64) bool {
+ if (utf8.len == 0 or self.focus != .file_view or self.ctrl or self.alt or
+ self.chooser != null or self.transfer_view.active or self.items.items.len == 0)
+ {
+ return false;
+ }
+
+ const expired = self.type_select_len == 0 or
+ now_ms - self.type_select_last_ms > type_select_timeout_ms;
+
+ // Repeating the current single-character prefix cycles through matches.
+ if (!expired and self.type_select_len == utf8.len and
+ sameFoldedText(self.type_select_buffer[0..self.type_select_len], utf8))
+ {
+ self.type_select_last_ms = now_ms;
+ const start = if (self.focused_index) |index|
+ (index + 1) % self.items.items.len
+ else
+ 0;
+ const options: fold.Options = .{ .turkish = @import("i18n").isTurkish() };
+ for (0..self.items.items.len) |offset| {
+ const index = (start + offset) % self.items.items.len;
+ if (fold.startsWith(self.items.items[index].text(), utf8, options)) {
+ self.selectIndex(index);
+ return true;
+ }
+ }
+ return true;
+ }
+
+ if (expired or utf8.len > self.type_select_buffer.len - self.type_select_len) {
+ self.type_select_len = 0;
+ }
+ if (utf8.len > self.type_select_buffer.len) return true;
+
+ @memcpy(
+ self.type_select_buffer[self.type_select_len..][0..utf8.len],
+ utf8,
+ );
+ self.type_select_len += utf8.len;
+ self.type_select_last_ms = now_ms;
+
+ const prefix = self.type_select_buffer[0..self.type_select_len];
+ const options: fold.Options = .{ .turkish = @import("i18n").isTurkish() };
+ for (self.items.items, 0..) |item, index| {
+ if (fold.startsWith(item.text(), prefix, options)) {
+ self.selectIndex(index);
+ break;
+ }
+ }
+ return true;
+}
+
pub fn handleKey(self: *App, sym: u32, utf8: []const u8) void {
self.cancelWheel();
if (sym == c.XKB_KEY_Escape and self.selection_band != null) {
@@ -200,6 +264,7 @@ pub fn handleKey(self: *App, sym: u32, utf8: []const u8) void {
// Arrow keys and scrolling in file view
if (self.items.items.len == 0) return;
+ if (typeSelectAt(self, utf8, nowMs())) return;
if (sym == c.XKB_KEY_Down or sym == c.XKB_KEY_Right) {
const step: usize = if (sym == c.XKB_KEY_Down) @intCast(self.columns()) else 1;
diff --git a/src/files/app_tests.zig b/src/files/app_tests.zig
index 2550703..46c40b6 100644
--- a/src/files/app_tests.zig
+++ b/src/files/app_tests.zig
@@ -364,6 +364,59 @@ test "keyboard ranges and focus-only movement in grid and list" {
}
}
+test "type select extends prefixes and matches displayed labels case insensitively" {
+ var app = try testApp();
+ defer app.deinit();
+ try testItems(&app, 4);
+ app.focus = .file_view;
+ app.items.items[0].name = "Daisy";
+ app.items.items[1].name = "different";
+ app.items.items[1].label = "Documents";
+ app.items.items[2].name = "Other";
+ app.items.items[3].name = "unrelated";
+
+ try std.testing.expect(app.typeSelectAt("d", 100));
+ try std.testing.expectEqual(@as(?usize, 0), app.focused_index);
+ try std.testing.expect(app.typeSelectAt("O", 200));
+ try std.testing.expectEqual(@as(?usize, 1), app.focused_index);
+}
+
+test "type select cycles repeated letters and resets after timeout" {
+ var app = try testApp();
+ defer app.deinit();
+ try testItems(&app, 4);
+ app.focus = .file_view;
+ app.items.items[0].name = "cat";
+ app.items.items[1].name = "car";
+ app.items.items[2].name = "Cow";
+ app.items.items[3].name = "Other";
+
+ try std.testing.expect(app.typeSelectAt("c", 100));
+ try std.testing.expectEqual(@as(?usize, 0), app.focused_index);
+ try std.testing.expect(app.typeSelectAt("C", 200));
+ try std.testing.expectEqual(@as(?usize, 1), app.focused_index);
+ try std.testing.expect(app.typeSelectAt("c", 300));
+ try std.testing.expectEqual(@as(?usize, 2), app.focused_index);
+ try std.testing.expect(app.typeSelectAt("c", 400));
+ try std.testing.expectEqual(@as(?usize, 0), app.focused_index);
+
+ try std.testing.expect(app.typeSelectAt("o", 1500));
+ try std.testing.expectEqual(@as(?usize, 3), app.focused_index);
+}
+
+test "type select matches Croatian letters case insensitively" {
+ var app = try testApp();
+ defer app.deinit();
+ try testItems(&app, 3);
+ app.focus = .file_view;
+ app.items.items[0].name = "žirafa";
+ app.items.items[1].name = "čarobnjak";
+ app.items.items[2].name = "ostalo";
+
+ try std.testing.expect(app.typeSelectAt("Č", 100));
+ try std.testing.expectEqual(@as(?usize, 1), app.focused_index);
+}
+
test "history restores names and scroll while tolerating removed items" {
var app = try testApp();
defer app.deinit();