Struct Console

Source
pub struct Console { /* private fields */ }
Expand description

Handle to a console device file, usually located at /dev/console. This structure allows managing virtual terminals.

Implementations§

Source§

impl Console

Source

pub fn open() -> Result<Console, Error>

Opens a new handle to the console device file.

Examples found in repository?
examples/blank.rs (line 9)
6fn main() {
7    
8    // Allocate a new vt
9    let console = Console::open().expect("Cannot open console device");
10    let original_vt = console.current_vt_number().unwrap();
11    let mut vt = console.new_vt_with_minimum_number(7).unwrap();
12    println!("Allocated new VT: {}", vt.number());
13    
14    println!("Switching in 3s...");
15    sleep(Duration::from_secs(3));
16
17    // Setup the vt then switch
18    vt.clear()
19        .and_then(|vt| vt.set_echo(true))
20        .and_then(|vt| vt.switch())
21        .unwrap();
22    
23    // Write something
24    let n = vt.number();
25    writeln!(vt, "Hello world, this is VT {}!", n).unwrap();
26
27    // Blank
28    writeln!(vt, "Blanking in 3s...").unwrap();
29    sleep(Duration::from_secs(3));
30    vt.blank(true).unwrap();
31    sleep(Duration::from_secs(3));
32    vt.blank(false).unwrap();
33
34    // Switch back
35    writeln!(vt, "Example finished. Switching back in 3s...").unwrap();
36    sleep(Duration::from_secs(3));
37    console.switch_to(original_vt).unwrap();
38}
Source

pub fn current_vt_number(&self) -> Result<VtNumber>

Returns the currently active virtual terminal.

Examples found in repository?
examples/blank.rs (line 10)
6fn main() {
7    
8    // Allocate a new vt
9    let console = Console::open().expect("Cannot open console device");
10    let original_vt = console.current_vt_number().unwrap();
11    let mut vt = console.new_vt_with_minimum_number(7).unwrap();
12    println!("Allocated new VT: {}", vt.number());
13    
14    println!("Switching in 3s...");
15    sleep(Duration::from_secs(3));
16
17    // Setup the vt then switch
18    vt.clear()
19        .and_then(|vt| vt.set_echo(true))
20        .and_then(|vt| vt.switch())
21        .unwrap();
22    
23    // Write something
24    let n = vt.number();
25    writeln!(vt, "Hello world, this is VT {}!", n).unwrap();
26
27    // Blank
28    writeln!(vt, "Blanking in 3s...").unwrap();
29    sleep(Duration::from_secs(3));
30    vt.blank(true).unwrap();
31    sleep(Duration::from_secs(3));
32    vt.blank(false).unwrap();
33
34    // Switch back
35    writeln!(vt, "Example finished. Switching back in 3s...").unwrap();
36    sleep(Duration::from_secs(3));
37    console.switch_to(original_vt).unwrap();
38}
Source

pub fn new_vt(&self) -> Result<Vt<'_>>

Allocates a new virtual terminal. To switch to the newly created terminal, use Vt::switch or Console::switch_to.

Source

pub fn new_vt_with_minimum_number(&self, min: i32) -> Result<Vt<'_>>

Allocates a new virtual terminal with a number greater than or equal to the given number. Be careful not to exaggerate too much with the minimum threshold: usually systems have a maximum number of 16 or 64 vts.

To switch to the newly created terminal, use Vt::switch or Console::switch_to.

Examples found in repository?
examples/blank.rs (line 11)
6fn main() {
7    
8    // Allocate a new vt
9    let console = Console::open().expect("Cannot open console device");
10    let original_vt = console.current_vt_number().unwrap();
11    let mut vt = console.new_vt_with_minimum_number(7).unwrap();
12    println!("Allocated new VT: {}", vt.number());
13    
14    println!("Switching in 3s...");
15    sleep(Duration::from_secs(3));
16
17    // Setup the vt then switch
18    vt.clear()
19        .and_then(|vt| vt.set_echo(true))
20        .and_then(|vt| vt.switch())
21        .unwrap();
22    
23    // Write something
24    let n = vt.number();
25    writeln!(vt, "Hello world, this is VT {}!", n).unwrap();
26
27    // Blank
28    writeln!(vt, "Blanking in 3s...").unwrap();
29    sleep(Duration::from_secs(3));
30    vt.blank(true).unwrap();
31    sleep(Duration::from_secs(3));
32    vt.blank(false).unwrap();
33
34    // Switch back
35    writeln!(vt, "Example finished. Switching back in 3s...").unwrap();
36    sleep(Duration::from_secs(3));
37    console.switch_to(original_vt).unwrap();
38}
Source

pub fn open_vt<N: AsVtNumber>(&self, vt_number: N) -> Result<Vt<'_>>

Opens the terminal with the given number.

Source

pub fn switch_to<N: AsVtNumber>(&self, vt_number: N) -> Result<()>

Switches to the virtual terminal with the given number.

Examples found in repository?
examples/blank.rs (line 37)
6fn main() {
7    
8    // Allocate a new vt
9    let console = Console::open().expect("Cannot open console device");
10    let original_vt = console.current_vt_number().unwrap();
11    let mut vt = console.new_vt_with_minimum_number(7).unwrap();
12    println!("Allocated new VT: {}", vt.number());
13    
14    println!("Switching in 3s...");
15    sleep(Duration::from_secs(3));
16
17    // Setup the vt then switch
18    vt.clear()
19        .and_then(|vt| vt.set_echo(true))
20        .and_then(|vt| vt.switch())
21        .unwrap();
22    
23    // Write something
24    let n = vt.number();
25    writeln!(vt, "Hello world, this is VT {}!", n).unwrap();
26
27    // Blank
28    writeln!(vt, "Blanking in 3s...").unwrap();
29    sleep(Duration::from_secs(3));
30    vt.blank(true).unwrap();
31    sleep(Duration::from_secs(3));
32    vt.blank(false).unwrap();
33
34    // Switch back
35    writeln!(vt, "Example finished. Switching back in 3s...").unwrap();
36    sleep(Duration::from_secs(3));
37    console.switch_to(original_vt).unwrap();
38}
Source

pub fn lock_switch(&self, lock: bool) -> Result<()>

Enables or disables virtual terminal switching (usually done with Ctrl + Alt + F<n>).

Source

pub fn blank_timer(&self) -> Result<u32>

Returns the current console blank timer value. A value of 0 means that the timer is disabled. To change the blank timer, use the Vt::set_blank_timer method.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.