diff --git a/src/arg_handler.rs b/src/arg_handler.rs index 1cc3b87..1b2cda7 100644 --- a/src/arg_handler.rs +++ b/src/arg_handler.rs @@ -51,6 +51,10 @@ pub struct Arguments { /// Use binary mode to set pixels (`PB` protocol extension) [default: off] #[arg(short, long, alias = "bin")] binary: bool, + + /// Do not flush socket after each pixel [default: on] + #[arg(short, long)] + no_flush: bool, } /// CLI argument handler. @@ -107,4 +111,9 @@ impl ArgHandler { pub fn binary(&self) -> bool { self.data.binary } + + /// Whether to prevent flushing after each pixel. + pub fn no_flush(&self) -> bool { + self.data.no_flush + } } diff --git a/src/main.rs b/src/main.rs index 23294cb..8bfc995 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,6 +44,7 @@ fn start(arg_handler: &ArgHandler) { size, arg_handler.offset(), arg_handler.binary(), + !arg_handler.no_flush(), ); // Load the image manager @@ -56,7 +57,7 @@ fn start(arg_handler: &ArgHandler) { /// Gather important facts about the host. fn gather_host_facts(arg_handler: &ArgHandler) -> Result<(u16, u16), Error> { // Set up a client, and get the screen size - let size = Client::connect(arg_handler.host().to_string(), false)?.read_screen_size()?; + let size = Client::connect(arg_handler.host().to_string(), false, false)?.read_screen_size()?; // Print status println!("Gathered screen size: {}x{}", size.0, size.1); diff --git a/src/pix/canvas.rs b/src/pix/canvas.rs index a4b54db..6efd582 100644 --- a/src/pix/canvas.rs +++ b/src/pix/canvas.rs @@ -27,6 +27,7 @@ impl Canvas { size: (u16, u16), offset: (u16, u16), binary: bool, + flush: bool, ) -> Canvas { // Initialize the object let mut canvas = Canvas { @@ -41,14 +42,14 @@ impl Canvas { println!("Starting painter threads..."); // Spawn some painters - canvas.spawn_painters(binary); + canvas.spawn_painters(binary, flush); // Return the canvas canvas } /// Spawn the painters for this canvas - fn spawn_painters(&mut self, binary: bool) { + fn spawn_painters(&mut self, binary: bool, flush: bool) { // Spawn some painters for i in 0..self.painter_count { // Determine the slice width @@ -58,12 +59,12 @@ impl Canvas { let painter_area = Rect::from((i as u16) * width, 0, width, self.size.1); // Spawn the painter - self.spawn_painter(painter_area, binary); + self.spawn_painter(painter_area, binary, flush); } } /// Spawn a single painter in a thread. - fn spawn_painter(&mut self, area: Rect, binary: bool) { + fn spawn_painter(&mut self, area: Rect, binary: bool, flush: bool) { // Get the host that will be used let host = self.host.to_string(); @@ -80,7 +81,7 @@ impl Canvas { loop { // Connect - match Client::connect(host.clone(), binary) { + match Client::connect(host.clone(), binary, flush) { Ok(client) => { painter.set_client(Some(client)); diff --git a/src/pix/client.rs b/src/pix/client.rs index 9cf2865..5107e9c 100644 --- a/src/pix/client.rs +++ b/src/pix/client.rs @@ -30,21 +30,25 @@ pub struct Client { /// Whether to use binary mode (PB) instead of (PX). binary: bool, + + /// Whether to flush the stream after each pixel. + flush: bool, } impl Client { /// Create a new client instance. - pub fn new(stream: TcpStream, binary: bool) -> Client { + pub fn new(stream: TcpStream, binary: bool, flush: bool) -> Client { Client { stream: BufStream::new(stream), binary, + flush, } } /// Create a new client instane from the given host, and connect to it. - pub fn connect(host: String, binary: bool) -> Result { + pub fn connect(host: String, binary: bool, flush: bool) -> Result { // Create a new stream, and instantiate the client - Ok(Client::new(create_stream(host)?, binary)) + Ok(Client::new(create_stream(host)?, binary, flush)) } /// Write a pixel to the given stream. @@ -108,9 +112,10 @@ impl Client { // Flush, make sure to clear the send buffer // TODO: only flush each 100 pixels? - // TODO: make flushing configurable? // TODO: make buffer size configurable? - self.stream.flush()?; + if self.flush { + self.stream.flush()?; + } // Everything seems to be ok Ok(())