Skip to content

Instantly share code, notes, and snippets.

@crazymonkyyy
Created April 8, 2024 01:06
Show Gist options
  • Save crazymonkyyy/abca120ad67e409ae628fc2ecb33a2c6 to your computer and use it in GitHub Desktop.
Save crazymonkyyy/abca120ad67e409ae628fc2ecb33a2c6 to your computer and use it in GitHub Desktop.
/*
a one char clock for 256 color teminals
red-yellow: am
teal-blue: pm
☀: noon
☾: midnight
red/teal: early hour
yellow/blue: late hour
accepts command line arguments such as "15 30"
*/
import std;
char[] magiccolorstring(int i,bool whitetext){
char[] o="\033[48;5;999m\033[30m".dup;
enum offset="\033[48;5;".length;
o[offset+2]=((i/1 )%10)+'0';
o[offset+1]=((i/10 )%10)+'0';
o[offset+0]=((i/100)%10)+'0';
enum offset2="\033[48;5;999m\033[3".length;
if(whitetext){
o[offset2]='7';
}
return o;
}
enum string resetcolor="\033[0m";
void colorprint(string s,int color,bool whitetext){
magiccolorstring(color,whitetext).write;
s.write;
resetcolor.write;
}
unittest{
colorprint("hello",21,true);
colorprint("hello",51,false);
}
alias now=Clock.currTime;
string[13] glyph=["1","2","3","4","5","6","7","8","9","X","ꔔ","☾","︎☀"];
int toglyph(int i){
switch(i){
case 0:return 11;
case 12:return 12;
default: return i%12-1;
}}
unittest{
writeln(now.hour.to!int,":",now.minute);
glyph[now.hour.toglyph].writeln;
}
//note d 2d arrays are backwards
int[6][2] colors=[
[51,45,39,33,27,21],//pm
[196,202,208,214,220,226]
];
bool isnight(int hour)=>(hour<13)^(hour%12==0);
void main(string[] args){
int min;
int hour;
if(args.length<3){
min=now.minute;
hour=now.hour;
} else {
hour=args[1].to!int;
min=args[2].to!int;
}
min/=10;
assert(min<6);
assert(hour<24);
glyph[hour.toglyph].//hour glyph
colorprint(colors[hour.isnight][min],//colorscheme
!(min<3)^(hour.isnight));//whitetext
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment