libnotcurses_sys/widgets/progbar/
methods.rs

1//! `NcProgBar` & `NcProgBarOptions` methods and associated functions.
2
3use super::{NcProgBar, NcProgBarOptions};
4use crate::{c_api, error, NcPlane, NcResult};
5
6/// # `NcProgBarOptions` Methods
7impl NcProgBarOptions {
8    /// New NcProgBarOptions for [NcProgBar].
9    pub fn new() -> Self {
10        Self { ulchannel: 0, urchannel: 0, blchannel: 0, brchannel: 0, flags: 0 }
11    }
12}
13
14/// # `NcProgBar` Methods
15impl NcProgBar {
16    /// New NcProgBar.
17    ///
18    /// Takes ownership of the `plane`, which will be destroyed by
19    /// [destroy][NcProgBar#method.destroy](). The progress bar is initially at 0%.
20    pub fn new<'a>(plane: &mut NcPlane) -> &'a mut Self {
21        Self::with_options(plane, &NcProgBarOptions::new())
22    }
23
24    /// New NcProgBar. Expects an [NcProgBarOptions] struct.
25    ///
26    /// *C style function: [ncprogbar_create()][c_api::ncprogbar_create].*
27    pub fn with_options<'a>(plane: &mut NcPlane, options: &NcProgBarOptions) -> &'a mut Self {
28        unsafe { &mut *c_api::ncprogbar_create(plane, options) }
29    }
30
31    /// Destroy the progress bar and its underlying ncplane.
32    ///
33    /// *C style function: [ncprogbar_destroy()][c_api::ncprogbar_destroy].*
34    pub fn destroy(&mut self) {
35        unsafe {
36            c_api::ncprogbar_destroy(self);
37        }
38    }
39
40    /// Return a reference to the ncprogbar's underlying ncplane.
41    ///
42    /// *C style function: [ncprogbar_plane()][c_api::ncprogbar_plane].*
43    pub fn plane(&mut self) -> &mut NcPlane {
44        unsafe { &mut *c_api::ncprogbar_plane(self) }
45    }
46
47    /// Get the progress bar's completion, an [f64] on [0, 1].
48    ///
49    /// *C style function: [ncprogbar_progress()][c_api::ncprogbar_progress].*
50    pub fn progress(&self) -> f64 {
51        unsafe { c_api::ncprogbar_progress(self) }
52    }
53
54    /// Sets the progress bar's completion, an 0 <= [f64] <= 1.
55    ///
56    /// Returns [`NCRESULT_ERR`][c_api::NCRESULT_ERR]
57    /// if progress is < 0 || > 1.
58    ///
59    /// *C style function: [ncprogbar_set_progress()][c_api::ncprogbar_set_progress].*
60    pub fn set_progress(&mut self, progress: f64) -> NcResult<()> {
61        error![unsafe { c_api::ncprogbar_set_progress(self, progress) }]
62    }
63}