Struct minus::Pager[][src]

pub struct Pager { /* fields omitted */ }
Expand description

A struct containing basic configurations for the pager. This is used by all initializing functions

Example

You can use any async runtime, but we are taking the example of [tokio]

 #[tokio::main]
 async fn main() -> Result<(), Box<dyn std::error::Error>> {
     use minus::{Pager, LineNumbers, tokio_updating};
     let mut pager = Pager::new().unwrap();
     pager.set_line_numbers(LineNumbers::AlwaysOn);
     pager.set_prompt("A complex configuration");

     // Normally, you would use `futures::join` to join the pager and the text
     // updating function. We are doing this here to make the example simple
     tokio_updating(pager.finish()).await?;
     Ok(())
 }

For static output

 fn main() -> Result<(), Box<dyn std::error::Error>> {
      let mut pager = minus::Pager::new().unwrap();
      pager.set_text("Hello");
      pager.set_prompt("Example");
      minus::page_all(pager)?;
      Ok(())
 }

Implementations

Initialize a new pager configuration

Errors

This function will return an error if it cannot determine the terminal size

Example

let pager = minus::Pager::new().unwrap();

Set the output text to this t

Note that unlike Pager::push_str, this replaces the original text. If you want to append text, use the Pager::push_str function

Example

let mut pager = minus::Pager::new().unwrap();
pager.set_text("This is a line");

Set line number to this setting

Example

use minus::{Pager, LineNumbers};

let mut pager = Pager::new().unwrap();
pager.set_line_numbers(LineNumbers::Enabled);

Set the prompt displayed at the prompt to t

Example

use minus::Pager;

let mut pager = Pager::new().unwrap();
pager.set_prompt("my awesome program");

Return a PagerMutex from this Pager. This is gated on tokio_lib or async_std_lib feature

Example

use minus::Pager;

let mut pager = Pager::new().unwrap();
pager.set_text("This output is paged");
let _pager_mutex = pager.finish();

Set the default exit strategy.

This controls how the pager will behave when the user presses q or Ctrl+C. See ExitStrategy for available options

use minus::{Pager, ExitStrategy};

let mut pager = Pager::new().unwrap();
pager.set_exit_strategy(ExitStrategy::ProcessQuit);

Set whether to display pager if there’s less data than available screen height

By default this is set to false

use minus::Pager;

let mut pager = Pager::new().unwrap();
pager.set_run_no_overflow(true);

Appends text to the pager output

This function will automatically split the lines, if they overflow the number of terminal columns

let mut pager = minus::Pager::new().unwrap();
pager.push_str("This is some text");

Tells the running pager that no more data is coming

Note that after this function is called, any call to Pager::set_text() or Pager::push_str() will panic

Example

use minus::Pager;

let mut pager = Pager::new().unwrap();
pager.set_text("Hello from minus!");
pager.end_data_stream();

Set custom input handler function

See example in InputHandler on using this function

Run the exit callbacks

Example

use minus::Pager;

fn hello() {
    println!("Hello");
}

let mut pager = Pager::new().unwrap();
pager.add_exit_callback(Box::new(hello));
pager.exit()

Example

use minus::Pager;

fn hello() {
    println!("Hello");
}

let mut pager = Pager::new().unwrap();
pager.add_exit_callback(Box::new(hello));

Trait Implementations

Returns the “default value” for a type. Read more

Write a buffer into this writer, returning how many bytes were written. Read more

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more

Like write, except that it writes from a slice of buffers. Read more

🔬 This is a nightly-only experimental API. (can_vector)

Determines if this Writer has an efficient write_vectored implementation. Read more

Attempts to write an entire buffer into this writer. Read more

🔬 This is a nightly-only experimental API. (write_all_vectored)

Attempts to write multiple buffers into this writer. Read more

Writes a formatted string into this writer, returning any error encountered. Read more

Creates a “by reference” adaptor for this instance of Write. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Executes the given command directly.

The given command its ANSI escape code will be written and flushed onto Self.

Arguments

  • Command

    The command that you want to execute directly.

Example

use std::io::{Write, stdout};

use crossterm::{Result, ExecutableCommand, style::Print};

 fn main() -> Result<()> {
     // will be executed directly
      stdout()
        .execute(Print("sum:\n".to_string()))?
        .execute(Print(format!("1 + 1= {} ", 1 + 1)))?;

      Ok(())

     // ==== Output ====
     // sum:
     // 1 + 1 = 2
 }

Have a look over at the Command API for more details.

Notes

  • In the case of UNIX and Windows 10, ANSI codes are written to the given ‘writer’.
  • In case of Windows versions lower than 10, a direct WinAPI call will be made. The reason for this is that Windows versions lower than 10 do not support ANSI codes, and can therefore not be written to the given writer. Therefore, there is no difference between execute and queue for those old Windows versions.

Performs the conversion.

Performs the conversion.

Queues the given command for further execution.

Queued commands will be executed in the following cases:

  • When flush is called manually on the given type implementing io::Write.
  • The terminal will flush automatically if the buffer is full.
  • Each line is flushed in case of stdout, because it is line buffered.

Arguments

  • Command

    The command that you want to queue for later execution.

Examples

use std::io::{Write, stdout};

use crossterm::{Result, QueueableCommand, style::Print};

 fn main() -> Result<()> {
    let mut stdout = stdout();

    // `Print` will executed executed when `flush` is called.
    stdout
        .queue(Print("foo 1\n".to_string()))?
        .queue(Print("foo 2".to_string()))?;

    // some other code (no execution happening here) ...

    // when calling `flush` on `stdout`, all commands will be written to the stdout and therefore executed.
    stdout.flush()?;

    Ok(())

    // ==== Output ====
    // foo 1
    // foo 2
}

Have a look over at the Command API for more details.

Notes

  • In the case of UNIX and Windows 10, ANSI codes are written to the given ‘writer’.
  • In case of Windows versions lower than 10, a direct WinAPI call will be made. The reason for this is that Windows versions lower than 10 do not support ANSI codes, and can therefore not be written to the given writer. Therefore, there is no difference between execute and queue for those old Windows versions.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.