blob: 62ba75dfb17be897d88fc16541007989001411b1 (
plain)
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
|
use std::io;
use crate::seqfile;
use crate::cursorfile;
use crate::khline::{KhLine,lines_to_khlines,khlines_to_events};
use crate::utils::stdioutils;
use crate::selectors::SelectFilters;
use crate::khevent::KhEvent;
pub fn selection(args: &[&str]) -> Result<Box<dyn Iterator<Item = KhEvent>>, String> {
if args.is_empty() {
let khlines = default_input_khlines()?;
let events = khlines_to_events(khlines);
return Ok(Box::new(events));
}
let filters = SelectFilters::parse_from_args_with_range(args)?;
let khlines = input_khlines_seq()?;
let events = filters.filter_khlines(khlines);
Ok(Box::new(events))
}
fn input_khlines_stdin() -> impl Iterator<Item = KhLine> {
let lines = stdioutils::read_lines_from_stdin().unwrap().into_iter();
lines_to_khlines(lines)
}
fn input_khlines_seq() -> Result<impl Iterator<Item = KhLine>, String> {
let lines = seqfile::read_seqfile().map_err(|_| "Invalid input".to_string())?;
Ok(lines_to_khlines(lines))
}
pub fn default_input_khlines() -> Result<Box<dyn Iterator<Item = KhLine>>, String> {
let khlines: Box<dyn Iterator<Item = KhLine>> = if !stdioutils::is_stdin_tty() {
debug!("Taking input from Stdin");
Box::new(input_khlines_stdin())
} else {
Box::new(input_khlines_seq()?)
};
Ok(khlines)
}
pub fn default_input_khline() -> io::Result<KhLine> {
if !stdioutils::is_stdin_tty() {
debug!("Taking input from Stdin");
let lines = stdioutils::read_lines_from_stdin()?;
if lines.len() > 1 {
Err(io::Error::new(io::ErrorKind::Other, "too many lines in input"))
} else {
lines[0].parse::<KhLine>().map_err(|err| io::Error::new(io::ErrorKind::Other, err.to_string()))
}
} else {
cursorfile::read_cursorfile()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::utils::stdioutils;
#[test]
fn test_default_input_khline() {
stdioutils::test_stdin_write("a\nb\n");
assert!( default_input_khline().is_err());
}
}
|