Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a library interface #11

Merged
merged 7 commits into from
Jan 5, 2020
Merged
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
202 changes: 120 additions & 82 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ name = "sp"
path = "src/main.rs"

[dependencies]
anyhow = "1.0.20"
bit-set = "0.5.1"
clap = { version = "2.32.0", features = ["wrap_help"] }
failure = "0.1.5"
lazy_static = "1.3.0"
lru-cache = "0.1.2"
memmap = "0.7.0"
regex = "1.1.5"
scopeguard = "1.0.0"
smallvec = "0.6.9"
terminfo = "0.6"
termwiz = "0.3.1"
termwiz = "0.5"
unicode-segmentation = "1.2.1"
unicode-width = "0.1.5"
vec_map = "0.8.1"

[build-dependencies]
clap = "2.32.0"

[dev-dependencies]
pipe = "0.2.0"
52 changes: 52 additions & 0 deletions examples/streams_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Run this with: cargo run --example streams_example

use pipe::pipe;
use std::io::Write;
use std::thread::{sleep, spawn};
use std::time::Duration;
use streampager::{Pager, Result};

fn main() -> Result<()> {
let (out_read, mut out_write) = pipe();
let (err_read, mut err_write) = pipe();
let (prog_read, mut prog_write) = pipe();

spawn(move || {
for i in 1..=100 {
let _ = out_write.write_all(format!("this is line {}\n", i).as_bytes());
sleep(Duration::from_millis(450));
}
let _ = out_write.write_all(b"this is the end of output stream\n");
});

spawn(move || {
for i in 1..=10 {
let _ = err_write.write_all(format!("this is error line {}\n", i).as_bytes());
sleep(Duration::from_millis(4000));
}
let _ = err_write.write_all(b"this is the end of error stream\n");
});

spawn(move || {
let mut extra: &[u8] = b"";
for i in 1..=100 {
let _ = prog_write.write_all(format!("progress step {}\n", i).as_bytes());
let _ = prog_write.write_all(extra);
let _ = prog_write.write_all(b"\x0c");
if i == 40 {
extra = b"\x1b[32mprogress can have colors and multiple lines\n";
}
sleep(Duration::from_millis(120));
}
});

let mut pager = Pager::new_using_system_terminal()?;
pager
.add_output_stream(out_read, "output stream")?
.add_error_stream(err_read, "error stream")?
.set_progress_stream(prog_read);
pager.run()?;

println!("pager has exited");
Ok(())
}
2 changes: 1 addition & 1 deletion src/command.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Commands
//!
//! Commands the user can invoke.
use failure::Error;
use anyhow::Error;

use crate::display::Action;
use crate::event::EventSender;
Expand Down
4 changes: 2 additions & 2 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Manage the Display.
use failure::Error;
use anyhow::Error;
use scopeguard::guard;
use std::time::Duration;
use termwiz::caps::Capabilities as TermCapabilities;
Expand Down Expand Up @@ -45,7 +45,7 @@ impl Capabilities {
/// An action that affects the display.
pub(crate) enum Action {
/// Run a function. The function may return a new action to run next.
Run(Box<FnMut(&mut Screen) -> Result<Option<Action>, Error>>),
Run(Box<dyn FnMut(&mut Screen) -> Result<Option<Action>, Error>>),

/// Change the terminal.
Change(Change),
Expand Down
4 changes: 2 additions & 2 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Events.
use failure::Error;
use anyhow::Error;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc;
use std::sync::Arc;
Expand Down Expand Up @@ -97,7 +97,7 @@ impl EventStream {
/// Get an event, either from the event stream or from the terminal.
pub(crate) fn get(
&self,
term: &mut Terminal,
term: &mut dyn Terminal,
wait: Option<Duration>,
) -> Result<Option<Event>, Error> {
// First, try to get an event from the queue.
Expand Down
18 changes: 14 additions & 4 deletions src/file.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Files.
use failure::{Error, ResultExt};
use anyhow::{Context, Error};
use memmap::Mmap;
use std::borrow::Cow;
use std::cmp::min;
Expand Down Expand Up @@ -252,17 +252,27 @@ pub(crate) struct File {
}

impl File {
/// Load stdin.
pub(crate) fn new_stdin(
/// Load stream.
pub(crate) fn new_streamed(
index: usize,
stream: impl Read + Send + 'static,
title: &str,
event_sender: EventSender,
) -> Result<File, Error> {
let meta = Arc::new(FileMeta::new(index, title.to_string()));
let data = FileData::new_streamed(std::io::stdin(), meta.clone(), event_sender)?;
let data = FileData::new_streamed(stream, meta.clone(), event_sender)?;
Ok(File { data, meta })
}

/// Load stdin.
pub(crate) fn new_stdin(
index: usize,
title: &str,
event_sender: EventSender,
) -> Result<File, Error> {
Self::new_streamed(index, std::io::stdin(), title, event_sender)
}

/// Load an input fd
pub(crate) fn new_fd(
index: usize,
Expand Down
Loading