summaryrefslogtreecommitdiff
path: root/core/Field.zig
blob: 2f668001bca5bc2ae803cc51f17e6659492fd97c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//! A field typed in a tag's head (walk UI, pick-ui SPEC.md §3.2, the typing
//! row): one line of text and its caret, in a fixed buffer, each view's
//! own (TagState.field). Help's query, a picker's, a prompt's: the keys
//! that edit it are a line editor's (readline's, helix's prompt's), and
//! the state that owns it takes the rest (Enter, Esc, the arrows a list
//! steps with).
const Field = @This();

const std = @import("std");
const pardes = @import("root.zig");
const modal = @import("modal.zig");
const Key = pardes.Key;

/// The longest field, in bytes: a query, a command line, a pattern.
pub const max = 256;

buf: [max]u8 = undefined,
len: u16 = 0,
/// A byte offset in the text, at a character's start.
caret: u16 = 0,

/// The caret held to the text there is (walk.zig `.place`): a damaged
/// image's past it typed out of the buffer.
pub const replica = .{ .len = .{.{ "buf", "len" }}, .place = .{.{ "caret", "lastCaret" }} };

pub fn lastCaret(f: *const Field) usize {
    return f.len;
}

pub fn text(f: *const Field) []const u8 {
    return f.buf[0..f.len];
}

pub fn clear(f: *Field) void {
    f.len = 0;
    f.caret = 0;
}

/// `s` in place of the text, the caret at its end (a history's line).
pub fn set(f: *Field, s: []const u8) void {
    var n = @min(s.len, max);
    while (n > 0 and n < s.len and s[n] & 0xC0 == 0x80) n -= 1;
    @memcpy(f.buf[0..n], s[0..n]);
    f.len = @intCast(n);
    f.caret = f.len;
}

/// An editing key taken: true, the text or the caret moved (or a key that
/// would have, at an edge). False for a key the field has no use for,
/// which its owner takes.
pub fn key(f: *Field, k: Key) bool {
    const t = f.text();
    const ctrl = k.ctrl and !k.alt;
    const alt = k.alt and !k.ctrl;
    const plain = !k.ctrl and !k.alt;
    if (plain and k.cp == Key.backspace or ctrl and k.cp == 'h') {
        if (f.caret > 0) f.cut(prev(t, f.caret), f.caret);
    } else if (plain and k.cp == Key.delete or ctrl and k.cp == 'd') {
        if (f.caret < f.len) f.cut(f.caret, @intCast(modal.nextGrapheme(t, f.caret)));
    } else if (plain and k.cp == Key.left or ctrl and k.cp == 'b') {
        f.caret = prev(t, f.caret);
    } else if (plain and k.cp == Key.right or ctrl and k.cp == 'f') {
        if (f.caret < f.len) f.caret = @intCast(modal.nextGrapheme(t, f.caret));
    } else if (plain and k.cp == Key.home or ctrl and k.cp == 'a') {
        f.caret = 0;
    } else if (plain and k.cp == Key.end or ctrl and k.cp == 'e') {
        f.caret = f.len;
    } else if (ctrl and k.cp == 'u') {
        f.cut(0, f.caret);
    } else if (ctrl and k.cp == 'k') {
        f.cut(f.caret, f.len);
    } else if (ctrl and k.cp == 'w' or alt and k.cp == Key.backspace) {
        f.cut(wordBack(t, f.caret), f.caret);
    } else if (alt and k.cp == 'b') {
        f.caret = wordBack(t, f.caret);
    } else if (alt and k.cp == 'f') {
        f.caret = wordOn(t, f.caret);
    } else if (plain and k.cp >= 0x20 and k.cp != Key.backspace and k.cp < 0xF0000) {
        var utf: [4]u8 = undefined;
        const n = std.unicode.utf8Encode(k.cp, &utf) catch return true;
        f.insert(if (k.text.len > 0) k.text else utf[0..n]);
    } else return false;
    return true;
}

/// `s` typed at the caret; what does not fit is dropped whole.
pub fn insert(f: *Field, s: []const u8) void {
    if (f.len + s.len > max) return;
    std.mem.copyBackwards(u8, f.buf[f.caret + s.len .. f.len + s.len], f.buf[f.caret..f.len]);
    @memcpy(f.buf[f.caret..][0..s.len], s);
    f.len += @intCast(s.len);
    f.caret += @intCast(s.len);
}

fn cut(f: *Field, lo: u16, hi: u16) void {
    std.mem.copyForwards(u8, f.buf[lo .. f.len - (hi - lo)], f.buf[hi..f.len]);
    f.len -= hi - lo;
    f.caret = lo;
}

fn prev(t: []const u8, at: u16) u16 {
    var i = at;
    while (i > 0) {
        i -= 1;
        if (t[i] & 0xC0 != 0x80) break;
    }
    return i;
}

fn wordBack(t: []const u8, at: u16) u16 {
    var i = at;
    while (i > 0 and t[i - 1] == ' ') i -= 1;
    while (i > 0 and t[i - 1] != ' ') i -= 1;
    return i;
}

fn wordOn(t: []const u8, at: u16) u16 {
    var i = at;
    while (i < t.len and t[i] == ' ') i += 1;
    while (i < t.len and t[i] != ' ') i += 1;
    return i;
}

test "a field types, moves and deletes as a line editor does" {
    var f: Field = .{};
    for ("find me") |c| try std.testing.expect(f.key(.{ .cp = c }));
    try std.testing.expectEqualStrings("find me", f.text());
    try std.testing.expect(f.key(.{ .cp = 'w', .ctrl = true }));
    try std.testing.expectEqualStrings("find ", f.text());
    try std.testing.expect(f.key(.{ .cp = Key.home }));
    try std.testing.expect(f.key(.{ .cp = '>' }));
    try std.testing.expectEqualStrings(">find ", f.text());
    try std.testing.expectEqual(@as(u16, 1), f.caret);
    try std.testing.expect(f.key(.{ .cp = Key.backspace }));
    try std.testing.expectEqualStrings("find ", f.text());
    try std.testing.expect(f.key(.{ .cp = 0x65e5 })); // 日
    try std.testing.expect(f.key(.{ .cp = Key.left }));
    try std.testing.expectEqual(@as(u16, 0), f.caret);
    try std.testing.expect(f.key(.{ .cp = Key.delete }));
    try std.testing.expectEqualStrings("find ", f.text());
    try std.testing.expect(f.key(.{ .cp = 'k', .ctrl = true }));
    try std.testing.expectEqualStrings("", f.text());
    // Enter, Esc and the arrows a list steps with are its owner's.
    try std.testing.expect(!f.key(.{ .cp = Key.enter }));
    try std.testing.expect(!f.key(.{ .cp = Key.escape }));
    try std.testing.expect(!f.key(.{ .cp = Key.down }));
    try std.testing.expect(!f.key(.{ .cp = 'n', .ctrl = true }));
}