Bar

Struct Bar 

Source
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

Source

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?
examples/cli.rs (line 19)
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}
Source

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?
examples/cli.rs (line 23)
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}
Source

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!");
}
Source

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?
examples/cli.rs (line 29)
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}
Source

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 }));

Trait Implementations§

Source§

impl Debug for Bar

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Bar

§

impl RefUnwindSafe for Bar

§

impl Send for Bar

§

impl !Sync for Bar

§

impl Unpin for Bar

§

impl UnwindSafe for Bar

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.