Skip to content

Commit

Permalink
feat: colorize output (#63)
Browse files Browse the repository at this point in the history
* feat: add colorized output

* chore: use stdout instead of std.debug.print

* chore: add runtime styles api
  • Loading branch information
hendriknielaender authored Dec 3, 2024
1 parent 4f1044d commit 96d97c3
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 75 deletions.
8 changes: 4 additions & 4 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const builtin = @import("builtin");
const Build = std.Build;

const min_zig_string = "0.13.0";
// Semantic version of your application
const version = std.SemanticVersion{ .major = 0, .minor = 5, .patch = 0 };
const semver = std.SemanticVersion{ .major = 0, .minor = 5, .patch = 0 };
const semver_string = "0.5.0";

const CrossTargetInfo = struct {
crossTarget: std.zig.CrossTarget,
Expand All @@ -32,14 +32,14 @@ pub fn build(b: *Build) void {
// Add a global option for versioning
const options = b.addOptions();
options.addOption(std.log.Level, "log_level", b.option(std.log.Level, "log_level", "The Log Level to be used.") orelse .info);
options.addOption(std.SemanticVersion, "zvm_version", version);
options.addOption([]const u8, "version", semver_string);

const exe = b.addExecutable(.{
.name = "zvm",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.version = version,
.version = semver,
});

const exe_options_module = options.createModule();
Expand Down
33 changes: 20 additions & 13 deletions src/alias.zig
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
//! This file is used to create soft links and verify version
//! for Windows, we will use copy dir(when Windows create soft link it requires admin)
//! for set version
//! for Windows, we will use copy dir (creating symlinks requires admin privileges)
//! for setting versions.
const std = @import("std");
const assert = std.debug.assert;
const builtin = @import("builtin");
const config = @import("config.zig");
const util_data = @import("util/data.zig");
const util_tool = @import("util/tool.zig");

/// try to set zig version
/// this will use system link on unix-like
/// for windows, this will use copy dir
/// Try to set the Zig version.
/// This will use a symlink on Unix-like systems.
/// For Windows, this will copy the directory.
pub fn set_version(version: []const u8, is_zls: bool) !void {
var arena = std.heap.ArenaAllocator.init(util_data.get_allocator());
defer arena.deinit();
Expand All @@ -33,7 +33,8 @@ pub fn set_version(version: []const u8, is_zls: bool) !void {
if (err != error.FileNotFound)
return err;

std.debug.print("zig version {s} is not installed. Please install it before proceeding.\n", .{version});
const err_msg = "{s} version {s} is not installed. Please install it before proceeding.\n";
try std.io.getStdErr().writer().print(err_msg, .{ if (is_zls) "zls" else "Zig", version });
std.process.exit(1);
};

Expand Down Expand Up @@ -67,32 +68,38 @@ fn update_current(zig_path: []const u8, symlink_path: []const u8) !void {
try std.posix.symlink(zig_path, symlink_path);
}

/// Verify current zig version
/// Verify the current Zig version
fn verify_zig_version(expected_version: []const u8) !void {
const allocator = util_data.get_allocator();

const actual_version = try util_data.get_current_version(allocator, false);
defer allocator.free(actual_version);

var stdout = std.io.getStdOut().writer();

if (std.mem.eql(u8, expected_version, "master")) {
std.debug.print("Now using Zig version {s}\n", .{actual_version});
try stdout.print("Now using Zig version {s}\n", .{actual_version});
} else if (!std.mem.eql(u8, expected_version, actual_version)) {
std.debug.print("Expected Zig version {s}, but currently using {s}. Please check.\n", .{ expected_version, actual_version });
const err_msg = "Expected Zig version {s}, but currently using {s}. Please check.\n";
try std.io.getStdErr().writer().print(err_msg, .{ expected_version, actual_version });
} else {
std.debug.print("Now using Zig version {s}\n", .{expected_version});
try stdout.print("Now using Zig version {s}\n", .{expected_version});
}
}

/// verify current zig version
/// Verify the current zls version
fn verify_zls_version(expected_version: []const u8) !void {
const allocator = util_data.get_allocator();

const actual_version = try util_data.get_current_version(allocator, true);
defer allocator.free(actual_version);

var stdout = std.io.getStdOut().writer();

if (!std.mem.eql(u8, expected_version, actual_version)) {
std.debug.print("Expected zls version {s}, but currently using {s}. Please check.\n", .{ expected_version, actual_version });
const err_msg = "Expected zls version {s}, but currently using {s}. Please check.\n";
try std.io.getStdErr().writer().print(err_msg, .{ expected_version, actual_version });
} else {
std.debug.print("Now using zls version {s}\n", .{expected_version});
try stdout.print("Now using zls version {s}\n", .{expected_version});
}
}
Loading

0 comments on commit 96d97c3

Please sign in to comment.