add --lang and --theme options
This commit is contained in:
parent
0b0200aecc
commit
53b6895b02
4 changed files with 52 additions and 4 deletions
18
README.md
18
README.md
|
@ -3,7 +3,21 @@ zat is a syntax highlighting cat like utility.
|
|||
|
||||
It uses tree-sitter and supports for vscode themes.
|
||||
|
||||
Build with:
|
||||
Build with the provided zig wrapper:
|
||||
```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.
|
||||
|
|
|
@ -12,6 +12,13 @@ injections: ?[:0]const u8,
|
|||
first_line_matches: ?FirstLineMatch = null,
|
||||
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 {
|
||||
if (guess_first_line(content)) |ft| return ft;
|
||||
for (file_types) |*file_type|
|
||||
|
|
26
src/main.zig
26
src/main.zig
|
@ -7,10 +7,14 @@ const term = @import("ansi-term.zig");
|
|||
|
||||
const StyleCache = std.AutoHashMap(u32, ?Theme.Token);
|
||||
var style_cache: StyleCache = undefined;
|
||||
var lang_override: ?[]const u8 = null;
|
||||
|
||||
pub fn main() !void {
|
||||
const params = comptime clap.parseParamsComptime(
|
||||
\\-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.
|
||||
\\
|
||||
);
|
||||
|
@ -34,7 +38,17 @@ pub fn main() !void {
|
|||
if (res.args.help != 0)
|
||||
return clap.help(std.io.getStdErr().writer(), clap.Help, ¶ms, .{});
|
||||
|
||||
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();
|
||||
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 {
|
||||
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 {
|
||||
writer: @TypeOf(writer),
|
||||
|
@ -175,6 +192,11 @@ fn get_theme_by_name(name: []const u8) ?Theme {
|
|||
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 {
|
||||
const ansi_style = .{
|
||||
.foreground = if (style.fg) |color| to_rgb_color(color) else .Default,
|
||||
|
|
|
@ -33,6 +33,11 @@ pub fn create(file_type: *const FileType, a: std.mem.Allocator, content: []const
|
|||
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 {
|
||||
const file_type = FileType.guess(file_path, content) orelse return error.NotFound;
|
||||
return create(file_type, a, content);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue