-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b0d4e1b
Showing
6 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
zig-cache | ||
main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Marc Tiehuis | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
A library for printing Ansi escape codes. | ||
|
||
NOTE: Not fully-functional. Determining if how we can get the best ergonomics. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
const std = @import("std"); | ||
const assert = debug.assert; | ||
|
||
fn escape(comptime literal: []const u8) -> []const u8 { | ||
"\x1b[" ++ literal | ||
} | ||
|
||
pub const ResetAll = escape("c"); | ||
|
||
pub const Color = enum { | ||
Simple: u8, | ||
Ansi: u8, | ||
TrueColor: struct { r: u8, g: u8, b: u8 }, | ||
}; | ||
|
||
pub const Black = Color.Simple { 30 }; | ||
pub const Red = Color.Simple { 31 }; | ||
pub const Green = Color.Simple { 32 }; | ||
pub const Yellow = Color.Simple { 33 }; | ||
pub const Blue = Color.Simple { 34 }; | ||
pub const Magenta = Color.Simple { 35 }; | ||
pub const Cyan = Color.Simple { 36 }; | ||
pub const LightGray = Color.Simple { 37 }; | ||
pub const Default = Color.Simple { 39 }; | ||
pub const DarkGray = Color.Simple { 90 }; | ||
pub const LightRed = Color.Simple { 91 }; | ||
pub const LightGreen = Color.Simple { 92 }; | ||
pub const LightYellow = Color.Simple { 93 }; | ||
pub const LightBlue = Color.Simple { 94 }; | ||
pub const LightMagenta = Color.Simple { 95 }; | ||
pub const LightCyan = Color.Simple { 96 }; | ||
pub const White = Color.Simple { 97 }; | ||
|
||
pub fn Color216(r: u8, g: u8, b: u8) -> Color { | ||
assert(r <= 5 and g <= 5 and b <= 5); | ||
Color.Ansi { 16 + 36 * r + 6 * g + b } | ||
} | ||
|
||
pub fn GrayScale24(shade: u8) -> Color { | ||
assert(shade < 24); | ||
Color.Ansi { 0xe8 + shade } | ||
} | ||
|
||
pub fn Color256(code: u8) -> Color { | ||
Color.Ansi { code } | ||
} | ||
|
||
pub fn TrueColor(r: u8, g: u8, b: u8) -> Color { | ||
Color.TrueColor { .r = r, .g = g, .b = b } | ||
} | ||
|
||
// If we had a @stringify operator we could do most everything we need to. | ||
// The only slightly annoying thing would runtime Color's but we could utilize buffers and | ||
// the like somehow. This case less common in my mind. | ||
// | ||
// var buffer: [3]u8 = undefined; | ||
fn u8ToLit(comptime x: u8) -> []const u8 { | ||
// if (x < 10) { | ||
// buffer[0] = x + '0'; | ||
// buffer[0..1] | ||
// } else if (x < 100) { | ||
// buffer[0] = (x % 10) + '0'; | ||
// buffer[1] = (x / 10) + '0'; | ||
// buffer[0..2] | ||
// } else { | ||
// buffer[0] = (x % 10) + '0'; | ||
// buffer[1] = ((x / 10) % 10) + '0'; | ||
// buffer[2] = (x / 100) + '0'; | ||
// buffer[0..3] | ||
// } | ||
|
||
"_" | ||
} | ||
|
||
pub fn Fg(comptime color: Color) -> []const u8 { | ||
comptime switch (color) { | ||
Color.Simple => |v| escape(u8ToLit(v) ++ "m"), | ||
Color.Ansi => |v| escape("38;5;" ++ u8ToLit(v) ++ "m"), | ||
Color.TrueColor => |v| escape("38;2;" ++ u8ToLit(v.r) ++ ";" ++ u8ToLit(v.g) ++ | ||
";" ++ u8ToLit(v.b) ++ "m"), | ||
} | ||
} | ||
|
||
pub fn Bg(comptime color: Color) -> []const u8 { | ||
comptime switch (color) { | ||
Color.Simple => |v| escape(u8ToLit(v + 10) ++ "m"), | ||
Color.Ansi => |v| escape("48;5;" ++ u8ToLit(v) ++ "m"), | ||
Color.TrueColor => |v| escape("48;2;" ++ u8ToLit(v.r) ++ ";" ++ u8ToLit(v.g) ++ | ||
";" ++ u8ToLit(v.b) ++ "m"), | ||
} | ||
} | ||
|
||
pub const Attr = struct { | ||
pub const Reset = escape("m"); | ||
pub const Bright = escape("1m"); | ||
pub const Dim = escape("2m"); | ||
pub const Italic = escape("3m"); | ||
pub const Underline = escape("4m"); | ||
pub const Blink = escape("5m"); | ||
pub const Invert = escape("7m"); | ||
pub const Crossed = escape("9m"); | ||
pub const NoBright = escape("21m"); | ||
pub const NoDim = escape("22m"); | ||
pub const NoItalic = escape("23m"); | ||
pub const NoUnderline = escape("24m"); | ||
pub const NoBlink = escape("25m"); | ||
pub const NoInvert = escape("27m"); | ||
pub const NoCrossed = escape("29m"); | ||
pub const Framed = escape("51m"); | ||
}; | ||
|
||
pub const Erase = struct { | ||
pub const Down = escape("J"); | ||
pub const Up = escape("1J"); | ||
pub const Screen = escape("2J"); | ||
pub const EndOfLine = escape("K"); | ||
pub const StartOfLine = escape("1K"); | ||
pub const Line = escape("2K"); | ||
}; | ||
|
||
pub const Cursor = struct { | ||
pub const Hide = escape("?25hl"); | ||
pub const Show = escape("?25h"); | ||
pub const Save = escape("s"); | ||
pub const Restore = escape("u"); | ||
|
||
// The following are not-compile-time known and should be used via a standard println. | ||
// | ||
// This requires a buffer or a separate call (not using printf). | ||
pub fn Goto(x: u8, y: u8) -> []const u8 { | ||
assert(x != 0 and y != 0); | ||
escape("{};{}H", x, y) | ||
} | ||
|
||
pub fn Up(amount: u8) -> []const u8 { | ||
escape("{}A", amount); | ||
} | ||
|
||
pub fn Down(amount: u8) -> []const u8 { | ||
escape("{}B", amount) | ||
} | ||
|
||
pub fn Right(amount: u8) -> []const u8 { | ||
escape("{}C", amount) | ||
} | ||
|
||
pub fn Left(amount: u8) -> []const u8 { | ||
escape("{}D", amount) | ||
} | ||
}; | ||
|
||
pub const Screen = struct { | ||
pub const Save = escape("?47h"); | ||
pub const Restore = escape("?47l"); | ||
}; | ||
|
||
// Add a closest match function to the standard 256 color mappings. | ||
// | ||
// Allow specifying either the 16-color standard, 216-color, or True-Color as the | ||
// parameter space output. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const Builder = @import("std").build.Builder; | ||
|
||
pub fn build(b: &Builder) { | ||
const mode = b.standardReleaseOptions(); | ||
const exe = b.addExecutable("main", "main.zig"); | ||
exe.setBuildMode(mode); | ||
|
||
exe.setOutputPath("./main"); | ||
b.default_step.dependOn(&exe.step); | ||
b.installArtifact(exe); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const std = @import("std"); | ||
const printf = std.io.stdout.printf; | ||
|
||
use @import("ansiz.zig"); | ||
|
||
pub fn main() -> %void { | ||
// Zig should be able to determine that Bg(Green) is fully comptime knowable without the | ||
// explicit indication. | ||
This comment has been minimized.
Sorry, something went wrong. |
||
%%printf(comptime Bg(Green) ++ "Hello, World!\n"); | ||
} |
I believe this is ziglang/zig#425