summaryrefslogtreecommitdiff
path: root/src/cmd/echo.zig
blob: 4f2731b8409e19fc99bd93c2342d54e6dbc8c959 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const std = @import("std");
const Allocator = std.mem.Allocator;
const print = std.debug.print;

const types = @import("./lib/types.zig");
const CommandStatus = types.CommandStatus;
const CommandContext = types.CommandContext;

pub const EchoOff = struct {
    pub fn eval(echo_off: EchoOff, ctx: CommandContext) !CommandStatus {
        _ = echo_off;
        _ = ctx;

        return CommandStatus{ .Code = 0 };
    }
};

pub const EchoOn = struct {
    pub fn eval(echo_on: EchoOn, ctx: CommandContext) !CommandStatus {
        _ = echo_on;

        const output = "ECHO is on\n";
        if (ctx.output_capture) |capture| {
            try capture.write(output);
        } else {
            print("{s}", .{output});
        }
        return CommandStatus{ .Code = 0 };
    }
};

pub const EchoPlain = struct {
    pub fn eval(echo_plain: EchoPlain, ctx: CommandContext) !CommandStatus {
        _ = echo_plain;

        const output = "ECHO is on\n";
        if (ctx.output_capture) |capture| {
            try capture.write(output);
        } else {
            print("{s}", .{output});
        }
        return CommandStatus{ .Code = 0 };
    }
};

pub const EchoText = struct {
    message: []const u8,

    pub fn eval(echo_text: EchoText, ctx: CommandContext) !CommandStatus {
        const output = try std.fmt.allocPrint(ctx.allocator, "{s}\n", .{echo_text.message});
        defer ctx.allocator.free(output);
        if (ctx.output_capture) |capture| {
            try capture.write(output);
        } else {
            print("{s}", .{output});
        }
        return CommandStatus{ .Code = 0 };
    }
};