Skip to main content

Tabs

Struct Tabs 

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

A row of tabs, the chosen one a soft pink pill. A tab can have a colour of its own, and a little badge after its title: a count, a mark.

use tinsel::app::{Frame, Tabs};

tinsel::set_color_mode(tinsel::ColorMode::None);
let tabs = Tabs::new(["overview", "logs"]).selected(1);
let mut frame = Frame::new(30, 1);
frame.render(frame.area(), &tabs);
assert_eq!(frame.snapshot().plain(), " overview   logs");
assert_eq!(tabs.at(frame.area(), 13, 0), Some(1));

Implementations§

Source§

impl Tabs

Source

pub fn new<S: Into<String>>(titles: impl IntoIterator<Item = S>) -> Self

Tabs with these titles.

Examples found in repository?
examples/dashboard.rs (line 125)
123    fn tabs(&self) -> Tabs {
124        let slow = self.services.iter().filter(|s| self.slow(s)).count();
125        let tabs = Tabs::new(TABS).selected(self.tab);
126        let tabs = if slow == 0 {
127            tabs.badge(0, palette::SAGE, "✔")
128        } else {
129            tabs.badge(0, palette::AMBER, slow.to_string())
130        };
131        tabs.badge(1, palette::MUTED, self.logs.len().to_string())
132    }
More examples
Hide additional examples
examples/services.rs (line 262)
260    fn environments(&self) -> Tabs {
261        let down = self.services.iter().any(|s| s.health == Health::Down);
262        let mut tabs = Tabs::new(ENVS).selected(self.env).color(1, palette::RED);
263        for i in 0..ENVS.len() {
264            tabs = if self.services.is_empty() {
265                tabs.badge(i, palette::MUTED, "○")
266            } else if down && i == 1 {
267                tabs.badge(i, palette::RED, "✖")
268            } else {
269                tabs.badge(i, palette::SAGE, "✔")
270            };
271        }
272        tabs
273    }
Source

pub fn selected(self, index: usize) -> Self

Which tab is chosen.

Examples found in repository?
examples/dashboard.rs (line 125)
123    fn tabs(&self) -> Tabs {
124        let slow = self.services.iter().filter(|s| self.slow(s)).count();
125        let tabs = Tabs::new(TABS).selected(self.tab);
126        let tabs = if slow == 0 {
127            tabs.badge(0, palette::SAGE, "✔")
128        } else {
129            tabs.badge(0, palette::AMBER, slow.to_string())
130        };
131        tabs.badge(1, palette::MUTED, self.logs.len().to_string())
132    }
More examples
Hide additional examples
examples/services.rs (line 262)
260    fn environments(&self) -> Tabs {
261        let down = self.services.iter().any(|s| s.health == Health::Down);
262        let mut tabs = Tabs::new(ENVS).selected(self.env).color(1, palette::RED);
263        for i in 0..ENVS.len() {
264            tabs = if self.services.is_empty() {
265                tabs.badge(i, palette::MUTED, "○")
266            } else if down && i == 1 {
267                tabs.badge(i, palette::RED, "✖")
268            } else {
269                tabs.badge(i, palette::SAGE, "✔")
270            };
271        }
272        tabs
273    }
Source

pub fn color(self, index: usize, color: Rgb) -> Self

Draw tab index’s title in color, chosen or not: red for one with errors in it, say. An index past the last tab is ignored.

use tinsel::app::Tabs;
use tinsel::palette;

let tabs = Tabs::new(["overview", "errors"]).color(1, palette::RED);
Examples found in repository?
examples/services.rs (line 262)
260    fn environments(&self) -> Tabs {
261        let down = self.services.iter().any(|s| s.health == Health::Down);
262        let mut tabs = Tabs::new(ENVS).selected(self.env).color(1, palette::RED);
263        for i in 0..ENVS.len() {
264            tabs = if self.services.is_empty() {
265                tabs.badge(i, palette::MUTED, "○")
266            } else if down && i == 1 {
267                tabs.badge(i, palette::RED, "✖")
268            } else {
269                tabs.badge(i, palette::SAGE, "✔")
270            };
271        }
272        tabs
273    }
Source

pub fn badge(self, index: usize, color: Rgb, text: impl Into<String>) -> Self

Put a little badge after tab index’s title, in color: a count, or a mark like ✔. Plain text without colour. An index past the last tab is ignored.

use tinsel::app::{Frame, Tabs};
use tinsel::palette;

tinsel::set_color_mode(tinsel::ColorMode::None);
let tabs = Tabs::new(["build", "deploy"]).badge(0, palette::SAGE, "✔").badge(1, palette::RED, "3");
let mut frame = Frame::new(30, 1);
frame.render(frame.area(), &tabs);
assert_eq!(frame.snapshot().plain(), " build ✔   deploy 3");
assert_eq!(tabs.at(frame.area(), 7, 0), Some(0));
Examples found in repository?
examples/dashboard.rs (line 127)
123    fn tabs(&self) -> Tabs {
124        let slow = self.services.iter().filter(|s| self.slow(s)).count();
125        let tabs = Tabs::new(TABS).selected(self.tab);
126        let tabs = if slow == 0 {
127            tabs.badge(0, palette::SAGE, "✔")
128        } else {
129            tabs.badge(0, palette::AMBER, slow.to_string())
130        };
131        tabs.badge(1, palette::MUTED, self.logs.len().to_string())
132    }
More examples
Hide additional examples
examples/services.rs (line 265)
260    fn environments(&self) -> Tabs {
261        let down = self.services.iter().any(|s| s.health == Health::Down);
262        let mut tabs = Tabs::new(ENVS).selected(self.env).color(1, palette::RED);
263        for i in 0..ENVS.len() {
264            tabs = if self.services.is_empty() {
265                tabs.badge(i, palette::MUTED, "○")
266            } else if down && i == 1 {
267                tabs.badge(i, palette::RED, "✖")
268            } else {
269                tabs.badge(i, palette::SAGE, "✔")
270            };
271        }
272        tabs
273    }
Source

pub fn at(&self, area: Rect, x: usize, y: usize) -> Option<usize>

Which tab is at x, y, if the tabs are drawn at area: for clicks.

Examples found in repository?
examples/dashboard.rs (line 291)
271    fn event(&mut self, event: &Event) -> Flow {
272        match event {
273            Event::Tick => {
274                // drawn up to 20 times a second while focused, but a new reading only 5 times
275                // a second, focused or not
276                if app::time() - self.sampled >= SAMPLE_EVERY {
277                    self.sampled = app::time();
278                    self.sample();
279                }
280            }
281            Event::Key(Key::Char('q')) => return Flow::Quit,
282            Event::Key(Key::Esc) if self.help => self.help = false,
283            Event::Key(Key::Esc) => return Flow::Quit,
284            Event::Key(Key::Char('?')) => self.help = !self.help,
285            Event::Key(Key::Left | Key::BackTab) => self.tab = self.tab.saturating_sub(1),
286            Event::Key(Key::Right | Key::Tab) => self.tab = (self.tab + 1).min(TABS.len() - 1),
287            Event::Key(Key::Char(c @ '1'..='2')) => self.tab = *c as usize - '1' as usize,
288            Event::Mouse(m) if m.kind == MouseKind::Click => {
289                // where view puts the tabs: two in from the edge, after the 22-column name
290                let tabs = app::Rect::new(24, 1, 60, 1);
291                if let Some(i) = self.tabs().at(tabs, m.x, m.y) {
292                    self.tab = i;
293                }
294            }
295            other => {
296                app::navigate(&mut self.selected, self.services.len(), other, Keymap::Vim);
297            }
298        }
299        Flow::Continue
300    }

Trait Implementations§

Source§

impl Clone for Tabs

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Tabs

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Widget for Tabs

Source§

fn render(&self, area: Rect, frame: &mut Frame)

Draw into area of frame. area is already clipped to the screen. A widget that moves asks for the frames it needs, with Frame::animate or Frame::animate_at, so whoever draws it needn’t.
Source§

fn size(&self) -> Option<(usize, usize)>

How many columns and rows it would like, as (width, height) like every size in tinsel, if it knows: what a Modal is sized to. None unless implemented. Read more
Source§

fn inner(&self, area: Rect) -> Rect

Where what it holds goes when it’s drawn at area: inside a Panel’s border and padding, say. The whole area unless implemented; Frame::modal gives it back, so what’s in a modal can be drawn there. Read more

Auto Trait Implementations§

§

impl Forget for Tabs

§

impl Freeze for Tabs

§

impl Move for Tabs

§

impl RefUnwindSafe for Tabs

§

impl Send for Tabs

§

impl Sync for Tabs

§

impl Unpin for Tabs

§

impl UnsafeUnpin for Tabs

§

impl UnwindSafe for Tabs

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.