Skip to content

Instantly share code, notes, and snippets.

@Jarred-Sumner
Created June 21, 2021 20:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jarred-Sumner/a1de1561cfe948b16aa92bccbfb2daf3 to your computer and use it in GitHub Desktop.
Save Jarred-Sumner/a1de1561cfe948b16aa92bccbfb2daf3 to your computer and use it in GitHub Desktop.
const std = @import("std");
// usage:
// ./file-path:0 10
// 1 2 3
// 1. file path
// 2. Byte offset in file
// 3. ms update interval
pub fn main() anyerror!void {
var allocator = std.heap.c_allocator;
var timer = try std.time.Timer.start();
var color_buf: [2048]u8 = undefined;
var args = std.mem.span(try std.process.argsAlloc(allocator));
var basepath_with_colon: []u8 = args[args.len - 2];
var basepath: []u8 = "";
var position_str: []u8 = "";
if (std.mem.lastIndexOfScalar(u8, basepath_with_colon, ':')) |colon| {
basepath = basepath_with_colon[0..colon];
position_str = basepath_with_colon[colon + 1 ..];
}
var position = try std.fmt.parseInt(u32, position_str, 10);
const filepath = try std.fs.path.resolve(allocator, &.{basepath});
var file = try std.fs.openFileAbsolute(filepath, .{ .write = true });
var ms = @truncate(u64, (try std.fmt.parseInt(u128, args[args.len - 1], 10)) * std.time.ns_per_ms);
std.debug.assert(ms > 0);
// std.debug.assert(std.math.isFinite(position));
var prng = std.rand.DefaultPrng.init(0);
var stdout = std.io.getStdOut();
var log = stdout.writer();
var colors = std.mem.zeroes([4][3]u32);
var progress_bar: f64 = 0.0;
var destination_count: f64 = 18.0;
// Randomize initial colors
colors[0][0] = prng.random.int(u32);
colors[0][1] = prng.random.int(u32);
colors[0][2] = prng.random.int(u32);
colors[1][0] = prng.random.int(u32);
colors[1][1] = prng.random.int(u32);
colors[1][2] = prng.random.int(u32);
colors[2][0] = prng.random.int(u32);
colors[2][1] = prng.random.int(u32);
colors[2][2] = prng.random.int(u32);
colors[3][0] = prng.random.int(u32);
colors[3][1] = prng.random.int(u32);
colors[3][2] = prng.random.int(u32);
var rotate: u32 = 0;
var counter: usize = 0;
while (true) {
colors[0][0] += 1;
colors[0][1] += 1;
colors[0][2] += 1;
colors[1][0] += 1;
colors[1][1] += 1;
colors[1][2] += 1;
colors[2][0] += 1;
colors[2][1] += 1;
colors[2][2] += 1;
colors[3][0] += 1;
colors[3][1] += 1;
colors[3][2] += 1;
rotate += 1;
const fmtd =
\\:root {{
\\ --timestamp: "{d}";
\\ --interval: "{s}";
\\ --progress-bar: {d}%;
\\ --spinner-1-muted: rgb({d}, {d}, {d});
\\ --spinner-1-primary: rgb({d}, {d}, {d});
\\ --spinner-2-muted: rgb({d}, {d}, {d});
\\ --spinner-2-primary: rgb({d}, {d}, {d});
\\ --spinner-3-muted: rgb({d}, {d}, {d});
\\ --spinner-3-primary: rgb({d}, {d}, {d});
\\ --spinner-4-muted: rgb({d}, {d}, {d});
\\ --spinner-4-primary: rgb({d}, {d}, {d});
\\ --spinner-rotate: {d}deg;
\\}}
;
file = try std.fs.createFileAbsolute(filepath, .{ .truncate = true });
var wrote = try std.fmt.bufPrint(&color_buf, fmtd, .{
counter,
args[args.len - 1],
std.math.mod(f64, std.math.round(((progress_bar + 1.0) / destination_count) * 1000) / 1000, 100),
@floatToInt(u32, std.math.round(@intToFloat(f64, ((colors[0][0] + 1) % 256)) * 0.8)),
@floatToInt(u32, std.math.round(@intToFloat(f64, ((colors[0][1] + 1) % 256)) * 0.8)),
@floatToInt(u32, std.math.round(@intToFloat(f64, ((colors[0][2] + 1) % 256)) * 0.8)),
(colors[0][0] + 1) % 256,
(colors[0][1] + 1) % 256,
(colors[0][2] + 1) % 256,
@floatToInt(u32, std.math.round(@intToFloat(f64, ((colors[1][0] + 1) % 256)) * 0.8)),
@floatToInt(u32, std.math.round(@intToFloat(f64, ((colors[1][1] + 1) % 256)) * 0.8)),
@floatToInt(u32, std.math.round(@intToFloat(f64, ((colors[1][2] + 1) % 256)) * 0.8)),
(colors[1][0] + 1) % 256,
(colors[1][1] + 1) % 256,
(colors[1][2] + 1) % 256,
@floatToInt(u32, std.math.round(@intToFloat(f64, ((colors[2][0] + 1) % 256)) * 0.8)),
@floatToInt(u32, std.math.round(@intToFloat(f64, ((colors[2][1] + 1) % 256)) * 0.8)),
@floatToInt(u32, std.math.round(@intToFloat(f64, ((colors[2][2] + 1) % 256)) * 0.8)),
(colors[2][0] + 1) % 256,
(colors[2][1] + 1) % 256,
(colors[2][2] + 1) % 256,
@floatToInt(u32, std.math.round(@intToFloat(f64, ((colors[3][0] + 1) % 256)) * 0.8)),
@floatToInt(u32, std.math.round(@intToFloat(f64, ((colors[3][1] + 1) % 256)) * 0.8)),
@floatToInt(u32, std.math.round(@intToFloat(f64, ((colors[3][2] + 1) % 256)) * 0.8)),
(colors[3][0] + 1) % 256,
(colors[3][1] + 1) % 256,
(colors[3][2] + 1) % 256,
rotate % 360,
});
progress_bar += 1.0;
_ = try file.writeAll(wrote);
try log.print("[{d}] \"{s}\":{d}\n", .{
std.time.nanoTimestamp(),
filepath,
position,
});
counter += 1;
file.close();
std.time.sleep(ms);
}
}
:root {
--timestamp: "3384";
--interval: "4";
--progress-bar: 88.05600000000001%;
--spinner-1-muted: rgb(177, 42, 12);
--spinner-1-primary: rgb(221, 52, 15);
--spinner-2-muted: rgb(146, 183, 21);
--spinner-2-primary: rgb(182, 229, 26);
--spinner-3-muted: rgb(110, 80, 99);
--spinner-3-primary: rgb(138, 100, 124);
--spinner-4-muted: rgb(190, 164, 143);
--spinner-4-primary: rgb(238, 205, 179);
--spinner-rotate: 145deg;
}
@import "./colors.css";
:root {
--heading-font: "Space Mono", system-ui;
--body-font: "IBM Plex Sans", system-ui;
--color-brand: #02ff00;
--color-brand-muted: rgb(2, 150, 0);
--padding-horizontal: 90px;
--page-background: black;
--page-background-alpha: rgba(0, 0, 0, 0.8);
--result__background-color: black;
--result__primary-color: var(--color-brand);
--result__foreground-color: white;
--result__muted-color: rgb(165, 165, 165);
--card-width: 352px;
--page-width: 1152px;
--snippets_container-background-unfocused: #171717;
--snippets_container-background-focused: #0017e9;
--snippets_container-background: var(
--snippets_container-background-unfocused
);
--snippets_container-muted-color: rgb(153, 153, 153);
}
body {
color: white;
margin: 0;
padding: 0;
font-family: var(--body-font);
background-color: var(--page-background);
color: var(--result__muted-color);
display: flex;
flex-direction: column;
height: 100%;
}
.Subtitle {
text-align: center;
font-size: 4em;
margin: 0;
padding: 0;
margin-bottom: 0.25em;
align-items: center;
display: flex;
flex-direction: row;
}
#reactroot,
#__next,
body,
html {
height: 100%;
}
.Title {
color: var(--color-brand);
font-family: var(--heading-font);
font-weight: 700;
margin-top: 48px;
font-size: 48px;
text-transform: capitalize;
text-align: center;
}
.Description {
text-align: center;
}
.main {
display: flex;
flex-direction: column;
height: 100%;
}
header,
.main {
width: 650px;
margin: 0 auto;
}
section {
width: 650px;
}
header {
margin-bottom: 48px;
}
footer {
flex-shrink: 0;
}
#reactroot,
#__next {
display: flex;
flex-direction: column;
justify-content: center;
}
section {
height: 300px;
display: flex;
flex-direction: column;
}
.timer {
font-weight: normal;
}
.ProgressBar-container {
width: 100%;
display: block;
position: relative;
border: 1px solid var(--color-brand-muted);
border-radius: 4px;
height: 92px;
}
.ProgressBar {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
width: 100%;
height: 100%;
display: block;
background-color: var(--color-brand);
transform-origin: top left;
border-radius: 4px;
transform: scaleX(var(--progress-bar, 0%));
}
.Bundler-container {
background-color: var(--snippets_container-background-focused);
font-size: 64px;
font-weight: bold;
color: white;
left: 0;
right: 0;
padding: 0.8em 0.8em;
}
.Bundler-updateRate {
font-size: 0.8em;
font-weight: normal;
display: flex;
color: var(--result__muted-color);
}
.interval:before {
content: var(--interval, "16");
}
.highlight {
color: white;
}
.timer:after {
content: var(--timestamp);
font-variant-numeric: tabular-nums;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
display: inline;
font-weight: 500;
color: white;
width: 100%;
}
.SectionLabel {
font-weight: 300;
font-family: var(--heading-font);
text-align: center;
width: 100%;
font-weight: 700;
margin-top: 24px;
}
.FooterLabel {
margin-top: 0;
margin-bottom: 12px;
}
.Spinner-container {
--spinner-muted: rgb(0, 255, 0);
--spinner-primary: rgb(0, 60, 255);
width: 96px;
height: 96px;
border-radius: 50%;
background-color: var(--page-background);
border-top: 1.1em solid var(--spinner-muted);
border-right: 1.1em solid var(--spinner-muted);
border-bottom: 1.1em solid var(--spinner-muted);
border-left: 1.1em solid var(--spinner-primary);
transform: rotate(var(--spinner-rotate, 12deg));
}
.Spinners {
display: grid;
grid-auto-flow: column;
justify-content: space-between;
width: 100%;
}
.Spinner-1.Spinner-container {
--spinner-muted: var(--spinner-1-muted);
--spinner-primary: var(--spinner-1-primary);
}
.Spinner-2.Spinner-container {
--spinner-muted: var(--spinner-2-muted);
--spinner-primary: var(--spinner-2-primary);
}
.Spinner-3.Spinner-container {
--spinner-muted: var(--spinner-3-muted);
--spinner-primary: var(--spinner-3-primary);
}
.Spinner-4.Spinner-container {
--spinner-muted: var(--spinner-4-muted);
--spinner-primary: var(--spinner-4-primary);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment