summaryrefslogtreecommitdiff
path: root/src/cmd/echo.zig
blob: 16a41eb11f9a6d35c33b11d1179dc4e7941d9fcc (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
const std = @import("std");
const Allocator = std.mem.Allocator;

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";
        var writer = ctx.output_writer;
        try writer.write(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";
        var writer = ctx.output_writer;
        try writer.write(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);
        var writer = ctx.output_writer;
        try writer.write(output);
        return CommandStatus{ .Code = 0 };
    }
};