tegrine 0.1.3

Draw in a Terminal -- Ordered with Instances -> Dots like 2D Vertex/Pixels
Documentation
//! Initialization module


#![allow(unused)]


#[derive(Clone, Default)]
pub struct D2 {
	pub x: isize,
	pub y: isize,
}


impl D2 {
	pub fn new(x: isize, y: isize) -> D2 {
		D2 { x, y }
	}


	pub fn within_max(&self, max: &D2) -> bool {
		self.x >= 0 && self.y >= 0 && self.x < max.x && self.y < max.y
	}
}


#[derive(Default, Debug)]
pub struct Rgba {
	pub r: u8,
	pub g: u8,
	pub b: u8,
	pub a: u8,
}


impl Rgba {
	pub fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
		Self { r, g, b, a }
	}
}


#[derive(Default)]
pub struct Dot {
	pub pos: D2,
	pub color: Rgba,

	pub neighbors: Vec<usize>,

	/*
	size: D2,
	shape: u8,

	c: char,
	text: String,
	*/
}


impl Dot {
	pub fn new(pos: D2, color: Rgba) -> Dot {
		Dot { pos, color, neighbors: vec![] }
	}

	/*
	pub fn new_shape(pos: D2, color: Rgba, size: D2, shape: u8) -> Dot {
		let mut d = Self::new(pos, color);

		d.size = size;
		d.shape = shape;

		d
	}
	*/

	pub fn new_vertex(pos: D2, color: Rgba, neighbors: Vec<usize>) -> Dot {
		let mut d = Self::new(pos, color);

		d.neighbors = neighbors;

		d
	}

	/*
	pub fn new_text(pos: D2, color: Rgba, text: String) -> Dot {
		let mut d = Self::new(pos, color);

		d.text = text;

		d
	}
	*/


	pub fn connect_neighbor(&mut self, to: usize) {
		self.neighbors.push(to);
	}
}


#[derive(Default)]
pub struct Instance {
	pub dots: Vec<Dot>,

	pub pos: D2,
	// size: D2,
}


impl Instance {
	pub fn push_dot(&mut self, d: Dot) -> usize {
		self.dots.push(d);

		self.dots.len() - 1
	}
}


#[derive(Default)]
pub struct Tegrine {
	pub x: Vec<Instance>,

	pub ws: D2,
	pub b: Vec<Vec<Option<(usize, usize)>>>,
}


use anyhow::Result;
use crossterm::terminal::{disable_raw_mode, enable_raw_mode, size};


impl Tegrine {
	pub fn new() -> Result<Self> {
		let mut x = Self::default();

		x.set_ws()?;

		enable_raw_mode()?;

		Ok(x)
	}


	fn set_ws(&mut self) -> Result<()> {
		let (x, y) = size()?;

		self.ws = D2 {
			x: x as isize,
			y: y as isize,
		};

		// Screen Buffer
		self.b = vec![vec![None; self.ws.x as usize]; self.ws.y as usize];

		Ok(())
	}

	pub fn push_instance(&mut self, ins: Instance) -> usize {
		self.x.push(ins);

		self.x.len() - 1
	}
}

impl Drop for Tegrine {
	fn drop(&mut self) {
		disable_raw_mode().unwrap();
	}
}