refactor: move abbreviate_home to root module
This commit is contained in:
parent
f10559c635
commit
5fdcdba4b8
2 changed files with 22 additions and 20 deletions
19
src/main.zig
19
src/main.zig
|
@ -600,3 +600,22 @@ pub fn shorten_path(buf: []u8, path: []const u8, removed_prefix: *usize, max_len
|
||||||
@memcpy(buf[ellipsis.len .. max_len + ellipsis.len], path[prefix..]);
|
@memcpy(buf[ellipsis.len .. max_len + ellipsis.len], path[prefix..]);
|
||||||
return buf[0 .. max_len + ellipsis.len];
|
return buf[0 .. max_len + ellipsis.len];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn abbreviate_home(buf: []u8, path: []const u8) []const u8 {
|
||||||
|
const a = std.heap.c_allocator;
|
||||||
|
if (builtin.os.tag == .windows) return path;
|
||||||
|
if (!std.fs.path.isAbsolute(path)) return path;
|
||||||
|
const homedir = std.posix.getenv("HOME") orelse return path;
|
||||||
|
const homerelpath = std.fs.path.relative(a, homedir, path) catch return path;
|
||||||
|
defer a.free(homerelpath);
|
||||||
|
if (homerelpath.len == 0) {
|
||||||
|
return "~";
|
||||||
|
} else if (homerelpath.len > 3 and std.mem.eql(u8, homerelpath[0..3], "../")) {
|
||||||
|
return path;
|
||||||
|
} else {
|
||||||
|
buf[0] = '~';
|
||||||
|
buf[1] = '/';
|
||||||
|
@memcpy(buf[2 .. homerelpath.len + 2], homerelpath);
|
||||||
|
return buf[0 .. homerelpath.len + 2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -173,7 +173,7 @@ pub fn receive(self: *Self, _: *Button.State(Self), _: tp.pid_ref, m: tp.message
|
||||||
self.name = self.name_buf[0..file_path.len];
|
self.name = self.name_buf[0..file_path.len];
|
||||||
self.file_exists = true;
|
self.file_exists = true;
|
||||||
self.file_dirty = false;
|
self.file_dirty = false;
|
||||||
self.abbrv_home();
|
self.name = root.abbreviate_home(&self.name_buf, self.name);
|
||||||
} else if (try m.match(.{ "E", "open", tp.extract(&file_path), tp.extract(&self.file_exists), tp.extract(&file_type), tp.extract(&file_icon), tp.extract(&self.file_color) })) {
|
} else if (try m.match(.{ "E", "open", tp.extract(&file_path), tp.extract(&self.file_exists), tp.extract(&file_type), tp.extract(&file_icon), tp.extract(&self.file_color) })) {
|
||||||
@memcpy(self.name_buf[0..file_path.len], file_path);
|
@memcpy(self.name_buf[0..file_path.len], file_path);
|
||||||
self.name = self.name_buf[0..file_path.len];
|
self.name = self.name_buf[0..file_path.len];
|
||||||
|
@ -183,7 +183,7 @@ pub fn receive(self: *Self, _: *Button.State(Self), _: tp.pid_ref, m: tp.message
|
||||||
self.file_icon_buf[file_icon.len] = 0;
|
self.file_icon_buf[file_icon.len] = 0;
|
||||||
self.file_icon = self.file_icon_buf[0..file_icon.len :0];
|
self.file_icon = self.file_icon_buf[0..file_icon.len :0];
|
||||||
self.file_dirty = false;
|
self.file_dirty = false;
|
||||||
self.abbrv_home();
|
self.name = root.abbreviate_home(&self.name_buf, self.name);
|
||||||
self.file = true;
|
self.file = true;
|
||||||
} else if (try m.match(.{ "E", "close" })) {
|
} else if (try m.match(.{ "E", "close" })) {
|
||||||
self.name = "";
|
self.name = "";
|
||||||
|
@ -217,22 +217,5 @@ fn show_project(self: *Self) void {
|
||||||
const project_name = tp.env.get().str("project");
|
const project_name = tp.env.get().str("project");
|
||||||
@memcpy(self.name_buf[0..project_name.len], project_name);
|
@memcpy(self.name_buf[0..project_name.len], project_name);
|
||||||
self.name = self.name_buf[0..project_name.len];
|
self.name = self.name_buf[0..project_name.len];
|
||||||
self.abbrv_home();
|
self.name = root.abbreviate_home(&self.name_buf, self.name);
|
||||||
}
|
|
||||||
|
|
||||||
fn abbrv_home(self: *Self) void {
|
|
||||||
if (builtin.os.tag == .windows) return;
|
|
||||||
if (!std.fs.path.isAbsolute(self.name)) return;
|
|
||||||
const homedir = std.posix.getenv("HOME") orelse return;
|
|
||||||
const homerelpath = std.fs.path.relative(self.a, homedir, self.name) catch return;
|
|
||||||
if (homerelpath.len == 0) {
|
|
||||||
self.name = "~";
|
|
||||||
} else if (homerelpath.len > 3 and std.mem.eql(u8, homerelpath[0..3], "../")) {
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
self.name_buf[0] = '~';
|
|
||||||
self.name_buf[1] = '/';
|
|
||||||
@memcpy(self.name_buf[2 .. homerelpath.len + 2], homerelpath);
|
|
||||||
self.name = self.name_buf[0 .. homerelpath.len + 2];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue