remove stdio include and clean up

main
moss 2025-01-25 18:02:56 -06:00
parent 013f468703
commit c88a2815fa
1 changed files with 23 additions and 13 deletions

View File

@ -2,9 +2,6 @@ const std = @import("std");
const clap = @import("clap"); const clap = @import("clap");
const md4c = @import("md4c_h.zig"); const md4c = @import("md4c_h.zig");
const ansi = @import("ansi-term"); const ansi = @import("ansi-term");
const cstdio = @cImport({
@cInclude("stdio.h");
});
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
@ -97,30 +94,36 @@ const State = struct {
}; };
fn enter_block(blocktype: md4c.MD_BLOCKTYPE, detail: ?*anyopaque, userdata: ?*anyopaque) callconv(.C) c_int { fn enter_block(blocktype: md4c.MD_BLOCKTYPE, detail: ?*anyopaque, userdata: ?*anyopaque) callconv(.C) c_int {
//std.debug.print("enter {d}\n", .{blocktype});
const state: *State = @ptrCast(@alignCast(userdata)); const state: *State = @ptrCast(@alignCast(userdata));
if (blocktype == md4c.MD_BLOCK_H) { if (blocktype == md4c.MD_BLOCK_H) {
just_write(state.writer, "\n"); just_write(state.writer, "\n");
const h_detail: *md4c.MD_BLOCK_H_DETAIL = @ptrCast(@alignCast(detail)); const h_detail: *md4c.MD_BLOCK_H_DETAIL = @ptrCast(@alignCast(detail));
var temp_style = state.last_style; var temp_style = state.last_style;
temp_style.font_style.dim = true; temp_style.font_style.dim = true;
temp_style.font_style.bold = true; temp_style.font_style.bold = true;
state.update_style(temp_style); state.update_style(temp_style);
for (0..h_detail.level) |_| { for (0..h_detail.level) |_| {
just_write(state.writer, "#"); just_write(state.writer, "#");
} }
temp_style.font_style.dim = false; temp_style.font_style.dim = false;
temp_style.font_style.bold = state.nested_bold_count > 0; temp_style.font_style.bold = state.nested_bold_count > 0;
state.update_style(temp_style); state.update_style(temp_style);
just_write(state.writer, " "); just_write(state.writer, " ");
} }
if (blocktype == md4c.MD_BLOCK_LI) { if (blocktype == md4c.MD_BLOCK_LI) {
const li_detail: *md4c.MD_BLOCK_LI_DETAIL = @ptrCast(@alignCast(detail)); const li_detail: *md4c.MD_BLOCK_LI_DETAIL = @ptrCast(@alignCast(detail));
var temp_style = state.last_style; var temp_style = state.last_style;
if (li_detail.is_task == 0) temp_style.font_style.dim = true; if (li_detail.is_task == 0) temp_style.font_style.dim = true;
temp_style.font_style.bold = true; temp_style.font_style.bold = true;
state.update_style(temp_style); state.update_style(temp_style);
if (li_detail.is_task == 0) { if (li_detail.is_task == 0) {
just_write(state.writer, ""); just_write(state.writer, "");
} else { } else {
@ -130,6 +133,7 @@ fn enter_block(blocktype: md4c.MD_BLOCKTYPE, detail: ?*anyopaque, userdata: ?*an
just_write(state.writer, ""); just_write(state.writer, "");
} }
} }
temp_style.font_style.dim = false; temp_style.font_style.dim = false;
temp_style.font_style.bold = state.nested_bold_count > 0; temp_style.font_style.bold = state.nested_bold_count > 0;
state.update_style(temp_style); state.update_style(temp_style);
@ -141,7 +145,6 @@ fn enter_block(blocktype: md4c.MD_BLOCKTYPE, detail: ?*anyopaque, userdata: ?*an
} }
fn leave_block(blocktype: md4c.MD_BLOCKTYPE, detail: ?*anyopaque, userdata: ?*anyopaque) callconv(.C) c_int { fn leave_block(blocktype: md4c.MD_BLOCKTYPE, detail: ?*anyopaque, userdata: ?*anyopaque) callconv(.C) c_int {
//std.debug.print("leave {d}\n", .{blocktype});
const state: *State = @ptrCast(@alignCast(userdata)); const state: *State = @ptrCast(@alignCast(userdata));
if (blocktype == md4c.MD_BLOCK_H) { if (blocktype == md4c.MD_BLOCK_H) {
@ -151,14 +154,15 @@ fn leave_block(blocktype: md4c.MD_BLOCKTYPE, detail: ?*anyopaque, userdata: ?*an
just_write(state.writer, "\n"); just_write(state.writer, "\n");
} }
if (blocktype == md4c.MD_BLOCK_HR) { if (blocktype == md4c.MD_BLOCK_HR) {
var winsize: std.c.winsize = undefined;
var temp_style = state.last_style; var temp_style = state.last_style;
temp_style.font_style.dim = true; temp_style.font_style.dim = true;
temp_style.font_style.bold = true; temp_style.font_style.bold = true;
state.update_style(temp_style); state.update_style(temp_style);
const term_name_c = cstdio.ctermid(null);
const term_name = std.mem.sliceTo(term_name_c, 0); const term_name = "/dev/tty";
const term_fd = std.posix.open(term_name, .{}, 0) catch errorexit(5, "failed to open terminal D:\n"); const term_fd = std.posix.open(term_name, .{}, 0) catch errorexit(5, "failed to open terminal D:\n");
var winsize: std.c.winsize = undefined;
if (std.c.ioctl(term_fd, std.c.T.IOCGWINSZ, &winsize) != -1) { if (std.c.ioctl(term_fd, std.c.T.IOCGWINSZ, &winsize) != -1) {
for (0..winsize.ws_col) |_| { for (0..winsize.ws_col) |_| {
just_write(state.writer, "-"); just_write(state.writer, "-");
@ -167,6 +171,7 @@ fn leave_block(blocktype: md4c.MD_BLOCKTYPE, detail: ?*anyopaque, userdata: ?*an
} else { } else {
just_write(state.writer, "---\n"); just_write(state.writer, "---\n");
} }
temp_style.font_style.dim = false; temp_style.font_style.dim = false;
temp_style.font_style.bold = state.nested_bold_count > 0; temp_style.font_style.bold = state.nested_bold_count > 0;
state.update_style(temp_style); state.update_style(temp_style);
@ -201,14 +206,16 @@ fn enter_span(spantype: md4c.MD_SPANTYPE, detail: ?*anyopaque, userdata: ?*anyop
temp_style.foreground = .Blue; temp_style.foreground = .Blue;
temp_style.font_style.underline = true; temp_style.font_style.underline = true;
state.update_style(temp_style); state.update_style(temp_style);
just_write(state.writer, "\x1b]8;;");
just_write(state.writer, "\x1b]8;;"); // OSC 8 link begin
const detail_a: *md4c.MD_SPAN_A_DETAIL = @ptrCast(@alignCast(detail)); const detail_a: *md4c.MD_SPAN_A_DETAIL = @ptrCast(@alignCast(detail));
const link_c = detail_a.href.text; const link_c = detail_a.href.text;
const link = link_c[0..detail_a.href.size]; const link = link_c[0..detail_a.href.size];
just_write(state.writer, link); just_write(state.writer, link);
just_write(state.writer, "\x07"); just_write(state.writer, "\x07"); // OSC 8 link seperate url and text
} }
// TODO emit ansi to the writer
return 0; // TODO what does the return value represent ???? return 0; // TODO what does the return value represent ????
} }
@ -232,13 +239,13 @@ fn leave_span(spantype: md4c.MD_SPANTYPE, detail: ?*anyopaque, userdata: ?*anyop
state.update_underline(); state.update_underline();
} }
if (spantype == md4c.MD_SPAN_A) { if (spantype == md4c.MD_SPAN_A) {
just_write(state.writer, "\x1b]8;;\x07"); just_write(state.writer, "\x1b]8;;\x07"); // OSC 8 link end
var temp_style = state.last_style; var temp_style = state.last_style;
temp_style.foreground = .Default; temp_style.foreground = .Default;
state.update_style(temp_style); state.update_style(temp_style);
state.update_underline(); state.update_underline();
} }
// TODO emit ansi to the writer
return 0; // TODO what does the return value represent ???? return 0; // TODO what does the return value represent ????
} }
@ -260,7 +267,9 @@ fn on_text(texttype: md4c.MD_TEXTTYPE, text: [*c]const md4c.MD_CHAR, size: md4c.
var temp_style = state.last_style; var temp_style = state.last_style;
temp_style.font_style.dim = true; temp_style.font_style.dim = true;
state.update_style(temp_style); state.update_style(temp_style);
just_write(state.writer, text_data); just_write(state.writer, text_data);
temp_style.font_style.dim = false; temp_style.font_style.dim = false;
state.update_style(temp_style); state.update_style(temp_style);
} }
@ -282,6 +291,7 @@ const parser: md4c.MD_PARSER = .{
}; };
fn errorexit(code: u8, comptime msg: []const u8) noreturn { fn errorexit(code: u8, comptime msg: []const u8) noreturn {
std.debug.print("mdcat exiting with error: ", .{});
std.debug.print(msg, .{}); std.debug.print(msg, .{});
std.process.exit(code); std.process.exit(code);
} }