pipedconsole/
lib.rs

1// Copyright 2021 Foxcirc.
2//
3// Licensed under he MIT license: https://opensource.org/licenses/MIT
4
5//! Create multiple consoles for a **windows** application.
6//! 
7//! This crate makes use of windows Named Pipes to enable a process
8//! to controll multiple consoles.
9//!  
10//! # Quick Overview
11//! 
12//! Normaly a program running on windows can only use one console.
13//! > A process can be associated with only one console, so the AllocConsole 
14//! > function fails if the calling process already has a console.
15//! 
16//! _From the [microsoft docs]._
17//! 
18//! This crate solves this problem by providing an abstraction over a worker process
19//! wich is controlled using named pipes.
20//! 
21//! # Examples
22//! 
23//! You can use the [`Console`] class to create a new console, after that
24//! you can write to it or read a line.
25//! It is planned, that [`Console`] will also implement the `Read` and `Write` traits.
26//! 
27//! ```rust
28//! use pipedconsole::Console;
29//! 
30//! let my_console = Console::new("My Console").expect("Failed to create a new console");
31//! my_console.println("What is your name?"); // seperate window
32//! 
33//! let mut name = String::new();
34//! my_console.read_line(&mut name).expect("Could not read from the console");
35//! println!("Your name is: {}", name); // main processe's console
36//! ```
37//! When the console object is dropped or the calling program exits, the console
38//! will close automaticly, unless another process also writes to it.
39//! 
40//! # Important
41//! This crate comes with a build script wich tries to compile the `console_worker` executable.
42//! You need to have the `cargo` command usable in order for it to work.
43//! This script is important because cargo does not build binaries inside library crates, so
44//! it needs to be done manually.
45//! 
46//! If the build script runs for the first time, it will display an info message.
47//! 
48//! When you run your program, a call to `Console::new` will do the following:
49//! 
50//! 1. It trys to find the `console_worker` executable **in the same directory** as the currently running one.
51//!    This will always work, if you put `console_worker.exe` into the same folder as any executable calling Console::new().
52//! 2. If it cant find the executable, it tries to find it at the default path cargo will put it when you build normally.
53//!    This only works inside the default cargo project structure and makes it easier to just use this crate as-is.
54//! 
55//! If you want to move your executable and run it in a new directory because of some reason,
56//! you will have to find `console_worker.exe` on your computer.
57//! For more information on where you can find it, run the build script again with the `PIPED_CONSOLE_HELP`
58//! environment variable set.
59//! 
60//! If you use `cmd`:
61//! 
62//! ```
63//! set PIPED_CONSOLE_HELP=TRUE
64//! cargo clean
65//! cargo build 
66//! ``` 
67//! 
68//! This should display a message on where the `console_worker` executable is located.
69//! 
70//! # Additional Information
71//! 
72//! Creating a new [`Console`] **will create a new seperate process**. That means you will
73//! see a "console_worker" process in your task manager. That process is just the console
74//! listening for commands to execute.
75//! 
76//! In order to interface to a console-worker process using another language etc. you can
77//! manually launch it and then connect to the named pipe wich is created. For more
78//! information about this see the `worker` documentation inside the repository.
79//! You can find the worker binary files at `/src/bin/console_worker/main.rs`
80//! 
81//! [microsoft docs]: https://docs.microsoft.com/en-us/windows/console/allocconsole
82
83#![deny(missing_docs)]
84#![deny(missing_debug_implementations)]
85
86#[doc(hidden)]
87pub(crate) mod com;
88pub(crate) mod error;
89mod console;
90
91pub use console::Console;
92pub use error::{ConsoleError as Error, ErrorKind};