add --lang and --theme options

This commit is contained in:
CJ van den Berg 2024-02-20 18:58:49 +01:00
parent 0b0200aecc
commit 53b6895b02
4 changed files with 52 additions and 4 deletions

View file

@ -3,7 +3,21 @@ zat is a syntax highlighting cat like utility.
It uses tree-sitter and supports for vscode themes. It uses tree-sitter and supports for vscode themes.
Build with: Build with the provided zig wrapper:
```shell ```shell
./zig build ./zig build -Doptimize=ReleaseFast
``` ```
The zig wrapper just fetches a known good version of zig nightly and places it in the .cache directory. Or use your own version of zig.
Run with:
```shell
zig-out/bin/zat
```
Place it in your path for convinient access. You might want to strip it first.
Supply files to highlight on the command line. Multiple files will be appended like with cat. If no files are on the command line zat will read from stdin. Override the language with --lang and select a different theme with --theme.
See --help for full command line.

View file

@ -12,6 +12,13 @@ injections: ?[:0]const u8,
first_line_matches: ?FirstLineMatch = null, first_line_matches: ?FirstLineMatch = null,
comment: []const u8, comment: []const u8,
pub fn get_by_name(name: []const u8) ?*const FileType {
for (file_types) |*file_type|
if (std.mem.eql(u8, file_type.name, name))
return file_type;
return null;
}
pub fn guess(file_path: ?[]const u8, content: []const u8) ?*const FileType { pub fn guess(file_path: ?[]const u8, content: []const u8) ?*const FileType {
if (guess_first_line(content)) |ft| return ft; if (guess_first_line(content)) |ft| return ft;
for (file_types) |*file_type| for (file_types) |*file_type|

View file

@ -7,10 +7,14 @@ const term = @import("ansi-term.zig");
const StyleCache = std.AutoHashMap(u32, ?Theme.Token); const StyleCache = std.AutoHashMap(u32, ?Theme.Token);
var style_cache: StyleCache = undefined; var style_cache: StyleCache = undefined;
var lang_override: ?[]const u8 = null;
pub fn main() !void { pub fn main() !void {
const params = comptime clap.parseParamsComptime( const params = comptime clap.parseParamsComptime(
\\-h, --help Display this help and exit. \\-h, --help Display this help and exit.
\\-l, --lang <str> Override the language.
\\-t, --theme <str> Select theme to use.
\\--list-themes Show available themes.
\\<str>... File to open. \\<str>... File to open.
\\ \\
); );
@ -34,7 +38,17 @@ pub fn main() !void {
if (res.args.help != 0) if (res.args.help != 0)
return clap.help(std.io.getStdErr().writer(), clap.Help, &params, .{}); return clap.help(std.io.getStdErr().writer(), clap.Help, &params, .{});
const theme = get_theme_by_name("default") orelse unreachable; if (res.args.@"list-themes" != 0)
return list_themes(std.io.getStdOut().writer());
const theme_name = if (res.args.theme) |theme| theme else "default";
const theme = get_theme_by_name(theme_name) orelse {
try std.io.getStdErr().writer().print("theme \"{s}\" not found\n", .{theme_name});
std.os.exit(1);
};
lang_override = res.args.lang;
const stdout_file = std.io.getStdOut().writer(); const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file); var bw = std.io.bufferedWriter(stdout_file);
@ -58,7 +72,10 @@ pub fn main() !void {
} }
fn render_file(a: std.mem.Allocator, writer: anytype, content: []const u8, file_path: []const u8, theme: *const Theme) !void { fn render_file(a: std.mem.Allocator, writer: anytype, content: []const u8, file_path: []const u8, theme: *const Theme) !void {
const parser = try syntax.create_guess_file_type(a, content, file_path); const parser = if (lang_override) |name|
try syntax.create_file_type(a, content, name)
else
try syntax.create_guess_file_type(a, content, file_path);
const Ctx = struct { const Ctx = struct {
writer: @TypeOf(writer), writer: @TypeOf(writer),
@ -175,6 +192,11 @@ fn get_theme_by_name(name: []const u8) ?Theme {
return null; return null;
} }
fn list_themes(writer: anytype) !void {
for (themes.themes) |theme|
try writer.print("{s}\t{s}\n", .{ theme.name, theme.description });
}
fn set_ansi_style(writer: anytype, style: Theme.Style) !void { fn set_ansi_style(writer: anytype, style: Theme.Style) !void {
const ansi_style = .{ const ansi_style = .{
.foreground = if (style.fg) |color| to_rgb_color(color) else .Default, .foreground = if (style.fg) |color| to_rgb_color(color) else .Default,

View file

@ -33,6 +33,11 @@ pub fn create(file_type: *const FileType, a: std.mem.Allocator, content: []const
return self; return self;
} }
pub fn create_file_type(a: std.mem.Allocator, content: []const u8, lang_name: []const u8) !*Self {
const file_type = FileType.get_by_name(lang_name) orelse return error.NotFound;
return create(file_type, a, content);
}
pub fn create_guess_file_type(a: std.mem.Allocator, content: []const u8, file_path: ?[]const u8) !*Self { pub fn create_guess_file_type(a: std.mem.Allocator, content: []const u8, file_path: ?[]const u8) !*Self {
const file_type = FileType.guess(file_path, content) orelse return error.NotFound; const file_type = FileType.guess(file_path, content) orelse return error.NotFound;
return create(file_type, a, content); return create(file_type, a, content);