1
2
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#[macro_use]
extern crate log;
#[macro_use]
extern crate lazy_static;
mod ansi;
mod event;
mod field;
mod header;
mod input;
mod item;
mod matcher;
mod model;
mod options;
mod orderedvec;
mod output;
mod previewer;
mod query;
mod reader;
mod score;
mod selection;
mod spinlock;
mod theme;
mod util;
use crate::event::Event::*;
use crate::event::{EventReceiver, EventSender};
use crate::model::Model;
pub use crate::options::{SkimOptions, SkimOptionsBuilder};
pub use crate::output::SkimOutput;
use crate::reader::Reader;
use nix::unistd::isatty;
use std::io::BufRead;
use std::io::BufReader;
use std::os::unix::io::AsRawFd;
use std::sync::mpsc::channel;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use tuikit::term::{Term, TermHeight, TermOptions};
const REFRESH_DURATION: u64 = 25;
pub struct Skim {}
impl Skim {
pub fn run_with(options: &SkimOptions, source: Option<Box<BufRead + Send>>) -> Option<SkimOutput> {
let min_height = options
.min_height
.map(Skim::parse_height_string)
.expect("min_height should have default values");
let height = options
.height
.map(Skim::parse_height_string)
.expect("height should have default values");
let (tx, rx): (EventSender, EventReceiver) = channel();
let term = Arc::new(Term::with_options(TermOptions::default().min_height(min_height).height(height)).unwrap());
let mut input = input::Input::new(term.clone());
input.parse_keymaps(&options.bind);
input.parse_expect_keys(options.expect.as_ref().map(|x| &**x));
let tx_clone = tx.clone();
thread::spawn(move || loop {
let (ev, arg) = input.pool_event();
let _ = tx_clone.send((ev, arg));
if ev == EvActAccept || ev == EvActAbort {
break;
}
});
let source = source.or_else(|| {
let stdin = std::io::stdin();
if !isatty(stdin.as_raw_fd()).unwrap_or(true) {
Some(Box::new(BufReader::new(stdin)))
} else {
None
}
});
let reader = Reader::with_options(&options).source(source);
let tx_clone = tx.clone();
thread::spawn(move || loop {
let _ = tx_clone.send((EvHeartBeat, Box::new(true)));
thread::sleep(Duration::from_millis(REFRESH_DURATION));
});
let mut model = Model::new(rx, tx, reader, term, &options);
model.start()
}
fn parse_height_string(string: &str) -> TermHeight {
if string.ends_with('%') {
TermHeight::Percent(string[0..string.len() - 1].parse().unwrap_or(100))
} else {
TermHeight::Fixed(string.parse().unwrap_or(0))
}
}
}