summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs112
1 files changed, 111 insertions, 1 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 51f906c..fa1a737 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -3,8 +3,11 @@
iter_map_windows,
iter_array_chunks,
array_windows,
+ array_chunks,
iter_intersperse,
- impl_trait_in_assoc_type
+ impl_trait_in_assoc_type,
+ array_try_map,
+ slice_first_last_chunk
)]
pub mod day_01;
@@ -58,3 +61,110 @@ pub trait Solution: Problem {
Ok(())
}
}
+
+pub use printer::Printer;
+
+pub mod printer {
+
+ pub struct Printer<W: std::io::Write> {
+ lines: usize,
+ buffer: String,
+ writer: W,
+ }
+
+ impl<W> From<W> for Printer<W>
+ where
+ W: std::io::Write,
+ {
+ fn from(value: W) -> Self {
+ Self {
+ lines: 0,
+ buffer: String::new(),
+ writer: value,
+ }
+ }
+ }
+
+ impl<W: std::io::Write> Printer<W> {
+ pub fn write(&mut self) -> std::io::Result<&mut Self> {
+ writeln!(self.writer, "{}", self.buffer)?;
+ self.writer.flush()?;
+
+ self.lines = self.buffer.lines().count() + 1;
+ self.buffer.clear();
+
+ Ok(self)
+ }
+
+ pub fn write_pause(&mut self, prompt: &str) -> std::io::Result<&mut Self> {
+ use std::io::Read;
+
+ self.buffer.push('\n');
+ self.buffer.push_str(prompt);
+
+ self.write()?;
+ let _ = std::io::stdin().read(&mut [0u8])?;
+ Ok(self)
+ }
+
+ pub fn write_pause_condition(
+ &mut self,
+ prompt: &str,
+ cond: bool,
+ ) -> std::io::Result<&mut Self> {
+ if cond {
+ self.write_pause(prompt)
+ } else {
+ self.write()
+ }
+ }
+
+ pub fn write_sleep(&mut self, time: u64) -> std::io::Result<&mut Self> {
+ self.write()?;
+ std::thread::sleep(std::time::Duration::from_millis(time));
+
+ Ok(self)
+ }
+
+ pub fn clear(&mut self) -> &mut Self {
+ self.buffer
+ .insert_str(0, &"\x1b[1A\x1b[K".repeat(self.lines));
+ self.lines = 0;
+ self
+ }
+
+ pub fn with(&mut self, fmt: std::fmt::Arguments) -> &mut Self {
+ self.buffer.push('\n');
+ self.buffer.push_str(format!("{}", fmt).as_str());
+ self
+ }
+
+ pub fn with_grid<T: std::fmt::Display>(
+ &mut self,
+ grid: &[Vec<T>],
+ highlight: &[(usize, usize)],
+ ) -> &mut Self {
+ let s = grid
+ .iter()
+ .enumerate()
+ .flat_map(|(x, row)| {
+ row.iter()
+ .enumerate()
+ .map(move |(y, s)| {
+ if highlight.contains(&(x, y)) {
+ format!("\x1b[1;30;42m{s}\x1b[0m")
+ } else {
+ s.to_string()
+ }
+ })
+ .chain(std::iter::once("\n".to_string()))
+ })
+ .collect::<String>();
+
+ self.buffer.push('\n');
+ self.buffer.push_str(s.as_str());
+
+ self
+ }
+ }
+}