Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

WIP: List #102

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions examples/demo/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use tui::layout::{Constraint, Direction, Layout, Rect};
use tui::style::{Color, Modifier, Style};
use tui::widgets::canvas::{Canvas, Line, Map, MapResolution, Rectangle};
use tui::widgets::{
Axis, BarChart, Block, Borders, Chart, Dataset, Gauge, List, Marker, Paragraph, Row,
SelectableList, Sparkline, Table, Tabs, Text, Widget,
Axis, BarChart, Block, Borders, Chart, Dataset, Gauge, List, Marker, Paragraph, Row, Sparkline,
Table, Tabs, Text, Widget,
};
use tui::{Frame, Terminal};

Expand Down Expand Up @@ -103,28 +103,35 @@ where
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.direction(Direction::Horizontal)
.split(chunks[0]);
SelectableList::default()
let items = app.tasks.items.iter().map(|i| Text::raw(*i)).collect();
List::new(items)
.block(Block::default().borders(Borders::ALL).title("List"))
.items(&app.tasks.items)
.select(Some(app.tasks.selected))
.highlight_style(Style::default().fg(Color::Yellow).modifier(Modifier::BOLD))
.highlight_symbol(">")
.render(f, chunks[0]);
let info_style = Style::default().fg(Color::White);
let info_style = Style::default().fg(Color::Blue);
let warning_style = Style::default().fg(Color::Yellow);
let error_style = Style::default().fg(Color::Magenta);
let critical_style = Style::default().fg(Color::Red);
let events = app.logs.items.iter().map(|&(evt, level)| {
Text::styled(
format!("{}: {}", level, evt),
match level {
let events = app
.logs
.items
.iter()
.map(|&(evt, level)| {
let level_style = match level {
"ERROR" => error_style,
"CRITICAL" => critical_style,
"WARNING" => warning_style,
_ => info_style,
},
)
});
"INFO" => info_style,
_ => Style::default(),
};
Text::with_styles(vec![
(format!("{:<10}", level), level_style),
(format!(" : {}", evt), Style::default()),
])
})
.collect();
List::new(events)
.block(Block::default().borders(Borders::ALL).title("List"))
.render(f, chunks[1]);
Expand Down
6 changes: 5 additions & 1 deletion examples/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ use tui::Terminal;
use crate::util::event::{Event, Events};

fn main() -> Result<(), failure::Error> {
stderrlog::new()
.module(module_path!())
.verbosity(4)
.init()?;
// Terminal initialization
let stdout = io::stdout().into_raw_mode()?;
let stdout = MouseTerminal::from(stdout);
Expand All @@ -40,7 +44,7 @@ fn main() -> Result<(), failure::Error> {
.split(f.size());

Block::default()
.title("Block")
.title("Block 1")
.borders(Borders::ALL)
.render(&mut f, chunks[0]);
Block::default()
Expand Down
78 changes: 56 additions & 22 deletions examples/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use termion::screen::AlternateScreen;
use tui::backend::TermionBackend;
use tui::layout::{Constraint, Corner, Direction, Layout};
use tui::style::{Color, Modifier, Style};
use tui::widgets::{Block, Borders, List, SelectableList, Text, Widget};
use tui::widgets::{Block, Borders, List, Text, Widget};
use tui::Terminal;

use crate::util::event::{Event, Events};
Expand All @@ -29,13 +29,34 @@ impl<'a> App<'a> {
fn new() -> App<'a> {
App {
items: vec![
"Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8", "Item9",
"Item10", "Item11", "Item12", "Item13", "Item14", "Item15", "Item16", "Item17",
"Item18", "Item19", "Item20", "Item21", "Item22", "Item23", "Item24",
"Item1\nItem11\nItem12",
"Item2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222",
"Item3",
"Item4",
"Item5",
"Item6",
"Item7",
"Item8",
"Item9",
"Item10",
"Item11",
"Item12",
"Item13",
"Item14",
"Item15",
"Item16",
"Item17",
"Item18",
"Item19",
"Item20",
"Item21",
"Item22",
"Item23",
"Item24",
],
selected: None,
events: vec![
("Event1", "INFO"),
("Event1\nlorem ipsum", "INFO"),
("Event2", "INFO"),
("Event3", "CRITICAL"),
("Event4", "ERROR"),
Expand All @@ -62,7 +83,7 @@ impl<'a> App<'a> {
("Event25", "INFO"),
("Event26", "INFO"),
],
info_style: Style::default().fg(Color::White),
info_style: Style::default().fg(Color::Blue),
warning_style: Style::default().fg(Color::Yellow),
error_style: Style::default().fg(Color::Magenta),
critical_style: Style::default().fg(Color::Red),
Expand Down Expand Up @@ -97,26 +118,39 @@ fn main() -> Result<(), failure::Error> {
.split(f.size());

let style = Style::default().fg(Color::Black).bg(Color::White);
SelectableList::default()
.block(Block::default().borders(Borders::ALL).title("List"))
.items(&app.items)
.select(app.selected)
.style(style)
.highlight_style(style.fg(Color::LightGreen).modifier(Modifier::BOLD))
.highlight_symbol(">")
.render(&mut f, chunks[0]);
{
let events = app.events.iter().map(|&(evt, level)| {
Text::styled(
format!("{}: {}", level, evt),
match level {
let items = app
.items
.iter()
.map(|i| Text::raw(*i))
.collect::<Vec<Text>>();
List::new(items)
.block(Block::default().borders(Borders::ALL).title("List"))
.style(style)
.select(app.selected)
.highlight_style(style.fg(Color::LightGreen).modifier(Modifier::BOLD))
.highlight_symbol(">")
.render(&mut f, chunks[0]);
}
{
let events = app
.events
.iter()
.map(|&(evt, level)| {
let level_style = match level {
"ERROR" => app.error_style,
"CRITICAL" => app.critical_style,
"WARNING" => app.warning_style,
_ => app.info_style,
},
)
});
"INFO" => app.info_style,
_ => Default::default(),
};
Text::with_styles(vec![
(format!("{:<10}", level), level_style),
(" : ".to_owned(), Default::default()),
(evt.to_owned(), Default::default()),
])
})
.collect();
List::new(events)
.block(Block::default().borders(Borders::ALL).title("List"))
.start_corner(Corner::BottomLeft)
Expand Down
10 changes: 8 additions & 2 deletions examples/user_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use termion::raw::IntoRawMode;
use termion::screen::AlternateScreen;
use tui::backend::TermionBackend;
use tui::layout::{Constraint, Direction, Layout};
use tui::style::{Color, Style};
use tui::style::{Color, Modifier, Style};
use tui::widgets::{Block, Borders, List, Paragraph, Text, Widget};
use tui::Terminal;
use unicode_width::UnicodeWidthStr;
Expand Down Expand Up @@ -76,7 +76,13 @@ fn main() -> Result<(), failure::Error> {
.messages
.iter()
.enumerate()
.map(|(i, m)| Text::raw(format!("{}: {}", i, m)));
.map(|(i, m)| {
Text::with_styles(vec![
(format!("{}", i), Style::default().modifier(Modifier::BOLD)),
(format!(" : {}", m), Style::default()),
])
})
.collect();
List::new(messages)
.block(Block::default().borders(Borders::ALL).title("Messages"))
.render(&mut f, chunks[1]);
Expand Down
2 changes: 1 addition & 1 deletion src/backend/rustbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct RustboxBackend {

impl RustboxBackend {
pub fn new() -> Result<RustboxBackend, rustbox::InitError> {
let rustbox = r#try!(rustbox::RustBox::init(Default::default()));
let rustbox = rustbox::RustBox::init(Default::default())?;
Ok(RustboxBackend { rustbox })
}

Expand Down
2 changes: 1 addition & 1 deletion src/backend/termion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ where

/// Return the size of the terminal
fn size(&self) -> io::Result<Rect> {
let terminal = r#try!(termion::terminal_size());
let terminal = termion::terminal_size()?;
Ok(Rect::new(0, 0, terminal.0, terminal.1))
}

Expand Down
Loading