summaryrefslogtreecommitdiff
path: root/src/cmd/echo.zig
blob: a11d74852bee9e15713a72d97af35cf0693ed681 (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
60
61
62
63
64
65
66
67
68
const std = @import("std");
const Allocator = std.mem.Allocator;
const print = std.debug.print;

const types = @import("./types.zig");
const CommandStatus = types.CommandStatus;
const OutputCapture = types.OutputCapture;
const InputSource = types.InputSource;

pub const EchoOff = struct {
    pub fn eval(echo_off: EchoOff, allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource) !CommandStatus {
        _ = echo_off;
        _ = allocator;
        _ = output_capture;
        _ = input_source;

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

pub const EchoOn = struct {
    pub fn eval(echo_on: EchoOn, allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource) !CommandStatus {
        _ = echo_on;
        _ = allocator;
        _ = input_source;

        const output = "ECHO is on\n";
        if (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, allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource) !CommandStatus {
        _ = echo_plain;
        _ = allocator;
        _ = input_source;

        const output = "ECHO is on\n";
        if (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, allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource) !CommandStatus {
        _ = input_source;

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