pub struct Bar { /* private fields */ }Expand description
Wrapper around the bar configuration.
This is a safe wrapper around the bar configuration. It can notify consumers about any updates to the state of the configuration file.
The Bar is the central point of interaction for any consumer. The Config can be accessed
through an instance of Bar using the load method. The recv and try_recv methods
should be used to check for updates of any component of the configuration file.
Implementations§
Source§impl Bar
impl Bar
Sourcepub fn load<T: Read>(config_file: T) -> Result<Self, IOError>
pub fn load<T: Read>(config_file: T) -> Result<Self, IOError>
Load the initial bar configuration.
Loads the initial state of the bar configuration from the specified source.
The method will not launch any of the components that are specified in the configuration
file, this is done with the recv and try_recv methods.
§Errors
If the config_file cannot be read or its content is not valid. If the configuration is
invalid, the io::ErrorKind::InvalidData value is returned.
§Examples
use bar_config::Bar;
use std::io::Cursor;
let config_file = Cursor::new(String::from(
"height: 30\n\
monitors:\n\
- { name: \"DVI-1\" }"
));
let bar = Bar::load(config_file).unwrap();
let config = bar.lock();
assert_eq!(config.height, 30);
assert_eq!(config.monitors.len(), 1);
assert_eq!(config.monitors[0].name, "DVI-1");Examples found in repository?
5fn main() {
6 let input = Cursor::new(String::from(
7 "\
8 height: 30\n\
9 monitors:\n\
10 - { name: \"DVI-1\" }\n\
11 left:\n\
12 - { text: \"Hello, World!\" }\n\
13 center:\n\
14 - { name: \"clock\" }\n\
15 right:\n\
16 - { text: \"VOLUME\" }",
17 ));
18
19 let mut bar = Bar::load(input).unwrap();
20
21 print_bar(&bar);
22 loop {
23 let _ = bar.recv();
24 print_bar(&bar);
25 }
26}Sourcepub fn recv(&mut self) -> ComponentID
pub fn recv(&mut self) -> ComponentID
Blocking poll for updates.
Polls the event buffer for the next event. If no event is currently queued, this will block until the next event is received.
§Examples
use bar_config::Bar;
use std::io::Cursor;
let config_file = Cursor::new(String::from(
"height: 30\n\
monitors:\n\
- { name: \"DVI-1\" }\n\
left:\n\
- { name: \"clock\" }"
));
let mut bar = Bar::load(config_file).unwrap();
let component_id = bar.recv();
println!("Component {:?} was updated!", component_id);Examples found in repository?
5fn main() {
6 let input = Cursor::new(String::from(
7 "\
8 height: 30\n\
9 monitors:\n\
10 - { name: \"DVI-1\" }\n\
11 left:\n\
12 - { text: \"Hello, World!\" }\n\
13 center:\n\
14 - { name: \"clock\" }\n\
15 right:\n\
16 - { text: \"VOLUME\" }",
17 ));
18
19 let mut bar = Bar::load(input).unwrap();
20
21 print_bar(&bar);
22 loop {
23 let _ = bar.recv();
24 print_bar(&bar);
25 }
26}Sourcepub fn try_recv(&mut self) -> Option<ComponentID>
pub fn try_recv(&mut self) -> Option<ComponentID>
Non-Blocking poll for updates.
Polls the event buffer for the next event. If no event is currently queued, this will
return None.
§Examples
use bar_config::Bar;
use std::io::Cursor;
let config_file = Cursor::new(String::from(
"height: 30\n\
monitors:\n\
- { name: \"DVI-1\" }\n\
left:\n\
- { name: \"clock\" }"
));
let mut bar = Bar::load(config_file).unwrap();
if let Some(component_id) = bar.try_recv() {
println!("Component {:?} was updated!", component_id);
} else {
println!("No new event!");
}Sourcepub fn lock(&self) -> MutexGuard<'_, Config>
pub fn lock(&self) -> MutexGuard<'_, Config>
Lock the configuration file.
Locks the configuration file so its state can be used to render the bar. Since this creates
a MutexGuard, no events will be received while the lock is held.
§Examples
use bar_config::Bar;
use std::io::Cursor;
let config_file = Cursor::new(String::from(
"height: 30\n\
monitors:\n\
- { name: \"DVI-1\" }"
));
let mut bar = Bar::load(config_file).unwrap();
let config = bar.lock();
assert_eq!(config.height, 30);
assert_eq!(config.monitors.len(), 1);
assert_eq!(config.monitors[0].name, "DVI-1");Examples found in repository?
28fn print_bar(bar: &Bar) {
29 let config = bar.lock();
30 for comp in config
31 .left
32 .iter()
33 .chain(&config.center)
34 .chain(&config.right)
35 {
36 if let Some(text) = comp.text() {
37 print!("{}\t", text);
38 }
39 }
40 println!("");
41}Sourcepub fn notify(&mut self, event: Event)
pub fn notify(&mut self, event: Event)
Send an event to all components.
Notifies all components that a new event is available. The components then have the choice to react upon the event or ignore it completely.
If a component handles the event and marks itself as dirty as a result of the event, a
new redraw request will be queued for the recv and try_recv methods.
§Examples
use bar_config::event::{Event, Point};
use bar_config::Bar;
use std::io::Cursor;
let config_file = Cursor::new(String::from(
"height: 30\n\
monitors:\n\
- { name: \"DVI-1\" }"
));
let mut bar = Bar::load(config_file).unwrap();
bar.notify(Event::MouseMotion(Point { x: 0, y: 0 }));