Skip to content
Open
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
24 changes: 21 additions & 3 deletions e2e/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use which::which;

pub static SK: &str = "SKIM_DEFAULT_OPTIONS= SKIM_DEFAULT_COMMAND= cargo run --package skim --release --";

const TIMEOUT: usize = 30; // seconds

pub fn sk(outfile: &str, opts: &[&str]) -> String {
format!(
"{} {} > {}.part; mv {}.part {}",
Expand All @@ -29,7 +31,7 @@ fn wait<F, T>(pred: F) -> Result<T>
where
F: Fn() -> Result<T>,
{
for _ in 1..2000 {
for _ in 1..(TIMEOUT * 20) {
if let Ok(t) = pred() {
return Ok(t);
}
Expand All @@ -49,6 +51,8 @@ pub enum Keys<'a> {
Left,
Right,
BSpace,
Up,
Down,
}

impl Display for Keys<'_> {
Expand All @@ -64,6 +68,8 @@ impl Display for Keys<'_> {
BTab => write!(f, "BTab"),
Left => write!(f, "Left"),
Right => write!(f, "Right"),
Up => write!(f, "Up"),
Down => write!(f, "Down"),
BSpace => write!(f, "BSpace"),
}
}
Expand Down Expand Up @@ -130,10 +136,15 @@ impl TmuxController {
}

// Returns the lines in reverted order
pub fn capture(&self) -> Result<Vec<String>> {
fn _capture(&self, capture_ansi: bool) -> Result<Vec<String>> {
let tempfile = wait(|| {
let tempfile = self.tempfile()?;
Self::run(&["capture-pane", "-b", &self.window, "-t", &format!("{}.0", self.window)])?;
let win_str = format!("{}.0", self.window);
let mut capture_cmd = vec!["capture-pane", "-b", &self.window, "-t", &win_str];
if capture_ansi {
capture_cmd.push("-e")
}
Self::run(&capture_cmd)?;
Self::run(&["save-buffer", "-b", &self.window, &tempfile])?;
Ok(tempfile)
})?;
Expand All @@ -151,6 +162,13 @@ impl TmuxController {
.collect())
}

pub fn capture(&self) -> Result<Vec<String>> {
self._capture(false)
}
pub fn capture_ansi(&self) -> Result<Vec<String>> {
self._capture(true)
}

pub fn until<F>(&self, pred: F) -> Result<()>
where
F: Fn(&[String]) -> bool,
Expand Down
24 changes: 24 additions & 0 deletions e2e/tests/ansi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use e2e::SK;
use e2e::TmuxController;
use std::io::Result;

#[test]
pub fn reset_after_exit_height() -> Result<()> {
let tmux = TmuxController::new()?;
let _ = tmux.send_keys(&[
e2e::Keys::Str(&format!("echo -e '1\n2' | {SK} --height 10")),
e2e::Keys::Enter,
])?;
tmux.until(|l| l.len() > 0 && l[0] == ">")?;
tmux.send_keys(&[e2e::Keys::Up])?;
tmux.until(|l| l.len() > 3 && l[3] == "> 2")?;
tmux.send_keys(&[e2e::Keys::Enter])?;
tmux.until(|l| l[0].contains("$"))?;
let lines = tmux.capture_ansi()?;
println!("Lines: {:?}", lines);

// Ensure we properly reset the color after exiting, at least the foreground color
assert!(lines[1].contains("\u{1b}[39m2") || lines[1].contains("\u{1b}[0m2"));

Ok(())
}
1 change: 1 addition & 0 deletions skim-tuikit/src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ impl TermLock {
output.write("\n");
}
}
output.reset_attributes();
output.flush();
}
Ok(())
Expand Down
28 changes: 28 additions & 0 deletions skim/examples/default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
extern crate skim;
use skim::prelude::*;
use std::io::Cursor;

pub fn main() {
let options = SkimOptionsBuilder::default()
.height("50%".to_string())
.multi(true)
.build()
.unwrap();

let input = "aaaaa\nbbbb\nccc".to_string();

// `SkimItemReader` is a helper to turn any `BufRead` into a stream of `SkimItem`
// `SkimItem` was implemented for `AsRef<str>` by default
let item_reader = SkimItemReader::default();
let items = item_reader.of_bufread(Cursor::new(input));

// `run_with` would read and show items from the stream
let selected_items = Skim::run_with(&options, Some(items))
.map(|out| out.selected_items)
.unwrap_or_else(|| Vec::new());

println!("{}", "\n".repeat(6));
for item in selected_items.iter() {
println!("{:?}", item.output().chars().collect::<Vec<char>>());
}
}
Loading