flow/src/list_languages.zig

101 lines
3.7 KiB
Zig

const std = @import("std");
const syntax = @import("syntax");
const builtin = @import("builtin");
const bin_path = @import("bin_path.zig");
const checkmark_width = if (builtin.os.tag != .windows) 2 else 3;
const success_mark = if (builtin.os.tag != .windows) "" else "[y]";
const fail_mark = if (builtin.os.tag != .windows) "" else "[n]";
pub fn list(allocator: std.mem.Allocator, writer: anytype, tty_config: std.io.tty.Config) !void {
var max_language_len: usize = 0;
var max_langserver_len: usize = 0;
var max_formatter_len: usize = 0;
var max_extensions_len: usize = 0;
for (syntax.FileType.file_types) |file_type| {
max_language_len = @max(max_language_len, file_type.name.len);
max_langserver_len = @max(max_langserver_len, args_string_length(file_type.language_server));
max_formatter_len = @max(max_formatter_len, args_string_length(file_type.formatter));
max_extensions_len = @max(max_extensions_len, args_string_length(file_type.extensions));
}
try tty_config.setColor(writer, .yellow);
try write_string(writer, "Language", max_language_len + 1);
try write_string(writer, "Extensions", max_extensions_len + 1 + checkmark_width);
try write_string(writer, "Language Server", max_langserver_len + 1 + checkmark_width);
try write_string(writer, "Formatter", max_formatter_len);
try tty_config.setColor(writer, .reset);
try writer.writeAll("\n");
for (syntax.FileType.file_types) |file_type| {
try write_string(writer, file_type.name, max_language_len + 1);
try write_segmented(writer, file_type.extensions, ",", max_extensions_len + 1, tty_config);
if (file_type.language_server) |language_server|
try write_checkmark(writer, can_execute(allocator, language_server[0]), tty_config);
try write_segmented(writer, file_type.language_server, " ", max_langserver_len + 1, tty_config);
if (file_type.formatter) |formatter|
try write_checkmark(writer, can_execute(allocator, formatter[0]), tty_config);
try write_segmented(writer, file_type.formatter, " ", max_formatter_len, tty_config);
try writer.writeAll("\n");
}
}
fn args_string_length(args_: ?[]const []const u8) usize {
const args = args_ orelse return 0;
var len: usize = 0;
var first: bool = true;
for (args) |arg| {
if (first) first = false else len += 1;
len += arg.len;
}
return len;
}
fn write_string(writer: anytype, string: []const u8, pad: usize) !void {
try writer.writeAll(string);
try write_padding(writer, string.len, pad);
}
fn write_checkmark(writer: anytype, success: bool, tty_config: std.io.tty.Config) !void {
try tty_config.setColor(writer, if (success) .green else .red);
if (success) try writer.writeAll(success_mark) else try writer.writeAll(fail_mark);
}
fn write_segmented(
writer: anytype,
args_: ?[]const []const u8,
sep: []const u8,
pad: usize,
tty_config: std.io.tty.Config,
) !void {
const args = args_ orelse return;
var len: usize = 0;
var first: bool = true;
for (args) |arg| {
if (first) first = false else {
len += 1;
try writer.writeAll(sep);
}
len += arg.len;
try writer.writeAll(arg);
}
try tty_config.setColor(writer, .reset);
try write_padding(writer, len, pad);
}
fn write_padding(writer: anytype, len: usize, pad_len: usize) !void {
for (0..pad_len - len) |_| try writer.writeAll(" ");
}
fn can_execute(allocator: std.mem.Allocator, binary_name: []const u8) bool {
const resolved_binary_path = bin_path.find_binary_in_path(allocator, binary_name) catch return false;
defer if (resolved_binary_path) |path| allocator.free(path);
return resolved_binary_path != null;
}