Struct State

Source
pub struct State<'a> {
    pub pos: (usize, usize),
    pub size: (u16, u16),
    pub content: String,
    pub status_bar: StatusBar,
    pub commands: CommandList,
    pub show_line_numbers: bool,
    pub word_wrap: bool,
    pub word_wrap_option: Options<'a>,
    /* private fields */
}
Expand description

State that can be ran with pager_rs::run

Fields§

§pos: (usize, usize)

Cursor position in content.

(x, y)

§size: (u16, u16)

Size of terminal screen.

(width, height)

§content: String

Content to show.

§status_bar: StatusBar

status bar at the bottom.

§commands: CommandList

List of Commands that runnable in this State.

§show_line_numbers: bool

Show/Hide line numbers.

§word_wrap: bool

Enable/Disable word-wrap

§word_wrap_option: Options<'a>

textwrap::Options to use when word-wrap is enabled.

The width is not important since it will be replaced by terminal screen width when rendering text.

Implementations§

Source§

impl<'a> State<'a>

Source

pub fn new( content: String, status_bar: StatusBar, commands: CommandList, ) -> Result<Self>

Create new State

Examples found in repository?
examples/hello_world.rs (line 10)
2fn main() -> std::io::Result<()> {
3    let content = r#"fn main() {
4    println!("Hello World!");
5}"#
6    .to_string();
7
8    let status_bar = StatusBar::new("Hello World program in rust".to_string());
9
10    let mut state = State::new(content, status_bar, CommandList::default())?;
11
12    pager_rs::init()?;
13
14    pager_rs::run(&mut state)?;
15
16    pager_rs::finish()?;
17
18    Ok(())
19}
More examples
Hide additional examples
examples/custom_bar_theme.rs (line 18)
3fn main() -> std::io::Result<()> {
4    let content = r#"fn main() {
5    println!("Hello World!");
6}"#
7    .to_string();
8
9    let theme = ContentStyle::new()
10        .with(Color::White)
11        .on(Color::Red)
12        .attribute(Attribute::Italic);
13    let status_bar = StatusBar::with_theme(
14        "Hello World program in rust with colored status bar".to_string(),
15        theme,
16    );
17
18    let mut state = State::new(content, status_bar, CommandList::default())?;
19
20    pager_rs::init()?;
21
22    pager_rs::run(&mut state)?;
23
24    pager_rs::finish()?;
25
26    Ok(())
27}
examples/read_file.rs (line 15)
3fn main() -> std::io::Result<()> {
4    let args: Vec<String> = env::args().collect();
5
6    if args.len() >= 2 {
7        let file_name = args[1].clone();
8
9        let mut file = File::open(file_name.clone())?;
10        let mut content = String::new();
11        file.read_to_string(&mut content)?;
12
13        let status_bar = StatusBar::new(file_name);
14
15        let mut state = State::new(content, status_bar, CommandList::default())?;
16
17        pager_rs::init()?;
18
19        pager_rs::run(&mut state)?;
20
21        pager_rs::finish()?;
22    } else {
23        eprintln!("Missing Filename");
24    }
25
26    Ok(())
27}
examples/custom_command.rs (lines 21-47)
3fn main() -> std::io::Result<()> {
4    let content = r#"Lorem ipsum dolor sit amet, consectetur adipiscing
5elit. Pellentesque neque nulla, viverra ac sapien
6et, ultricies convallis lectus. Suspendisse mattis
7in urna quis efficitur. Quisque mollis vulputate ipsum,
8ut auctor risus luctus eu. Donec sagittis convallis erat
9eget imperdiet. Aliquam massa erat, venenatis eu massa at,
10dignissim tempus massa. Donec blandit augue et malesuada
11fermentum. In vehicula, nisl ut scelerisque sagittis,
12sapien elit gravida enim, eu feugiat magna arcu sed enim.
13Fusce accumsan sodales ipsum lobortis feugiat. Pellentesque
14quam lectus, molestie vitae nisi a, tempor mollis mauris.
15Maecenas in magna tempus, porta augue bibendum, feugiat nulla."#
16        .to_string();
17
18    let status_bar =
19        StatusBar::new("Press 'p' to open selected line on seperate instance".to_string());
20
21    let mut state = State::new(
22        content,
23        status_bar,
24        CommandList::combine(vec![
25            CommandList(vec![Command {
26                cmd: vec![CommandType::Key(KeyCode::Char('p'))],
27                desc: "Open selected line on seperate instance".to_string(),
28                func: &|state| {
29                    let commands =
30                        CommandList::combine(vec![CommandList::quit(), CommandList::navigation()]);
31
32                    let mut modal = State::new(
33                        state.content.lines().nth(state.pos.1).unwrap().to_string(),
34                        StatusBar::new("Quit (q)".to_string()),
35                        commands,
36                    )
37                    .unwrap();
38                    modal.show_line_numbers = false;
39                    run(&mut modal).unwrap();
40                    true
41                },
42            }]),
43            CommandList::quit(),
44            CommandList::navigation(),
45            CommandList::help(),
46        ]),
47    )?;
48    state.show_line_numbers = false;
49
50    pager_rs::init()?;
51
52    pager_rs::run(&mut state)?;
53
54    pager_rs::finish()?;
55
56    Ok(())
57}
Source

pub fn is_running(&self) -> bool

Returns true if the State is still runing.

Source

pub fn quit(&mut self)

Terminate State

Source

pub fn get_help_text(&self) -> String

Default help text formatter

Source§

impl<'a> State<'a>

Source

pub fn get_visible(&self) -> String

Get text to be printed on terminal except for the StatusBar.

Source§

impl<'a> State<'a>

Source

pub fn up(&mut self) -> bool

Move cursor up.

Source

pub fn down(&mut self) -> bool

Move cursor down.

Source

pub fn left(&mut self) -> bool

Move cursor left.

Source

pub fn right(&mut self) -> bool

Move cursor right.

Source

pub fn pgup(&mut self) -> bool

Move cursor one page up.

Source

pub fn pgdown(&mut self) -> bool

Move cursor one page down.

Source

pub fn home(&mut self) -> bool

Move cursor to the start.

Source

pub fn end(&mut self) -> bool

Move cursor to the end.

Source§

impl<'a> State<'a>

Source

pub fn match_key_event(&mut self, code: KeyCode) -> bool

Find and execute command matching with pressed key.

Auto Trait Implementations§

§

impl<'a> Freeze for State<'a>

§

impl<'a> !RefUnwindSafe for State<'a>

§

impl<'a> !Send for State<'a>

§

impl<'a> !Sync for State<'a>

§

impl<'a> Unpin for State<'a>

§

impl<'a> !UnwindSafe for State<'a>

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.