Struct pager_rs::State

source ·
pub struct State {
    pub pos: (usize, usize),
    pub size: (u16, u16),
    pub content: String,
    pub status_bar: StatusBar,
    pub commands: CommandList,
    pub show_line_numbers: bool,
    /* private fields */
}

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§show_line_numbers: bool

Implementations§

source§

impl State

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)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fn main() -> std::io::Result<()> {
    let content = r#"fn main() {
    println!("Hello World!");
}"#
    .to_string();

    let status_bar = StatusBar::new("Hello World program in rust".to_string());

    let mut state = State::new(content, status_bar, CommandList::default())?;

    pager_rs::init()?;

    pager_rs::run(&mut state)?;

    pager_rs::finish()?;

    Ok(())
}
More examples
Hide additional examples
examples/custom_bar_theme.rs (line 18)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
fn main() -> std::io::Result<()> {
    let content = r#"fn main() {
    println!("Hello World!");
}"#
    .to_string();

    let theme = ContentStyle::new()
        .with(Color::White)
        .on(Color::Red)
        .attribute(Attribute::Italic);
    let status_bar = StatusBar::with_theme(
        "Hello World program in rust with colored status bar".to_string(),
        theme,
    );

    let mut state = State::new(content, status_bar, CommandList::default())?;

    pager_rs::init()?;

    pager_rs::run(&mut state)?;

    pager_rs::finish()?;

    Ok(())
}
examples/read_file.rs (line 15)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
fn main() -> std::io::Result<()> {
    let args: Vec<String> = env::args().collect();

    if args.len() >= 2 {
        let file_name = args[1].clone();

        let mut file = File::open(file_name.clone())?;
        let mut content = String::new();
        file.read_to_string(&mut content)?;

        let status_bar = StatusBar::new(file_name);

        let mut state = State::new(content, status_bar, CommandList::default())?;

        pager_rs::init()?;

        pager_rs::run(&mut state)?;

        pager_rs::finish()?;
    } else {
        eprintln!("Missing Filename");
    }

    Ok(())
}
examples/custom_command.rs (lines 21-47)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
fn main() -> std::io::Result<()> {
    let content = r#"Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Pellentesque neque nulla, viverra ac sapien
et, ultricies convallis lectus. Suspendisse mattis
in urna quis efficitur. Quisque mollis vulputate ipsum,
ut auctor risus luctus eu. Donec sagittis convallis erat
eget imperdiet. Aliquam massa erat, venenatis eu massa at,
dignissim tempus massa. Donec blandit augue et malesuada
fermentum. In vehicula, nisl ut scelerisque sagittis,
sapien elit gravida enim, eu feugiat magna arcu sed enim.
Fusce accumsan sodales ipsum lobortis feugiat. Pellentesque
quam lectus, molestie vitae nisi a, tempor mollis mauris.
Maecenas in magna tempus, porta augue bibendum, feugiat nulla."#
        .to_string();

    let status_bar =
        StatusBar::new("Press 'p' to open selected line on seperate instance".to_string());

    let mut state = State::new(
        content,
        status_bar,
        CommandList::combine(vec![
            CommandList(vec![Command {
                cmd: vec![CommandType::Key(KeyCode::Char('p'))],
                desc: "Open selected line on seperate instance".to_string(),
                func: &|state| {
                    let commands =
                        CommandList::combine(vec![CommandList::quit(), CommandList::navigation()]);

                    let mut modal = State::new(
                        state.content.lines().nth(state.pos.1).unwrap().to_string(),
                        StatusBar::new("Quit (q)".to_string()),
                        commands,
                    )
                    .unwrap();
                    modal.show_line_numbers = false;
                    run(&mut modal).unwrap();
                    true
                },
            }]),
            CommandList::quit(),
            CommandList::navigation(),
            CommandList::help(),
        ]),
    )?;
    state.show_line_numbers = false;

    pager_rs::init()?;

    pager_rs::run(&mut state)?;

    pager_rs::finish()?;

    Ok(())
}
source

pub fn is_running(&self) -> bool

source

pub fn quit(&mut self)

Terminate State

source

pub fn get_help_text(&self) -> String

Default help text formatter

source§

impl State

source

pub fn get_visible(&self) -> String

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

source§

impl State

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 State

source

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

Find and execute command matching with pressed key.

Auto Trait Implementations§

§

impl Freeze for State

§

impl !RefUnwindSafe for State

§

impl !Send for State

§

impl !Sync for State

§

impl Unpin for State

§

impl !UnwindSafe for State

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>,

§

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>,

§

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.