Last active
November 6, 2016 22:53
-
-
Save jswrenn/ee1238a62eb7251a9c1297670c0e5bd3 to your computer and use it in GitHub Desktop.
Fireplace 2.0
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
[package] | |
name = "fireplace" | |
version = "0.0.1" | |
authors = ["John Wrenn <[email protected]>"] | |
[dependencies] | |
itertools = "0.5.4" | |
term_size = "0.2.1" |
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
#!/usr/bin/env bash | |
FREQUENCY=0.02 | |
T=0.0 | |
while true; | |
do | |
echo "scale=10;s($T)" | bc -l; | |
#T=$T + 0.1; | |
T=`echo $T + 0.02 | bc`; | |
#echo "$T"; | |
sleep $FREQUENCY; | |
done | cargo run -- |
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
extern crate itertools; | |
extern crate term_size; | |
use itertools::Itertools; | |
use std::iter::repeat; | |
use std::cmp; | |
use std::f64::{MIN_POSITIVE, MAX, INFINITY, NEG_INFINITY}; | |
use std::char::from_u32; | |
use std::io::{Write, BufWriter, BufRead, stdin, stdout}; | |
use term_size::dimensions; | |
//https://github.com/P1start/drawille-rs/blob/master/src/lib.rs#L32 | |
static PIXEL_MAP: [[u32; 2]; 4] = [[0x01, 0x08], | |
[0x02, 0x10], | |
[0x04, 0x20], | |
[0x40, 0x80]]; | |
fn main() { | |
let stdin = stdin(); | |
let mut stdout = BufWriter::new(stdout()); | |
let mut log = Vec::new(); | |
for datum in stdin.lock().lines() | |
.flatten() | |
.flat_map(|line| line.parse::<f64>()) { | |
log.push(datum); | |
// Move the cursor to the top left of the terminal and clear | |
// everything. | |
let _ = write!(stdout, "\x1B[1;1H\x1B[0J"); | |
let (tcols, trows) = dimensions().unwrap(); | |
let ( cols, rows) = (tcols * 2 - 1, (trows * 4 - 1) as f64); | |
let visible = &log[(log.len() - cmp::min(cols, log.len()))..]; | |
// calculate extremes | |
let (min, max) = visible.iter() | |
.fold((INFINITY, NEG_INFINITY), | |
|(min, max), n| (n.min(min), n.max(max))); | |
// Compute a scaling coefficient such that the largest visible | |
// value is at the top of the terminal, and the smallest visible | |
// value is at the bottom of the terminal. | |
let scale = rows / MIN_POSITIVE.max(max - min).min(MAX); | |
let chunks = visible.iter() | |
// Scale the values | |
.map(|&value| (rows - scale * (value - min)).round() as usize) | |
// Chunk in pairs; we can display two values per terminal column | |
.chunks(2); | |
chunks.into_iter() | |
// Each chunk is a terminal column, so we enumerate the chunks | |
// to keep track of the column number. | |
.enumerate() | |
// We enumerate the values within each chunk to keep track of | |
// the sub-column of each value (0 or 1). | |
.flat_map(|(col, chunk)| chunk.enumerate().zip(repeat(col))) | |
// Next, we group together values of a common terminal column | |
// and terminal row. | |
.group_by(|&((_, value), col)| (col, value / 4)).into_iter() | |
// Groups of two values indicate that those two values can be | |
// mapped into a single braille character. | |
.map(|((col, row), group)| | |
(row, col, group.fold(0x2800, | |
|ch, ((sub_col, value), _)| (ch | PIXEL_MAP[value % 4][sub_col])))) | |
// Convert the u32 character value into a unicode character | |
.flat_map(|(row, col, ch)| from_u32(ch).map(|ch| (row, col, ch))) | |
// print! | |
.foreach(|(row, col, ch)| | |
{let _ = write!(&mut stdout, "\x1B[{};{}H{}", row + 1, col + 1, ch);}); | |
let _ = stdout.flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment