From da67a679f744ff3d9abc055f1acec75fc0d190d2 Mon Sep 17 00:00:00 2001 From: Florian Dehau Date: Thu, 27 Oct 2016 19:34:45 +0200 Subject: [PATCH] Add Tabs widget --- src/widgets/tabs.rs | 94 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/widgets/tabs.rs diff --git a/src/widgets/tabs.rs b/src/widgets/tabs.rs new file mode 100644 index 0000000..db5c66f --- /dev/null +++ b/src/widgets/tabs.rs @@ -0,0 +1,94 @@ +use unicode_width::UnicodeWidthStr; + +use widgets::{Block, Widget}; +use buffer::Buffer; +use layout::Rect; +use style::Color; +use symbols::line; + +pub struct Tabs<'a> { + block: Option>, + titles: &'a [&'a str], + selected: usize, + highlight_color: Color, + color: Color, +} + +impl<'a> Default for Tabs<'a> { + fn default() -> Tabs<'a> { + Tabs { + block: None, + titles: &[], + selected: 0, + highlight_color: Color::Reset, + color: Color::Reset, + } + } +} + +impl<'a> Tabs<'a> { + pub fn block(&mut self, block: Block<'a>) -> &mut Tabs<'a> { + self.block = Some(block); + self + } + + pub fn titles(&mut self, titles: &'a [&'a str]) -> &mut Tabs<'a> { + self.titles = titles; + self + } + + pub fn select(&mut self, selected: usize) -> &mut Tabs<'a> { + self.selected = selected; + self + } + + pub fn color(&mut self, color: Color) -> &mut Tabs<'a> { + self.color = color; + self + } + + pub fn highlight_color(&mut self, color: Color) -> &mut Tabs<'a> { + self.highlight_color = color; + self + } +} + +impl<'a> Widget for Tabs<'a> { + fn buffer(&self, area: &Rect, buf: &mut Buffer) { + let tabs_area = match self.block { + Some(b) => { + b.buffer(area, buf); + b.inner(area) + } + None => *area, + }; + if tabs_area.height < 1 { + return; + } + + let mut x = tabs_area.left(); + for (title, color) in self.titles.iter().enumerate().map(|(i, t)| if i == self.selected { + (t, self.highlight_color) + } else { + (t, self.color) + }) { + x += 1; + if x > tabs_area.right() { + break; + } else { + buf.set_string(x, tabs_area.top(), title, color, Color::Reset); + x += title.width() as u16 + 1; + if x > tabs_area.right() { + break; + } else { + buf.set_cell(x, + tabs_area.top(), + line::VERTICAL, + Color::Reset, + Color::Reset); + x += 1; + } + } + } + } +}