tangled
alpha
login
or
join now
brookjeynes.dev
/
jido
7
fork
atom
地圖 (Jido) is a lightweight Unix TUI file explorer designed for speed and simplicity.
7
fork
atom
overview
issues
pulls
pipelines
refactor: Image struct from app.zig -> image.zig
brookjeynes.dev
2 months ago
081b41aa
3af28fa9
+104
-95
3 changed files
expand all
collapse all
unified
split
src
app.zig
drawer.zig
image.zig
+2
-30
src/app.zig
···
8
8
const Directories = @import("./directories.zig");
9
9
const FileLogger = @import("./file_logger.zig");
10
10
const CircStack = @import("./circ_stack.zig").CircularStack;
11
11
+
const Image = @import("./image.zig");
11
12
const zuid = @import("zuid");
12
13
const vaxis = @import("vaxis");
13
14
const Key = vaxis.Key;
···
79
80
winsize: vaxis.Winsize,
80
81
};
81
82
82
82
-
pub const Image = struct {
83
83
-
const Status = enum {
84
84
-
ready,
85
85
-
processing,
86
86
-
failed,
87
87
-
};
88
88
-
89
89
-
///Only use on first transmission. Subsequent draws should use
90
90
-
///`Image.image`.
91
91
-
data: ?vaxis.zigimg.Image = null,
92
92
-
image: ?vaxis.Image = null,
93
93
-
path: ?[]const u8 = null,
94
94
-
status: Status = .processing,
95
95
-
96
96
-
pub fn deinit(self: @This(), alloc: std.mem.Allocator, vx: vaxis.Vaxis, tty: *vaxis.Tty) void {
97
97
-
if (self.image) |image| {
98
98
-
vx.freeImage(tty.writer(), image.id);
99
99
-
}
100
100
-
if (self.data) |data| {
101
101
-
var d = data;
102
102
-
d.deinit(alloc);
103
103
-
}
104
104
-
if (self.path) |path| alloc.free(path);
105
105
-
}
106
106
-
};
107
107
-
108
83
const actions_len = 100;
109
84
const image_cache_cap = 100;
110
85
···
132
107
yanked: ?struct { dir: []const u8, entry: std.fs.Dir.Entry } = null,
133
108
last_known_height: usize,
134
109
135
135
-
images: struct {
136
136
-
mutex: std.Thread.Mutex = .{},
137
137
-
cache: std.StringHashMap(Image),
138
138
-
},
110
110
+
images: Image.Cache,
139
111
140
112
pub fn init(alloc: std.mem.Allocator, entry_dir: ?[]const u8) !App {
141
113
var vx = try vaxis.init(alloc, .{
+3
-65
src/drawer.zig
···
8
8
const Git = @import("./git.zig");
9
9
const List = @import("./list.zig").List;
10
10
const zeit = @import("zeit");
11
11
+
const Image = @import("./image.zig");
11
12
12
13
const Drawer = @This();
13
14
···
231
232
} else {
232
233
if (cache_entry.data == null) {
233
234
const path = try app.alloc.dupe(u8, self.current_item_path);
234
234
-
processImage(app, path) catch {
235
235
+
Image.processImage(app.alloc, app, path) catch {
235
236
app.alloc.free(path);
236
237
break :unsupported;
237
238
};
···
271
272
}, .{});
272
273
273
274
const path = try app.alloc.dupe(u8, self.current_item_path);
274
274
-
processImage(app, path) catch {
275
275
+
Image.processImage(app.alloc, app, path) catch {
275
276
app.alloc.free(path);
276
277
break :unsupported;
277
278
};
···
651
652
.style = config.styles.notification.box,
652
653
}, .{ .wrap = .word });
653
654
}
654
654
-
655
655
-
fn processImage(app: *App, path: []const u8) error{ Unsupported, OutOfMemory }!void {
656
656
-
app.images.cache.put(path, .{ .path = path, .status = .processing }) catch {
657
657
-
const message = try std.fmt.allocPrint(app.alloc, "Failed to load image '{s}' - error occurred while attempting to add image to cache.", .{path});
658
658
-
defer app.alloc.free(message);
659
659
-
app.notification.write(message, .err) catch {};
660
660
-
if (app.file_logger) |file_logger| file_logger.write(message, .err) catch {};
661
661
-
return error.Unsupported;
662
662
-
};
663
663
-
664
664
-
const load_img_thread = std.Thread.spawn(.{}, loadImage, .{
665
665
-
app,
666
666
-
path,
667
667
-
}) catch {
668
668
-
app.images.mutex.lock();
669
669
-
if (app.images.cache.getPtr(path)) |entry| {
670
670
-
entry.status = .failed;
671
671
-
}
672
672
-
app.images.mutex.unlock();
673
673
-
674
674
-
const message = try std.fmt.allocPrint(app.alloc, "Failed to load image '{s}' - error occurred while attempting to spawn processing thread.", .{path});
675
675
-
defer app.alloc.free(message);
676
676
-
app.notification.write(message, .err) catch {};
677
677
-
if (app.file_logger) |file_logger| file_logger.write(message, .err) catch {};
678
678
-
679
679
-
return error.Unsupported;
680
680
-
};
681
681
-
load_img_thread.detach();
682
682
-
}
683
683
-
684
684
-
fn loadImage(app: *App, path: []const u8) error{OutOfMemory}!void {
685
685
-
var buf: [(1024 * 1024) * 5]u8 = undefined; // 5mb
686
686
-
const data = vaxis.zigimg.Image.fromFilePath(app.alloc, path, &buf) catch {
687
687
-
app.images.mutex.lock();
688
688
-
if (app.images.cache.getPtr(path)) |entry| {
689
689
-
entry.status = .failed;
690
690
-
}
691
691
-
app.images.mutex.unlock();
692
692
-
693
693
-
const message = try std.fmt.allocPrint(app.alloc, "Failed to load image '{s}' - error occurred while attempting to read image from path.", .{path});
694
694
-
defer app.alloc.free(message);
695
695
-
app.notification.write(message, .err) catch {};
696
696
-
if (app.file_logger) |file_logger| file_logger.write(message, .err) catch {};
697
697
-
698
698
-
return;
699
699
-
};
700
700
-
701
701
-
app.images.mutex.lock();
702
702
-
if (app.images.cache.getPtr(path)) |entry| {
703
703
-
entry.status = .ready;
704
704
-
entry.data = data;
705
705
-
entry.path = path;
706
706
-
} else {
707
707
-
const message = try std.fmt.allocPrint(app.alloc, "Failed to load image '{s}' - error occurred while attempting to add image to cache.", .{path});
708
708
-
defer app.alloc.free(message);
709
709
-
app.notification.write(message, .err) catch {};
710
710
-
if (app.file_logger) |file_logger| file_logger.write(message, .err) catch {};
711
711
-
return;
712
712
-
}
713
713
-
app.images.mutex.unlock();
714
714
-
715
715
-
app.loop.postEvent(.image_ready);
716
716
-
}
+99
src/image.zig
···
1
1
+
const std = @import("std");
2
2
+
const vaxis = @import("vaxis");
3
3
+
const App = @import("app.zig");
4
4
+
5
5
+
6
6
+
pub const Cache = struct {
7
7
+
mutex: std.Thread.Mutex = .{},
8
8
+
cache: std.StringHashMap(Image),
9
9
+
};
10
10
+
11
11
+
const Status = enum {
12
12
+
ready,
13
13
+
processing,
14
14
+
failed,
15
15
+
};
16
16
+
17
17
+
const Image = @This();
18
18
+
19
19
+
///Only use on first transmission. Subsequent draws should use
20
20
+
///`Image.image`.
21
21
+
data: ?vaxis.zigimg.Image = null,
22
22
+
image: ?vaxis.Image = null,
23
23
+
path: ?[]const u8 = null,
24
24
+
status: Status = .processing,
25
25
+
26
26
+
pub fn deinit(self: @This(), alloc: std.mem.Allocator, vx: vaxis.Vaxis, tty: *vaxis.Tty) void {
27
27
+
if (self.image) |image| {
28
28
+
vx.freeImage(tty.writer(), image.id);
29
29
+
}
30
30
+
if (self.data) |data| {
31
31
+
var d = data;
32
32
+
d.deinit(alloc);
33
33
+
}
34
34
+
if (self.path) |path| alloc.free(path);
35
35
+
}
36
36
+
37
37
+
pub fn processImage(alloc: std.mem.Allocator, app: *App, path: []const u8) error{ Unsupported, OutOfMemory }!void {
38
38
+
app.images.cache.put(path, .{ .path = path, .status = .processing }) catch {
39
39
+
const message = try std.fmt.allocPrint(alloc, "Failed to load image '{s}' - error occurred while attempting to add image to cache.", .{path});
40
40
+
defer alloc.free(message);
41
41
+
app.notification.write(message, .err) catch {};
42
42
+
if (app.file_logger) |file_logger| file_logger.write(message, .err) catch {};
43
43
+
return error.Unsupported;
44
44
+
};
45
45
+
46
46
+
const load_img_thread = std.Thread.spawn(.{}, loadImage, .{
47
47
+
alloc,
48
48
+
app,
49
49
+
path,
50
50
+
}) catch {
51
51
+
app.images.mutex.lock();
52
52
+
if (app.images.cache.getPtr(path)) |entry| {
53
53
+
entry.status = .failed;
54
54
+
}
55
55
+
app.images.mutex.unlock();
56
56
+
57
57
+
const message = try std.fmt.allocPrint(alloc, "Failed to load image '{s}' - error occurred while attempting to spawn processing thread.", .{path});
58
58
+
defer alloc.free(message);
59
59
+
app.notification.write(message, .err) catch {};
60
60
+
if (app.file_logger) |file_logger| file_logger.write(message, .err) catch {};
61
61
+
62
62
+
return error.Unsupported;
63
63
+
};
64
64
+
load_img_thread.detach();
65
65
+
}
66
66
+
67
67
+
fn loadImage(alloc: std.mem.Allocator, app: *App, path: []const u8) error{OutOfMemory}!void {
68
68
+
var buf: [(1024 * 1024) * 5]u8 = undefined; // 5mb
69
69
+
const data = vaxis.zigimg.Image.fromFilePath(alloc, path, &buf) catch {
70
70
+
app.images.mutex.lock();
71
71
+
if (app.images.cache.getPtr(path)) |entry| {
72
72
+
entry.status = .failed;
73
73
+
}
74
74
+
app.images.mutex.unlock();
75
75
+
76
76
+
const message = try std.fmt.allocPrint(alloc, "Failed to load image '{s}' - error occurred while attempting to read image from path.", .{path});
77
77
+
defer alloc.free(message);
78
78
+
app.notification.write(message, .err) catch {};
79
79
+
if (app.file_logger) |file_logger| file_logger.write(message, .err) catch {};
80
80
+
81
81
+
return;
82
82
+
};
83
83
+
84
84
+
app.images.mutex.lock();
85
85
+
if (app.images.cache.getPtr(path)) |entry| {
86
86
+
entry.status = .ready;
87
87
+
entry.data = data;
88
88
+
entry.path = path;
89
89
+
} else {
90
90
+
const message = try std.fmt.allocPrint(alloc, "Failed to load image '{s}' - error occurred while attempting to add image to cache.", .{path});
91
91
+
defer alloc.free(message);
92
92
+
app.notification.write(message, .err) catch {};
93
93
+
if (app.file_logger) |file_logger| file_logger.write(message, .err) catch {};
94
94
+
return;
95
95
+
}
96
96
+
app.images.mutex.unlock();
97
97
+
98
98
+
app.loop.postEvent(.image_ready);
99
99
+
}