Struct pipedconsole::Console [−][src]
pub struct Console { pub pid: u32, // some fields omitted }
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
Currently this struct does not implement Send or Sync and there is no way of connecting to an already existing console, however this will be added in future versions.
Do not try to send this struct across threads in any way. The handle to
the pipe wich is owned by a Console
can currently be inherited.
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
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 .
Print to the extern console.
To guarantee that the console is flushed one may call the
the crate::Console::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()?;
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()?;
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
Auto Trait Implementations
impl RefUnwindSafe for Console
impl UnwindSafe for Console
Blanket Implementations
Mutably borrows from an owned value. Read more