summaryrefslogtreecommitdiff
path: root/src/terminal/hyperlink.zig
blob: c608321b1c268cd081a15575d1e1d11dd8aa2c46 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const std = @import("std");
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const hash_map = @import("hash_map.zig");
const AutoOffsetHashMap = hash_map.AutoOffsetHashMap;
const pagepkg = @import("page.zig");
const size = @import("size.zig");
const Offset = size.Offset;
const Cell = pagepkg.Cell;
const Page = pagepkg.Page;
const RefCountedSet = @import("ref_counted_set.zig").RefCountedSet;
const Wyhash = std.hash.Wyhash;
const autoHash = std.hash.autoHash;
const autoHashStrat = std.hash.autoHashStrat;

/// The unique identifier for a hyperlink. This is at most the number of cells
/// that can fit in a single terminal page.
pub const Id = size.CellCountInt;

// The mapping of cell to hyperlink. We use an offset hash map to save space
// since its very unlikely a cell is a hyperlink, so its a waste to store
// the hyperlink ID in the cell itself.
pub const Map = AutoOffsetHashMap(Offset(Cell), Id);

/// A fully decoded hyperlink that may or may not have its
/// memory within a page. The memory location of this is dependent
/// on the context so users should check with the source of the
/// hyperlink.
pub const Hyperlink = struct {
    id: Hyperlink.Id,
    uri: []const u8,

    /// See PageEntry.Id
    pub const Id = union(enum) {
        explicit: []const u8,
        implicit: size.OffsetInt,
    };

    /// Deinit and deallocate all the pointers using the given
    /// allocator.
    ///
    /// WARNING: This should only be called if the hyperlink was
    /// heap-allocated. This DOES NOT need to be unconditionally
    /// called.
    pub fn deinit(self: *const Hyperlink, alloc: Allocator) void {
        alloc.free(self.uri);
        switch (self.id) {
            .implicit => {},
            .explicit => |v| alloc.free(v),
        }
    }

    /// Duplicate a hyperlink by allocating all values with the
    /// given allocator. The returned hyperlink should have deinit
    /// called.
    pub fn dupe(
        self: *const Hyperlink,
        alloc: Allocator,
    ) Allocator.Error!Hyperlink {
        const uri = try alloc.dupe(u8, self.uri);
        errdefer alloc.free(uri);

        const id: Hyperlink.Id = switch (self.id) {
            .implicit => self.id,
            .explicit => |v| .{ .explicit = try alloc.dupe(u8, v) },
        };
        errdefer switch (id) {
            .implicit => {},
            .explicit => |v| alloc.free(v),
        };

        return .{ .id = id, .uri = uri };
    }
};

/// A hyperlink that has been committed to page memory. This
/// is a "page entry" because while it represents a hyperlink,
/// some decoding (pointer chasing) is still necessary to get the
/// fully realized ID, URI, etc.
pub const PageEntry = struct {
    id: PageEntry.Id,
    uri: Offset(u8).Slice,

    pub const Id = union(enum) {
        /// An explicitly provided ID via the OSC8 sequence.
        explicit: Offset(u8).Slice,

        /// No ID was provided so we auto-generate the ID based on an
        /// incrementing counter attached to the screen.
        implicit: size.OffsetInt,
    };

    /// Duplicate this hyperlink from one page to another.
    pub fn dupe(
        self: *const PageEntry,
        self_page: *const Page,
        dst_page: *Page,
    ) error{OutOfMemory}!PageEntry {
        var copy = self.*;

        // If the pages are the same then we can return a shallow copy.
        if (self_page == dst_page) return copy;

        // Copy the URI
        {
            const uri = self.uri.offset.ptr(self_page.memory)[0..self.uri.len];
            const buf = try dst_page.string_alloc.alloc(u8, dst_page.memory, uri.len);
            @memcpy(buf, uri);
            copy.uri = .{
                .offset = size.getOffset(u8, dst_page.memory, &buf[0]),
                .len = uri.len,
            };
        }
        errdefer dst_page.string_alloc.free(
            dst_page.memory,
            copy.uri.offset.ptr(dst_page.memory)[0..copy.uri.len],
        );

        // Copy the ID
        switch (copy.id) {
            .implicit => {}, // Shallow is fine
            .explicit => |slice| {
                const id = slice.offset.ptr(self_page.memory)[0..slice.len];
                const buf = try dst_page.string_alloc.alloc(u8, dst_page.memory, id.len);
                @memcpy(buf, id);
                copy.id = .{ .explicit = .{
                    .offset = size.getOffset(u8, dst_page.memory, &buf[0]),
                    .len = id.len,
                } };
            },
        }
        errdefer switch (copy.id) {
            .implicit => {},
            .explicit => |v| dst_page.string_alloc.free(
                dst_page.memory,
                v.offset.ptr(dst_page.memory)[0..v.len],
            ),
        };

        return copy;
    }

    pub fn hash(self: *const PageEntry, base: anytype) u64 {
        var hasher = Wyhash.init(0);
        autoHash(&hasher, std.meta.activeTag(self.id));
        switch (self.id) {
            .implicit => |v| autoHash(&hasher, v),
            .explicit => |slice| autoHashStrat(
                &hasher,
                slice.offset.ptr(base)[0..slice.len],
                .Deep,
            ),
        }
        autoHashStrat(
            &hasher,
            self.uri.offset.ptr(base)[0..self.uri.len],
            .Deep,
        );
        return hasher.final();
    }

    pub fn eql(
        self: *const PageEntry,
        self_base: anytype,
        other: *const PageEntry,
        other_base: anytype,
    ) bool {
        if (std.meta.activeTag(self.id) != std.meta.activeTag(other.id)) return false;
        switch (self.id) {
            .implicit => if (self.id.implicit != other.id.implicit) return false,
            .explicit => {
                const self_ptr = self.id.explicit.offset.ptr(self_base);
                const other_ptr = other.id.explicit.offset.ptr(other_base);
                if (!std.mem.eql(
                    u8,
                    self_ptr[0..self.id.explicit.len],
                    other_ptr[0..other.id.explicit.len],
                )) return false;
            },
        }

        return std.mem.eql(
            u8,
            self.uri.offset.ptr(self_base)[0..self.uri.len],
            other.uri.offset.ptr(other_base)[0..other.uri.len],
        );
    }

    /// Free the memory for this entry from its page.
    pub fn free(
        self: *const PageEntry,
        page: *Page,
    ) void {
        const alloc = &page.string_alloc;
        switch (self.id) {
            .implicit => {},
            .explicit => |v| alloc.free(
                page.memory,
                v.offset.ptr(page.memory)[0..v.len],
            ),
        }
        alloc.free(
            page.memory,
            self.uri.offset.ptr(page.memory)[0..self.uri.len],
        );
    }
};

/// The set of hyperlinks. This is ref-counted so that a set of cells
/// can share the same hyperlink without duplicating the data.
pub const Set = RefCountedSet(
    PageEntry,
    Id,
    size.CellCountInt,
    struct {
        /// The page which holds the strings for items in this set.
        page: ?*Page = null,

        /// The page which holds the strings for items
        /// looked up with, e.g., `add` or `lookup`,
        /// if different from the destination page.
        src_page: ?*const Page = null,

        pub fn hash(self: *const @This(), link: PageEntry) u64 {
            return link.hash((self.src_page orelse self.page.?).memory);
        }

        pub fn eql(self: *const @This(), a: PageEntry, b: PageEntry) bool {
            return a.eql(
                (self.src_page orelse self.page.?).memory,
                &b,
                self.page.?.memory,
            );
        }

        pub fn deleted(self: *const @This(), link: PageEntry) void {
            link.free(self.page.?);
        }
    },
);