1use crossterm::style::Stylize;
2use txtview::{TxtView, TxtViewConfig};
3
4fn bullet(name: &str, desc: &str, style: impl Fn(&str) -> String) -> String {
5 format!(" • {} {}", style(&format!("{name:<22}")), desc)
6}
7
8fn binding(keys: &str, desc: &str) -> String {
9 format!(" {} {}", format!("{keys:<11}").cyan(), desc)
10}
11
12fn setting(name: &str, desc: &str) -> String {
13 format!(" {} {}", format!("{name:<22}").green().italic(), desc)
14}
15
16fn main() {
17 let lines = vec![
18 "Welcome to TxtView".bold().underlined().to_string(),
19 "".to_string(),
20 "What is this?".bold().to_string(),
21 " A lightweight terminal text viewer for Rust, built on".to_string(),
22 format!(
23 " {} with no heavy dependencies.",
24 "crossterm".cyan().bold()
25 ),
26 "".to_string(),
27 "Supported features".bold().to_string(),
28 bullet("Scrolling", "line-by-line or page-by-page", |s| {
29 s.cyan().bold().to_string()
30 }),
31 bullet("Wrapping", "reflows text to the viewport width", |s| {
32 s.yellow().to_string()
33 }),
34 bullet("Scrollbar", "shows the reading position", |s| {
35 s.red().bold().to_string()
36 }),
37 bullet("Line numbers", "optional left-hand column", |s| {
38 s.italic().to_string()
39 }),
40 bullet("Mouse support", "wheel and button scrolling", |s| {
41 s.green().to_string()
42 }),
43 "".to_string(),
44 "Keybindings".bold().to_string(),
45 binding("j / k", "scroll one line"),
46 binding("PgUp / PgDn", "scroll one page"),
47 binding("g / G", "jump to start / end"),
48 binding("q / Esc", "quit"),
49 "".to_string(),
50 "Configuration".bold().to_string(),
51 " Everything is set through TxtViewConfig:".to_string(),
52 setting("show_line_numbers", "toggles the line number column"),
53 setting("show_scrollbar", "toggles the interactive scrollbar"),
54 setting("show_help_bar", "toggles this help section"),
55 "".to_string(),
56 ];
57
58 let mut viewer = TxtView::new(lines.join("\n")).with_config(TxtViewConfig::default());
59 viewer.run().unwrap();
60}