Skip to content

Commit

Permalink
Fixed swizzle and added more bash formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
al1-ce committed Dec 1, 2022
1 parent f252d61 commit 2df9bea
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 39 deletions.
113 changes: 88 additions & 25 deletions core/sily/bashfmt.d
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module sily.bashfmt;

import std.conv: to;
import std.conv : to;
import std.stdio : write, writef;


alias FG = Foreground;
alias BG = Background;
Expand Down Expand Up @@ -78,29 +80,90 @@ enum FormattingReset : string {
oline = "\033[55m"
}

private string[Formatting] formattingResetArray;

static this() {
formattingResetArray = [
Formatting.bold: "\033[21m",
Formatting.dim: "\033[22m",
Formatting.italics: "\033[22m",
Formatting.uline: "\033[24m",
Formatting.blink: "\033[25m",
Formatting.inverse: "\033[27m",
Formatting.hidden: "\033[28m",
Formatting.striked: "\033[29m",
Formatting.dline: "\033[24m",
Formatting.cline: "\033[4:0m",
Formatting.oline: "\033[55m"
];
}
string fmt(string text, FG col) {
return col ~ text ~ FG.reset;
}
string fmt(string text, BG col) {
return col ~ text ~ BG.reset;
}
string fmt(string text, FM style) {
return style ~ text ~ formattingResetArray[style];
version(windows) {
import core.stdc.stdlib: exit;
exit(2);
}
}

void fwrite(A...)(A args) {
foreach (arg; args) {
write(cast(string) arg);
}
}

void fwriteln(A...)(A args) {
foreach (arg; args) {
write(cast(string) arg);
}
write("\n");
}

void eraseLines(int num) {
eraseCurrentLine();
--num;

while (num) {
moveCursorUp(1);
eraseCurrentLine();
--num;
}
}

void moveCursorTo(int x, int y) {
writef("\033[%d;%df", x, y);
}

void moveCursorUp(int lineAmount = 1) {
writef("\033[%dA", lineAmount);
}

void moveCursorDown(int lineAmount = 1) {
writef("\033[%dB", lineAmount);
}

void moveCursorRight(int columnAmount = 1) {
writef("\033[%dC", columnAmount);
}

void moveCursorLeft(int columnAmount = 1) {
writef("\033[%dD", columnAmount);
}

void clearScreen() {
write("\033[2J");
moveCursorTo(0, 0);
}

void clearScreenOnly() {
write("\033[2J");
}

void eraseCurrentLine() {
write("\033[2K");
}

void eraseLineLeft() {
write("\033[1K");
}

void eraseLineRight() {
write("\033[K");
}

void saveCursorPosition() {
write("\033[s");
}

void restoreCursorPosition() {
write("\033[u");
}

void hideCursor() {
write("\033[?25l");
}

void showCursor() {
write("\033[?25h");
}
2 changes: 1 addition & 1 deletion core/sily/color.d
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ struct Color {
// idk how it works but it works awesome
// and im not going to touch it at all
enum AccessString = "r g b a";
mixin accessByString!(4, float, "data", AccessString);
mixin accessByString!(float, 4, "data", AccessString);


public Vector4f asVector4f() {
Expand Down
18 changes: 15 additions & 3 deletions core/sily/meta.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,20 @@ bool isValidOp(string op, T, R, E = T)() pure {
/* WHAT IS GOING ON HERE */
/* -------------------------------------------------------------------------- */


mixin template accessByString( size_t N, T, string data, string AS, string VVASES=" ", string VVASVS="|")

// used as:
//
// static if (N == 2 || N == 3 || N == 4) {
// static if (N == 2) enum AccessString = "x y|w h|u v";
// else
// static if (N == 3) enum AccessString = "x y z|w h d|u v t|r g b";
// else
// static if (N == 4) enum AccessString = "x y z w|r g b a";

// mixin accessByString!(N, T, "data", AccessString);
// }

mixin template accessByString( T, size_t N, string data, string AS, string VVASES=" ", string VVASVS="|")
if( isCompatibleArrayAccessStrings(N,AS,VVASES,VVASVS) ) {
pure @property {
T opDispatch(string v)() const if( getIndex(AS,v,VVASES,VVASVS) != -1 ) {
Expand All @@ -42,7 +54,7 @@ mixin template accessByString( size_t N, T, string data, string AS, string VVASE
return res.join(",");
}

mixin( `return Vector!(v.length,T)(` ~ gen() ~ `);` );
mixin( `return Vector!(T, v.length)(` ~ gen() ~ `);` );
}

auto opDispatch(string v,U)( in U b )
Expand Down
105 changes: 101 additions & 4 deletions core/sily/vector.d
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,115 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) {
T* ptr() {
return data.ptr;
}

// /**
// * Symbolic element access
// */
// private static string elements(string[4] letters) @property do {
// string res;
// foreach (i; 0..size) {
// res ~= "T " ~ letters[i] ~ "; ";
// }
// return res;
// }

// /**
// * Vector components
// */
// union {
// // Elements as static array
// T[size] arrayof;

// static if (size < 5) {
// struct { mixin(elements(["x", "y", "z", "w"])); }
// struct { mixin(elements(["r", "g", "b", "a"])); }
// struct { mixin(elements(["s", "t", "p", "q"])); }
// }

// // static if (size == 2) {
// // struct { mixin(elements(["x", "y"])); }
// // struct { mixin(elements(["w", "h"])); }
// // struct { mixin(elements(["u", "v"])); }
// // } else
// // static if (size == 3) {
// // struct { mixin(elements(["x", "y", "z"])); }
// // struct { mixin(elements(["w", "h", "d"])); }
// // struct { mixin(elements(["u", "v", "t"])); }
// // struct { mixin(elements(["r", "g", "b"])); }
// // } else
// // static if (size == 4) {
// // struct { mixin(elements(["x", "y", "z", "w"])); }
// // struct { mixin(elements(["r", "g", "b", "a"])); }
// // }
// }

// incredible magic from sily.meta
// idk how it works but it works awesome
// and im not going to touch it at all
// template opDispatch(string s) if (valid(s)) {
// static if (s.length <= 4) {
// private static auto extend(string s) {
// while (s.length < 4) s ~= s[$-1];
// return s;
// }

// @property auto ref opDispatch(this X)() {
// enum p = extend(s);
// enum i = (char c) => ['x':0, 'y':1, 'z':2, 'w':3,
// 'r':0, 'g':1, 'b':2, 'a':3,
// 'u':0, 'v':1][c];
// enum i0 = i(p[0]),
// i1 = i(p[1]),
// i2 = i(p[2]),
// i3 = i(p[3]);

// static if (s.length == 4)
// return Vector!(T,4)(arrayof[i0], arrayof[i1], arrayof[i2], arrayof[i3]);
// else static if (s.length == 3)
// return Vector!(T,3)(arrayof[i0], arrayof[i1], arrayof[i2]);
// else static if (s.length == 2)
// return Vector!(T,2)(arrayof[i0], arrayof[i1]);
// }

// @property void opDispatch(this X, T2, alias n)(Vector!(T2, n) vec) if (s.length == n) {
// enum p = extend(s);
// enum i = (char c) => ['x':0, 'y':1, 'z':2, 'w':3,
// 'r':0, 'g':1, 'b':2, 'a':3,
// 's':0, 't':1, 'p':2, 'q':3][c];
// enum i0 = i(p[0]),
// i1 = i(p[1]),
// i2 = i(p[2]),
// i3 = i(p[3]);

// static if (s.length == 4) arrayof[i3] = vec.arrayof[3];
// static if (s.length >= 3) arrayof[i2] = vec.arrayof[2];
// static if (s.length >= 2) {
// arrayof[i1] = vec.arrayof[1];
// arrayof[i0] = vec.arrayof[0];
// }
// }
// }
// }

// private static bool valid(string s) {
// if (s.length < 2) return false;
// foreach(c; s) {
// switch(c) {
// case 'w', 'a', 'q': if (size < 4) return false; else break;
// case 'z', 'b', 'p': if (size < 3) return false; else break;
// case 'y', 'g', 't': if (size < 2) return false; else break;
// case 'x', 'r', 's': if (size < 1) return false; else break;
// default: return false;
// }
// }
// return true;
// }

static if (N == 2 || N == 3 || N == 4) {
static if (N == 2) enum AccessString = "x y|w h|u v";
else
static if (N == 3) enum AccessString = "x y z|w h d|u v t|r g b";
else
static if (N == 4) enum AccessString = "x y z w|r g b a";

mixin accessByString!(N, T, "data", AccessString);
mixin accessByString!(T, N, "data", AccessString);
}

/* -------------------------------------------------------------------------- */
Expand Down
25 changes: 19 additions & 6 deletions colortest → tests/colortest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env dub
/+ dub.sdl:
name "colortest"
dependency "sily" version="~>1.2.0"
dependency "sily" version="~master"
+/

import std.stdio;
Expand Down Expand Up @@ -50,9 +50,22 @@ void main() {
// write(col.getFromAnsi8(i).toString());
}
writeln("" ~ FG.reset ~ BG.reset);
writeln(
fmt(fmt(fmt(fmt("My cool text", FM.blink),
FG.ltgreen) ~ " with ",
BG.dkgray) ~ "color",
FM.dline) ~ " and style");
// writeln(
// fmt(fmt(fmt(fmt("My cool text", FM.blink),
// FG.ltgreen) ~ " with ",
// BG.dkgray) ~ "color",
// FM.dline) ~ " and style");
fwriteln(
FM.blink,
FM.dline,
FG.ltgreen,
BG.dkgray,
"My cool text",
FR.blink,
FG.reset,
" with ",
BG.reset,
"color",
FR.reset,
" and style");
}
31 changes: 31 additions & 0 deletions tests/testfmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env dub
/+ dub.sdl:
name "testfmt"
dependency "sily" version="~master"
+/

import std.stdio: writeln, write, readln;
import sily.bashfmt;

void main() {
writeln("After read screen will be cleared");
write("Press enter: ");
readln();
clearScreen();
writeln("You'll see this line");
writeln("You should not see this line");
eraseLines(2);
writeln("And this one");
writeln("And these two lines");
writeln("Too");
eraseLines(3);
writeln("And this one too");
fwriteln(FG.red, "stuff", FM.blink, "was", BG.cyan, "here", FR.fullreset);
saveCursorPosition();
moveCursorUp(3);
moveCursorDown();
moveCursorRight(22);
moveCursorLeft(2);
fwrite(BG.ltblue, "this one is written out of sequence", FR.fullreset);
restoreCursorPosition();
}
19 changes: 19 additions & 0 deletions tests/vectortest
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env dub
/+ dub.sdl:
name "vectortest"
dependency "sily" version="~master"
+/

import std.stdio: writeln;
import sily.color;
import sily.vector;


void main() {
col c = col(3, 4, 1, 2);
col d = c.brga;
writeln(d.toString());
vec4 v = d.asVector4f();
vec4 e = v.xyyz;
writeln(e.toString());
}

0 comments on commit 2df9bea

Please sign in to comment.