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
impl Console
Sourcepub fn open() -> Result<Console, Error>
pub fn open() -> Result<Console, Error>
Opens a new handle to the console device file.
Examples found in repository?
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}
Sourcepub fn current_vt_number(&self) -> Result<VtNumber>
pub fn current_vt_number(&self) -> Result<VtNumber>
Returns the currently active virtual terminal.
Examples found in repository?
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}
Sourcepub fn new_vt(&self) -> Result<Vt<'_>>
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
.
Sourcepub fn new_vt_with_minimum_number(&self, min: i32) -> Result<Vt<'_>>
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?
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}
Sourcepub fn open_vt<N: AsVtNumber>(&self, vt_number: N) -> Result<Vt<'_>>
pub fn open_vt<N: AsVtNumber>(&self, vt_number: N) -> Result<Vt<'_>>
Opens the terminal with the given number.
Sourcepub fn switch_to<N: AsVtNumber>(&self, vt_number: N) -> Result<()>
pub fn switch_to<N: AsVtNumber>(&self, vt_number: N) -> Result<()>
Switches to the virtual terminal with the given number.
Examples found in repository?
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}
Sourcepub fn lock_switch(&self, lock: bool) -> Result<()>
pub fn lock_switch(&self, lock: bool) -> Result<()>
Enables or disables virtual terminal switching (usually done with Ctrl + Alt + F<n>
).
Sourcepub fn blank_timer(&self) -> Result<u32>
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.