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
impl Tabs
Sourcepub fn new<S: Into<String>>(titles: impl IntoIterator<Item = S>) -> Self
pub fn new<S: Into<String>>(titles: impl IntoIterator<Item = S>) -> Self
Tabs with these titles.
Examples found in repository?
More 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 }Sourcepub fn selected(self, index: usize) -> Self
pub fn selected(self, index: usize) -> Self
Which tab is chosen.
Examples found in repository?
More 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 }Sourcepub fn color(self, index: usize, color: Rgb) -> Self
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 }Sourcepub fn badge(self, index: usize, color: Rgb, text: impl Into<String>) -> Self
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?
More 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 }Sourcepub fn at(&self, area: Rect, x: usize, y: usize) -> Option<usize>
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 Widget for Tabs
impl Widget for Tabs
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more