ansi-term.zig (3283B)
1 // MIT License 2 // 3 // Copyright (c) 2019 joachimschmidt557 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 // 23 // Repo: https://github.com/ziglibs/ansi-term 24 25 const std = @import("std"); 26 27 pub const RGB = struct { 28 r: u8, 29 g: u8, 30 b: u8, 31 }; 32 33 pub const Colour = union(enum) { 34 Default, 35 Black, 36 White, 37 Red, 38 Green, 39 Blue, 40 Yellow, 41 Magenta, 42 Cyan, 43 Fixed: u8, 44 Grey: u8, 45 RGB: RGB, 46 }; 47 48 const esc = "\x1B"; 49 const csi = esc ++ "["; 50 const reset = csi ++ "0m"; 51 52 pub fn clearCurrentLine(writer: anytype) !void { 53 try writer.writeAll(csi ++ "2K"); 54 } 55 56 pub fn hideCursor(writer: anytype) !void { 57 try writer.writeAll(csi ++ "?25l"); 58 } 59 60 pub fn showCursor(writer: anytype) !void { 61 try writer.writeAll(csi ++ "?25h"); 62 } 63 64 pub fn setCursorColumn(writer: anytype, column: usize) !void { 65 try writer.print(csi ++ "{d}G", .{column}); 66 } 67 68 pub fn setCursorPos(writer: anytype, row: usize, column: usize) !void { 69 try writer.print(csi ++ "{d};{d}H", .{ row, column }); 70 } 71 72 pub fn cursorUp(writer: anytype, rows: usize) !void { 73 if (rows > 0) { 74 try writer.print(csi ++ "{d}A", .{rows}); 75 } 76 } 77 78 pub fn cursorDown(writer: anytype, rows: usize) !void { 79 if (rows > 0) { 80 try writer.print(csi ++ "{d}B", .{rows}); 81 } 82 } 83 84 pub fn cursorForward(writer: anytype, columns: usize) !void { 85 try writer.print(csi ++ "{d}C", .{columns}); 86 } 87 88 pub fn writeColour(writer: anytype, colour: Colour) !void { 89 try writer.writeAll(csi); 90 switch (colour) { 91 .Default => try writer.writeAll("39"), 92 .Black => try writer.writeAll("30"), 93 .Red => try writer.writeAll("31"), 94 .Green => try writer.writeAll("32"), 95 .Yellow => try writer.writeAll("33"), 96 .Blue => try writer.writeAll("34"), 97 .Magenta => try writer.writeAll("35"), 98 .Cyan => try writer.writeAll("36"), 99 .White => try writer.writeAll("37"), 100 .Fixed => |fixed| try writer.print("48;5;{}", .{fixed}), 101 .Grey => |grey| try writer.print("48;2;{};{};{}", .{ grey, grey, grey }), 102 .RGB => |rgb| try writer.print("38;2;{};{};{}", .{ rgb.r, rgb.g, rgb.b }), 103 } 104 try writer.writeAll("m"); 105 } 106 107 pub fn resetColour(writer: anytype) !void { 108 try writer.writeAll(reset); 109 }