use std::fmt::Display;
use std::io::Write;
use crate::{execute, impl_display, queue, write_cout, Result};
pub trait Command {
type AnsiType: Display;
fn ansi_code(&self) -> Self::AnsiType;
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()>;
}
pub trait QueueableCommand<T: Display>: Sized {
fn queue(&mut self, command: impl Command<AnsiType = T>) -> Result<&mut Self>;
}
pub trait ExecutableCommand<T: Display>: Sized {
fn execute(&mut self, command: impl Command<AnsiType = T>) -> Result<&mut Self>;
}
impl<T, A> QueueableCommand<A> for T
where
A: Display,
T: Write,
{
fn queue(&mut self, command: impl Command<AnsiType = A>) -> Result<&mut Self> {
queue!(self, command)?;
Ok(self)
}
}
impl<T, A> ExecutableCommand<A> for T
where
A: Display,
T: Write,
{
fn execute(&mut self, command: impl Command<AnsiType = A>) -> Result<&mut Self> {
execute!(self, command)?;
Ok(self)
}
}
pub struct Output(pub String);
impl Command for Output {
type AnsiType = String;
fn ansi_code(&self) -> Self::AnsiType {
return self.0.clone();
}
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
print!("{}", self.0);
Ok(())
}
}
impl_display!(for Output);