srctree

Gregory Mullen parent 0b892b6d 9e5d7e27
move highlighting into its own file

src/child-process.zig added: 88, removed: 61, total 27
@@ -67,7 +67,7 @@ pub fn stdin(self: *ChildProc, data: []const u8, more: bool) !usize {
 
pub fn stdpoll(_: *ChildProc, fd: *std.os.pollfd, buf: []u8) ![]const u8 {
while (true) {
var poll: []std.os.pollfd = &[_]std.os.pollfd{fd};
const poll: []std.os.pollfd = &[_]std.os.pollfd{fd};
const events_len = std.os.poll(poll, std.math.maxInt(i32)) catch unreachable;
if (events_len == 0) continue;
if (fd.revents & std.os.POLL.IN != 0) {
 
src/endpoints/repos.zig added: 88, removed: 61, total 27
@@ -23,6 +23,7 @@ const Humanize = @import("../humanize.zig");
const Ini = @import("../ini.zig");
const Repos = @import("../repos.zig");
const Git = @import("../git.zig");
const Highlighting = @import("../syntax-highlight.zig");
 
const Commits = @import("repos/commits.zig");
const Diffs = @import("repos/diffs.zig");
@@ -308,21 +309,6 @@ fn treeBlob(ctx: *Context) Error!void {
return tree(ctx, &repo, &files);
}
 
fn guessLang(name: []const u8) ?[]const u8 {
if (std.mem.endsWith(u8, name, ".zig")) {
return "zig";
} else if (std.mem.endsWith(u8, name, ".html")) {
return "html";
} else if (std.mem.endsWith(u8, name, ".h")) {
return "cpp";
} else if (std.mem.endsWith(u8, name, ".c")) {
return "c";
} else if (std.mem.endsWith(u8, name, ".cpp")) {
return "cpp";
}
return null;
}
 
const BlameCommit = struct {
sha: []const u8,
parent: ?[]const u8 = null,
@@ -423,8 +409,8 @@ fn blame(ctx: *Context) Error!void {
try source_lines.append('\n');
}
 
const formatted = if (guessLang(blame_file)) |lang| fmt: {
var pre = try highlight(ctx.alloc, lang, source_lines.items);
const formatted = if (Highlighting.Language.guessFromFilename(blame_file)) |lang| fmt: {
var pre = try Highlighting.highlight(ctx.alloc, lang, source_lines.items);
break :fmt pre[28..][0 .. pre.len - 38];
} else Bleach.sanitizeAlloc(ctx.alloc, source_lines.items, .{}) catch return error.Unknown;
 
@@ -451,44 +437,6 @@ fn blame(ctx: *Context) Error!void {
try ctx.sendTemplate(&tmpl);
}
 
fn highlight(a: Allocator, lang: []const u8, text: []const u8) ![]u8 {
var child = std.ChildProcess.init(&[_][]const u8{ "pygmentize", "-f", "html", "-l", lang }, a);
child.stdin_behavior = .Pipe;
child.stdout_behavior = .Pipe;
child.stderr_behavior = .Ignore;
child.expand_arg0 = .no_expand;
child.spawn() catch unreachable;
 
const err_mask = std.posix.POLL.ERR | std.posix.POLL.NVAL | std.posix.POLL.HUP;
var poll_fd = [_]std.posix.pollfd{
.{
.fd = child.stdout.?.handle,
.events = std.posix.POLL.IN,
.revents = undefined,
},
};
_ = std.posix.write(child.stdin.?.handle, text) catch unreachable;
std.posix.close(child.stdin.?.handle);
child.stdin = null;
var buf = std.ArrayList(u8).init(a);
const abuf = try a.alloc(u8, 0xffffff);
while (true) {
const events_len = std.posix.poll(&poll_fd, std.math.maxInt(i32)) catch unreachable;
if (events_len == 0) continue;
if (poll_fd[0].revents & std.posix.POLL.IN != 0) {
const amt = std.posix.read(poll_fd[0].fd, abuf) catch unreachable;
if (amt == 0) break;
try buf.appendSlice(abuf[0..amt]);
} else if (poll_fd[0].revents & err_mask != 0) {
break;
}
}
a.free(abuf);
 
_ = child.wait() catch unreachable;
return try buf.toOwnedSlice();
}
 
fn wrapLineNumbersBlame(
a: Allocator,
blames: []BlameLine,
@@ -561,8 +509,8 @@ fn blob(ctx: *Context, repo: *Git.Repo, pfiles: Git.Tree) Error!void {
 
const d2 = reader.readAllAlloc(ctx.alloc, 0xffffff) catch unreachable;
 
if (guessLang(blb.name)) |lang| {
const pre = try highlight(ctx.alloc, lang, d2);
if (Highlighting.Language.guessFromFilename(blb.name)) |lang| {
const pre = try Highlighting.highlight(ctx.alloc, lang, d2);
formatted = pre[28..][0 .. pre.len - 38];
} else {
formatted = Bleach.sanitizeAlloc(ctx.alloc, d2, .{}) catch return error.Unknown;
 
filename was Deleted added: 88, removed: 61, total 27
@@ -0,0 +1,79 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
 
pub const Language = enum {
c,
cpp,
h,
html,
zig,
 
pub fn toString(l: Language) ![]const u8 {
return switch (l) {
.c => "c",
.cpp, .h => "cpp",
.html => "html",
.zig => "zig",
//else => error.LanguageNotSupported,
};
}
 
pub fn guessFromFilename(name: []const u8) ?Language {
if (std.mem.endsWith(u8, name, ".zig")) {
return .zig;
} else if (std.mem.endsWith(u8, name, ".html")) {
return .html;
} else if (std.mem.endsWith(u8, name, ".h")) {
return .cpp;
} else if (std.mem.endsWith(u8, name, ".c")) {
return .c;
} else if (std.mem.endsWith(u8, name, ".cpp")) {
return .cpp;
}
return null;
}
};
 
pub fn highlight(a: Allocator, lang: Language, text: []const u8) ![]u8 {
var child = std.ChildProcess.init(&[_][]const u8{
"pygmentize",
"-f",
"html",
"-l",
try lang.toString(),
}, a);
child.stdin_behavior = .Pipe;
child.stdout_behavior = .Pipe;
child.stderr_behavior = .Ignore;
child.expand_arg0 = .no_expand;
child.spawn() catch unreachable;
 
const err_mask = std.posix.POLL.ERR | std.posix.POLL.NVAL | std.posix.POLL.HUP;
var poll_fd = [_]std.posix.pollfd{
.{
.fd = child.stdout.?.handle,
.events = std.posix.POLL.IN,
.revents = undefined,
},
};
_ = std.posix.write(child.stdin.?.handle, text) catch unreachable;
std.posix.close(child.stdin.?.handle);
child.stdin = null;
var buf = std.ArrayList(u8).init(a);
const abuf = try a.alloc(u8, 0xffffff);
while (true) {
const events_len = std.posix.poll(&poll_fd, std.math.maxInt(i32)) catch unreachable;
if (events_len == 0) continue;
if (poll_fd[0].revents & std.posix.POLL.IN != 0) {
const amt = std.posix.read(poll_fd[0].fd, abuf) catch unreachable;
if (amt == 0) break;
try buf.appendSlice(abuf[0..amt]);
} else if (poll_fd[0].revents & err_mask != 0) {
break;
}
}
a.free(abuf);
 
_ = child.wait() catch unreachable;
return try buf.toOwnedSlice();
}