Struct pipedconsole::Console
source · pub struct Console {
pub pid: u32,
/* private fields */
}
Expand description
Used for printing and reading from an external console.
This struct is the main interaction point to a worker process you can controll using
this struct’s methods like Self::println
or everything else documented here.
Examples
use pipedconsole::Console;
let my_console = Console::new("My console")?;
// Prints hello world on another console window.
my_console.println("What is your name?");
let mut name = String::new();
my_console.read_to_string(&mut name)?
// Prints normally on the calling processe's console.
println!("Your name is: {}", name);
Cloning
You can clone this struct however you want, but note that all cloned instances will controll the same console window.
Threads
A Console
cannot be send between threads safely, as that would create
raceconditions with the worker process.
Fields§
§pid: u32
The process id of the worker process. You can use this to further interface with the process. You can shutdown the worker process using this, to ensure that it is closed “correctly” even if it get’s stuck, although that souldn’t happen under normal conditions.
Implementations§
source§impl Console
impl Console
sourcepub fn new(name: &str) -> Result<Self, Error>
pub fn new(name: &str) -> Result<Self, Error>
Creates a new Console object with the specified name.
This function is currently the only way of launching a new console.
It spawns a worker process wich waits for any messages
from the parent and then prints them.
For more information about that see [console-worker
].
The console is closed automaticly when the returned Console
is
dropped or your program exits.
Examples
use pipedconsole::Console;
let my_console = Console::new("My console")?; // creates a new console window
my_console.println("Hello world!")?;
Technical details
This method creates a worker process using the CreateProcess function from winapi
and then obtains a handle to the pipe by calling the CreateFile function.
For more information about the information in returned errors see crate::Error
: pipedconsole::Error .
source§impl Console
impl Console
sourcepub fn print<T: ToString>(&self, message: T) -> Result<usize, Error>
pub fn print<T: ToString>(&self, message: T) -> Result<usize, Error>
Print to the extern console.
To guarantee that the console is flushed one may call the the flush function when done printing.
Examples
use pipedconsole::Console;
let my_console = Console::new()?;
// Produces "Hello world!" as an output.
my_console.print("Hello ")?;
my_console.print("world!")?;
my_console.flush()?;
source§impl Console
impl Console
source§impl Console
impl Console
sourcepub fn read_line(&self, target: &mut String) -> Result<usize, Error>
pub fn read_line(&self, target: &mut String) -> Result<usize, Error>
Read a line from the console. Similar to std::io::stdin().read_to_string()
Reads from the console until a newline (the 0xA byte) is reached, and appends them to the provided buffer. This function will block until all the input was read.
Examples
use pipedconsole::Console;
let my_console = Console::new()?;
let mut buffer = String::new();
my_console.read_line(&mut buffer)?;
println!("You entered {}", &buffer);
Trait Implementations§
source§impl Drop for Console
impl Drop for Console
Closes the handle to the pipe. When the handle is closed, the worker process will automaticly exit.
source§impl Write for Console
impl Write for Console
source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Prints the data to the console.
The data must be valid utf-8, otherwise ErrorKind::InvalidInput
will be returned.
If an internal error is generated, ErrorKind::InvalidData
may be retuend.
source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Force the console to flush.
This function should be called when you are done printing something, to ensure that is actually gets displayed correctly.
Usually this function only needs to be called after a call to
crate::Console::print
. (If no newline character is at the end of the message.)
Examples
use pipedconsole::Console;
let my_console = Console::new("My Console")?;
for i 0..100 {
my_console.print(i)?;
};
my_console.flush()?;
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)