Struct cdx::LookbackReader [−][src]
pub struct LookbackReader {
pub cont: InfileContext,
pub do_split: bool,
// some fields omitted
}Expand description
File reader for text file broken into lines with columns previous N lines are stil available
Fields
cont: InfileContextcontext
do_split: boolAutomatically split each line into columns
Implementations
make a new LookbackReader
Examples found in repository
src/bin/cdx/uniq_main.rs (line 23)
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::One);
const A: [ArgSpec; 1] = [arg! {"key", "k", "Spec", "How to compare adjacent lines"}];
let (args, files) = args::parse(&prog, &A, argv);
let mut comp = LineCompList::new();
for x in args {
if x.name == "key" {
comp.add(&x.value)?;
} else {
unreachable!();
}
}
assert_eq!(files.len(), 1);
let mut f = LookbackReader::new(1);
f.open(&files[0])?;
if f.is_empty() {
return Ok(());
}
let mut w = get_writer("-")?;
f.write_header(&mut w)?;
if f.is_done() {
return Ok(());
}
f.write_curr(&mut w)?;
comp.lookup(&f.names())?;
f.do_split = comp.need_split();
loop {
if f.getline()? {
break;
}
if !comp.equal_cols(f.prev_line(1), f.curr_line()) {
f.write_curr(&mut w)?;
}
}
Ok(())
}More examples
src/bin/cdx/cgrep_main.rs (line 42)
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::Many);
const A: [ArgSpec; 4] = [
arg! {"pattern", "p", "Col,Spec,Pattern", "Select line where this col matches this pattern."},
arg! {"show-matchers", "s", "", "Print available matchers"},
arg! {"or", "o", "", "A line matches if any of the matchers matches."},
arg_enum! {"header", "h", "Mode", "header requirements", &HEADER_MODE},
];
let (args, files) = args::parse(&prog, &A, argv);
let mut checker = HeaderChecker::new();
let mut list = MultiLineMatcher::new(MultiMode::And);
for x in args {
if x.name == "header" {
checker.mode = HeaderMode::from_str(&x.value)?;
} else if x.name == "pattern" {
list.push(Box::new(ColMatcher::new(&x.value)?));
} else if x.name == "show-matchers" {
MatchMaker::help();
return Ok(());
} else if x.name == "or" {
list.multi = MultiMode::Or;
} else {
unreachable!();
}
}
let mut w = get_writer("-")?;
let grep_mode = true;
let reverse = false;
let max_fails = 5;
let mut first_file = true;
for x in &files {
let mut f = LookbackReader::new(1);
f.open(x)?;
if f.is_empty() {
continue;
}
if first_file {
first_file = false;
list.lookup(&f.names())?;
}
if checker.check_file(&f.cont, &files[0])? {
f.write_header(&mut w)?;
}
if f.is_done() {
continue;
}
if grep_mode {
loop {
if list.ok(f.curr_line()) ^ reverse {
// write previous lines of context if necessary
f.write_curr(&mut w)?;
} else {
// write more lines of context if necessary
}
if f.getline()? {
break;
}
}
} else {
let mut fails = 0;
loop {
if !list.ok_verbose(f.curr_line(), false) {
fails += 1;
if fails >= max_fails {
break;
}
}
if f.getline()? {
break;
}
}
if fails > 0 {
return Err(Error::Silent);
}
}
}
Ok(())
}get column names
Examples found in repository
src/bin/cdx/uniq_main.rs (line 37)
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::One);
const A: [ArgSpec; 1] = [arg! {"key", "k", "Spec", "How to compare adjacent lines"}];
let (args, files) = args::parse(&prog, &A, argv);
let mut comp = LineCompList::new();
for x in args {
if x.name == "key" {
comp.add(&x.value)?;
} else {
unreachable!();
}
}
assert_eq!(files.len(), 1);
let mut f = LookbackReader::new(1);
f.open(&files[0])?;
if f.is_empty() {
return Ok(());
}
let mut w = get_writer("-")?;
f.write_header(&mut w)?;
if f.is_done() {
return Ok(());
}
f.write_curr(&mut w)?;
comp.lookup(&f.names())?;
f.do_split = comp.need_split();
loop {
if f.getline()? {
break;
}
if !comp.equal_cols(f.prev_line(1), f.curr_line()) {
f.write_curr(&mut w)?;
}
}
Ok(())
}More examples
src/bin/cdx/cgrep_main.rs (line 49)
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::Many);
const A: [ArgSpec; 4] = [
arg! {"pattern", "p", "Col,Spec,Pattern", "Select line where this col matches this pattern."},
arg! {"show-matchers", "s", "", "Print available matchers"},
arg! {"or", "o", "", "A line matches if any of the matchers matches."},
arg_enum! {"header", "h", "Mode", "header requirements", &HEADER_MODE},
];
let (args, files) = args::parse(&prog, &A, argv);
let mut checker = HeaderChecker::new();
let mut list = MultiLineMatcher::new(MultiMode::And);
for x in args {
if x.name == "header" {
checker.mode = HeaderMode::from_str(&x.value)?;
} else if x.name == "pattern" {
list.push(Box::new(ColMatcher::new(&x.value)?));
} else if x.name == "show-matchers" {
MatchMaker::help();
return Ok(());
} else if x.name == "or" {
list.multi = MultiMode::Or;
} else {
unreachable!();
}
}
let mut w = get_writer("-")?;
let grep_mode = true;
let reverse = false;
let max_fails = 5;
let mut first_file = true;
for x in &files {
let mut f = LookbackReader::new(1);
f.open(x)?;
if f.is_empty() {
continue;
}
if first_file {
first_file = false;
list.lookup(&f.names())?;
}
if checker.check_file(&f.cont, &files[0])? {
f.write_header(&mut w)?;
}
if f.is_done() {
continue;
}
if grep_mode {
loop {
if list.ok(f.curr_line()) ^ reverse {
// write previous lines of context if necessary
f.write_curr(&mut w)?;
} else {
// write more lines of context if necessary
}
if f.getline()? {
break;
}
}
} else {
let mut fails = 0;
loop {
if !list.ok_verbose(f.curr_line(), false) {
fails += 1;
if fails >= max_fails {
break;
}
}
if f.getline()? {
break;
}
}
if fails > 0 {
return Err(Error::Silent);
}
}
}
Ok(())
}open file for reading
Examples found in repository
src/bin/cdx/uniq_main.rs (line 24)
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::One);
const A: [ArgSpec; 1] = [arg! {"key", "k", "Spec", "How to compare adjacent lines"}];
let (args, files) = args::parse(&prog, &A, argv);
let mut comp = LineCompList::new();
for x in args {
if x.name == "key" {
comp.add(&x.value)?;
} else {
unreachable!();
}
}
assert_eq!(files.len(), 1);
let mut f = LookbackReader::new(1);
f.open(&files[0])?;
if f.is_empty() {
return Ok(());
}
let mut w = get_writer("-")?;
f.write_header(&mut w)?;
if f.is_done() {
return Ok(());
}
f.write_curr(&mut w)?;
comp.lookup(&f.names())?;
f.do_split = comp.need_split();
loop {
if f.getline()? {
break;
}
if !comp.equal_cols(f.prev_line(1), f.curr_line()) {
f.write_curr(&mut w)?;
}
}
Ok(())
}More examples
src/bin/cdx/cgrep_main.rs (line 43)
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::Many);
const A: [ArgSpec; 4] = [
arg! {"pattern", "p", "Col,Spec,Pattern", "Select line where this col matches this pattern."},
arg! {"show-matchers", "s", "", "Print available matchers"},
arg! {"or", "o", "", "A line matches if any of the matchers matches."},
arg_enum! {"header", "h", "Mode", "header requirements", &HEADER_MODE},
];
let (args, files) = args::parse(&prog, &A, argv);
let mut checker = HeaderChecker::new();
let mut list = MultiLineMatcher::new(MultiMode::And);
for x in args {
if x.name == "header" {
checker.mode = HeaderMode::from_str(&x.value)?;
} else if x.name == "pattern" {
list.push(Box::new(ColMatcher::new(&x.value)?));
} else if x.name == "show-matchers" {
MatchMaker::help();
return Ok(());
} else if x.name == "or" {
list.multi = MultiMode::Or;
} else {
unreachable!();
}
}
let mut w = get_writer("-")?;
let grep_mode = true;
let reverse = false;
let max_fails = 5;
let mut first_file = true;
for x in &files {
let mut f = LookbackReader::new(1);
f.open(x)?;
if f.is_empty() {
continue;
}
if first_file {
first_file = false;
list.lookup(&f.names())?;
}
if checker.check_file(&f.cont, &files[0])? {
f.write_header(&mut w)?;
}
if f.is_done() {
continue;
}
if grep_mode {
loop {
if list.ok(f.curr_line()) ^ reverse {
// write previous lines of context if necessary
f.write_curr(&mut w)?;
} else {
// write more lines of context if necessary
}
if f.getline()? {
break;
}
}
} else {
let mut fails = 0;
loop {
if !list.ok_verbose(f.curr_line(), false) {
fails += 1;
if fails >= max_fails {
break;
}
}
if f.getline()? {
break;
}
}
if fails > 0 {
return Err(Error::Silent);
}
}
}
Ok(())
}was file zero bytes?
Examples found in repository
src/bin/cdx/uniq_main.rs (line 25)
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::One);
const A: [ArgSpec; 1] = [arg! {"key", "k", "Spec", "How to compare adjacent lines"}];
let (args, files) = args::parse(&prog, &A, argv);
let mut comp = LineCompList::new();
for x in args {
if x.name == "key" {
comp.add(&x.value)?;
} else {
unreachable!();
}
}
assert_eq!(files.len(), 1);
let mut f = LookbackReader::new(1);
f.open(&files[0])?;
if f.is_empty() {
return Ok(());
}
let mut w = get_writer("-")?;
f.write_header(&mut w)?;
if f.is_done() {
return Ok(());
}
f.write_curr(&mut w)?;
comp.lookup(&f.names())?;
f.do_split = comp.need_split();
loop {
if f.getline()? {
break;
}
if !comp.equal_cols(f.prev_line(1), f.curr_line()) {
f.write_curr(&mut w)?;
}
}
Ok(())
}More examples
src/bin/cdx/cgrep_main.rs (line 44)
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::Many);
const A: [ArgSpec; 4] = [
arg! {"pattern", "p", "Col,Spec,Pattern", "Select line where this col matches this pattern."},
arg! {"show-matchers", "s", "", "Print available matchers"},
arg! {"or", "o", "", "A line matches if any of the matchers matches."},
arg_enum! {"header", "h", "Mode", "header requirements", &HEADER_MODE},
];
let (args, files) = args::parse(&prog, &A, argv);
let mut checker = HeaderChecker::new();
let mut list = MultiLineMatcher::new(MultiMode::And);
for x in args {
if x.name == "header" {
checker.mode = HeaderMode::from_str(&x.value)?;
} else if x.name == "pattern" {
list.push(Box::new(ColMatcher::new(&x.value)?));
} else if x.name == "show-matchers" {
MatchMaker::help();
return Ok(());
} else if x.name == "or" {
list.multi = MultiMode::Or;
} else {
unreachable!();
}
}
let mut w = get_writer("-")?;
let grep_mode = true;
let reverse = false;
let max_fails = 5;
let mut first_file = true;
for x in &files {
let mut f = LookbackReader::new(1);
f.open(x)?;
if f.is_empty() {
continue;
}
if first_file {
first_file = false;
list.lookup(&f.names())?;
}
if checker.check_file(&f.cont, &files[0])? {
f.write_header(&mut w)?;
}
if f.is_done() {
continue;
}
if grep_mode {
loop {
if list.ok(f.curr_line()) ^ reverse {
// write previous lines of context if necessary
f.write_curr(&mut w)?;
} else {
// write more lines of context if necessary
}
if f.getline()? {
break;
}
}
} else {
let mut fails = 0;
loop {
if !list.ok_verbose(f.curr_line(), false) {
fails += 1;
if fails >= max_fails {
break;
}
}
if f.getline()? {
break;
}
}
if fails > 0 {
return Err(Error::Silent);
}
}
}
Ok(())
}have we hit EOF?
Examples found in repository
src/bin/cdx/uniq_main.rs (line 32)
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::One);
const A: [ArgSpec; 1] = [arg! {"key", "k", "Spec", "How to compare adjacent lines"}];
let (args, files) = args::parse(&prog, &A, argv);
let mut comp = LineCompList::new();
for x in args {
if x.name == "key" {
comp.add(&x.value)?;
} else {
unreachable!();
}
}
assert_eq!(files.len(), 1);
let mut f = LookbackReader::new(1);
f.open(&files[0])?;
if f.is_empty() {
return Ok(());
}
let mut w = get_writer("-")?;
f.write_header(&mut w)?;
if f.is_done() {
return Ok(());
}
f.write_curr(&mut w)?;
comp.lookup(&f.names())?;
f.do_split = comp.need_split();
loop {
if f.getline()? {
break;
}
if !comp.equal_cols(f.prev_line(1), f.curr_line()) {
f.write_curr(&mut w)?;
}
}
Ok(())
}More examples
src/bin/cdx/cgrep_main.rs (line 54)
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::Many);
const A: [ArgSpec; 4] = [
arg! {"pattern", "p", "Col,Spec,Pattern", "Select line where this col matches this pattern."},
arg! {"show-matchers", "s", "", "Print available matchers"},
arg! {"or", "o", "", "A line matches if any of the matchers matches."},
arg_enum! {"header", "h", "Mode", "header requirements", &HEADER_MODE},
];
let (args, files) = args::parse(&prog, &A, argv);
let mut checker = HeaderChecker::new();
let mut list = MultiLineMatcher::new(MultiMode::And);
for x in args {
if x.name == "header" {
checker.mode = HeaderMode::from_str(&x.value)?;
} else if x.name == "pattern" {
list.push(Box::new(ColMatcher::new(&x.value)?));
} else if x.name == "show-matchers" {
MatchMaker::help();
return Ok(());
} else if x.name == "or" {
list.multi = MultiMode::Or;
} else {
unreachable!();
}
}
let mut w = get_writer("-")?;
let grep_mode = true;
let reverse = false;
let max_fails = 5;
let mut first_file = true;
for x in &files {
let mut f = LookbackReader::new(1);
f.open(x)?;
if f.is_empty() {
continue;
}
if first_file {
first_file = false;
list.lookup(&f.names())?;
}
if checker.check_file(&f.cont, &files[0])? {
f.write_header(&mut w)?;
}
if f.is_done() {
continue;
}
if grep_mode {
loop {
if list.ok(f.curr_line()) ^ reverse {
// write previous lines of context if necessary
f.write_curr(&mut w)?;
} else {
// write more lines of context if necessary
}
if f.getline()? {
break;
}
}
} else {
let mut fails = 0;
loop {
if !list.ok_verbose(f.curr_line(), false) {
fails += 1;
if fails >= max_fails {
break;
}
}
if f.getline()? {
break;
}
}
if fails > 0 {
return Err(Error::Silent);
}
}
}
Ok(())
}get next line of text
Examples found in repository
src/bin/cdx/uniq_main.rs (line 41)
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::One);
const A: [ArgSpec; 1] = [arg! {"key", "k", "Spec", "How to compare adjacent lines"}];
let (args, files) = args::parse(&prog, &A, argv);
let mut comp = LineCompList::new();
for x in args {
if x.name == "key" {
comp.add(&x.value)?;
} else {
unreachable!();
}
}
assert_eq!(files.len(), 1);
let mut f = LookbackReader::new(1);
f.open(&files[0])?;
if f.is_empty() {
return Ok(());
}
let mut w = get_writer("-")?;
f.write_header(&mut w)?;
if f.is_done() {
return Ok(());
}
f.write_curr(&mut w)?;
comp.lookup(&f.names())?;
f.do_split = comp.need_split();
loop {
if f.getline()? {
break;
}
if !comp.equal_cols(f.prev_line(1), f.curr_line()) {
f.write_curr(&mut w)?;
}
}
Ok(())
}More examples
src/bin/cdx/cgrep_main.rs (line 65)
≺ ≻
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::Many);
const A: [ArgSpec; 4] = [
arg! {"pattern", "p", "Col,Spec,Pattern", "Select line where this col matches this pattern."},
arg! {"show-matchers", "s", "", "Print available matchers"},
arg! {"or", "o", "", "A line matches if any of the matchers matches."},
arg_enum! {"header", "h", "Mode", "header requirements", &HEADER_MODE},
];
let (args, files) = args::parse(&prog, &A, argv);
let mut checker = HeaderChecker::new();
let mut list = MultiLineMatcher::new(MultiMode::And);
for x in args {
if x.name == "header" {
checker.mode = HeaderMode::from_str(&x.value)?;
} else if x.name == "pattern" {
list.push(Box::new(ColMatcher::new(&x.value)?));
} else if x.name == "show-matchers" {
MatchMaker::help();
return Ok(());
} else if x.name == "or" {
list.multi = MultiMode::Or;
} else {
unreachable!();
}
}
let mut w = get_writer("-")?;
let grep_mode = true;
let reverse = false;
let max_fails = 5;
let mut first_file = true;
for x in &files {
let mut f = LookbackReader::new(1);
f.open(x)?;
if f.is_empty() {
continue;
}
if first_file {
first_file = false;
list.lookup(&f.names())?;
}
if checker.check_file(&f.cont, &files[0])? {
f.write_header(&mut w)?;
}
if f.is_done() {
continue;
}
if grep_mode {
loop {
if list.ok(f.curr_line()) ^ reverse {
// write previous lines of context if necessary
f.write_curr(&mut w)?;
} else {
// write more lines of context if necessary
}
if f.getline()? {
break;
}
}
} else {
let mut fails = 0;
loop {
if !list.ok_verbose(f.curr_line(), false) {
fails += 1;
if fails >= max_fails {
break;
}
}
if f.getline()? {
break;
}
}
if fails > 0 {
return Err(Error::Silent);
}
}
}
Ok(())
}get current line of text
Examples found in repository
More examples
src/bin/cdx/uniq_main.rs (line 44)
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::One);
const A: [ArgSpec; 1] = [arg! {"key", "k", "Spec", "How to compare adjacent lines"}];
let (args, files) = args::parse(&prog, &A, argv);
let mut comp = LineCompList::new();
for x in args {
if x.name == "key" {
comp.add(&x.value)?;
} else {
unreachable!();
}
}
assert_eq!(files.len(), 1);
let mut f = LookbackReader::new(1);
f.open(&files[0])?;
if f.is_empty() {
return Ok(());
}
let mut w = get_writer("-")?;
f.write_header(&mut w)?;
if f.is_done() {
return Ok(());
}
f.write_curr(&mut w)?;
comp.lookup(&f.names())?;
f.do_split = comp.need_split();
loop {
if f.getline()? {
break;
}
if !comp.equal_cols(f.prev_line(1), f.curr_line()) {
f.write_curr(&mut w)?;
}
}
Ok(())
}src/bin/cdx/cgrep_main.rs (line 59)
≺ ≻
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::Many);
const A: [ArgSpec; 4] = [
arg! {"pattern", "p", "Col,Spec,Pattern", "Select line where this col matches this pattern."},
arg! {"show-matchers", "s", "", "Print available matchers"},
arg! {"or", "o", "", "A line matches if any of the matchers matches."},
arg_enum! {"header", "h", "Mode", "header requirements", &HEADER_MODE},
];
let (args, files) = args::parse(&prog, &A, argv);
let mut checker = HeaderChecker::new();
let mut list = MultiLineMatcher::new(MultiMode::And);
for x in args {
if x.name == "header" {
checker.mode = HeaderMode::from_str(&x.value)?;
} else if x.name == "pattern" {
list.push(Box::new(ColMatcher::new(&x.value)?));
} else if x.name == "show-matchers" {
MatchMaker::help();
return Ok(());
} else if x.name == "or" {
list.multi = MultiMode::Or;
} else {
unreachable!();
}
}
let mut w = get_writer("-")?;
let grep_mode = true;
let reverse = false;
let max_fails = 5;
let mut first_file = true;
for x in &files {
let mut f = LookbackReader::new(1);
f.open(x)?;
if f.is_empty() {
continue;
}
if first_file {
first_file = false;
list.lookup(&f.names())?;
}
if checker.check_file(&f.cont, &files[0])? {
f.write_header(&mut w)?;
}
if f.is_done() {
continue;
}
if grep_mode {
loop {
if list.ok(f.curr_line()) ^ reverse {
// write previous lines of context if necessary
f.write_curr(&mut w)?;
} else {
// write more lines of context if necessary
}
if f.getline()? {
break;
}
}
} else {
let mut fails = 0;
loop {
if !list.ok_verbose(f.curr_line(), false) {
fails += 1;
if fails >= max_fails {
break;
}
}
if f.getline()? {
break;
}
}
if fails > 0 {
return Err(Error::Silent);
}
}
}
Ok(())
}get a previous line of text looking back from the start of the file shows empty lines.
Examples found in repository
src/bin/cdx/uniq_main.rs (line 44)
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::One);
const A: [ArgSpec; 1] = [arg! {"key", "k", "Spec", "How to compare adjacent lines"}];
let (args, files) = args::parse(&prog, &A, argv);
let mut comp = LineCompList::new();
for x in args {
if x.name == "key" {
comp.add(&x.value)?;
} else {
unreachable!();
}
}
assert_eq!(files.len(), 1);
let mut f = LookbackReader::new(1);
f.open(&files[0])?;
if f.is_empty() {
return Ok(());
}
let mut w = get_writer("-")?;
f.write_header(&mut w)?;
if f.is_done() {
return Ok(());
}
f.write_curr(&mut w)?;
comp.lookup(&f.names())?;
f.do_split = comp.need_split();
loop {
if f.getline()? {
break;
}
if !comp.equal_cols(f.prev_line(1), f.curr_line()) {
f.write_curr(&mut w)?;
}
}
Ok(())
}write the current text line with newline
Examples found in repository
src/bin/cdx/uniq_main.rs (line 35)
≺ ≻
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::One);
const A: [ArgSpec; 1] = [arg! {"key", "k", "Spec", "How to compare adjacent lines"}];
let (args, files) = args::parse(&prog, &A, argv);
let mut comp = LineCompList::new();
for x in args {
if x.name == "key" {
comp.add(&x.value)?;
} else {
unreachable!();
}
}
assert_eq!(files.len(), 1);
let mut f = LookbackReader::new(1);
f.open(&files[0])?;
if f.is_empty() {
return Ok(());
}
let mut w = get_writer("-")?;
f.write_header(&mut w)?;
if f.is_done() {
return Ok(());
}
f.write_curr(&mut w)?;
comp.lookup(&f.names())?;
f.do_split = comp.need_split();
loop {
if f.getline()? {
break;
}
if !comp.equal_cols(f.prev_line(1), f.curr_line()) {
f.write_curr(&mut w)?;
}
}
Ok(())
}More examples
src/bin/cdx/cgrep_main.rs (line 61)
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::Many);
const A: [ArgSpec; 4] = [
arg! {"pattern", "p", "Col,Spec,Pattern", "Select line where this col matches this pattern."},
arg! {"show-matchers", "s", "", "Print available matchers"},
arg! {"or", "o", "", "A line matches if any of the matchers matches."},
arg_enum! {"header", "h", "Mode", "header requirements", &HEADER_MODE},
];
let (args, files) = args::parse(&prog, &A, argv);
let mut checker = HeaderChecker::new();
let mut list = MultiLineMatcher::new(MultiMode::And);
for x in args {
if x.name == "header" {
checker.mode = HeaderMode::from_str(&x.value)?;
} else if x.name == "pattern" {
list.push(Box::new(ColMatcher::new(&x.value)?));
} else if x.name == "show-matchers" {
MatchMaker::help();
return Ok(());
} else if x.name == "or" {
list.multi = MultiMode::Or;
} else {
unreachable!();
}
}
let mut w = get_writer("-")?;
let grep_mode = true;
let reverse = false;
let max_fails = 5;
let mut first_file = true;
for x in &files {
let mut f = LookbackReader::new(1);
f.open(x)?;
if f.is_empty() {
continue;
}
if first_file {
first_file = false;
list.lookup(&f.names())?;
}
if checker.check_file(&f.cont, &files[0])? {
f.write_header(&mut w)?;
}
if f.is_done() {
continue;
}
if grep_mode {
loop {
if list.ok(f.curr_line()) ^ reverse {
// write previous lines of context if necessary
f.write_curr(&mut w)?;
} else {
// write more lines of context if necessary
}
if f.getline()? {
break;
}
}
} else {
let mut fails = 0;
loop {
if !list.ok_verbose(f.curr_line(), false) {
fails += 1;
if fails >= max_fails {
break;
}
}
if f.getline()? {
break;
}
}
if fails > 0 {
return Err(Error::Silent);
}
}
}
Ok(())
}write header
Examples found in repository
src/bin/cdx/uniq_main.rs (line 30)
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::One);
const A: [ArgSpec; 1] = [arg! {"key", "k", "Spec", "How to compare adjacent lines"}];
let (args, files) = args::parse(&prog, &A, argv);
let mut comp = LineCompList::new();
for x in args {
if x.name == "key" {
comp.add(&x.value)?;
} else {
unreachable!();
}
}
assert_eq!(files.len(), 1);
let mut f = LookbackReader::new(1);
f.open(&files[0])?;
if f.is_empty() {
return Ok(());
}
let mut w = get_writer("-")?;
f.write_header(&mut w)?;
if f.is_done() {
return Ok(());
}
f.write_curr(&mut w)?;
comp.lookup(&f.names())?;
f.do_split = comp.need_split();
loop {
if f.getline()? {
break;
}
if !comp.equal_cols(f.prev_line(1), f.curr_line()) {
f.write_curr(&mut w)?;
}
}
Ok(())
}More examples
src/bin/cdx/cgrep_main.rs (line 52)
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
pub fn main(argv: &[String]) -> Result<()> {
let prog = args::ProgSpec::new("Select uniq lines.", args::FileCount::Many);
const A: [ArgSpec; 4] = [
arg! {"pattern", "p", "Col,Spec,Pattern", "Select line where this col matches this pattern."},
arg! {"show-matchers", "s", "", "Print available matchers"},
arg! {"or", "o", "", "A line matches if any of the matchers matches."},
arg_enum! {"header", "h", "Mode", "header requirements", &HEADER_MODE},
];
let (args, files) = args::parse(&prog, &A, argv);
let mut checker = HeaderChecker::new();
let mut list = MultiLineMatcher::new(MultiMode::And);
for x in args {
if x.name == "header" {
checker.mode = HeaderMode::from_str(&x.value)?;
} else if x.name == "pattern" {
list.push(Box::new(ColMatcher::new(&x.value)?));
} else if x.name == "show-matchers" {
MatchMaker::help();
return Ok(());
} else if x.name == "or" {
list.multi = MultiMode::Or;
} else {
unreachable!();
}
}
let mut w = get_writer("-")?;
let grep_mode = true;
let reverse = false;
let max_fails = 5;
let mut first_file = true;
for x in &files {
let mut f = LookbackReader::new(1);
f.open(x)?;
if f.is_empty() {
continue;
}
if first_file {
first_file = false;
list.lookup(&f.names())?;
}
if checker.check_file(&f.cont, &files[0])? {
f.write_header(&mut w)?;
}
if f.is_done() {
continue;
}
if grep_mode {
loop {
if list.ok(f.curr_line()) ^ reverse {
// write previous lines of context if necessary
f.write_curr(&mut w)?;
} else {
// write more lines of context if necessary
}
if f.getline()? {
break;
}
}
} else {
let mut fails = 0;
loop {
if !list.ok_verbose(f.curr_line(), false) {
fails += 1;
if fails >= max_fails {
break;
}
}
if f.getline()? {
break;
}
}
if fails > 0 {
return Err(Error::Silent);
}
}
}
Ok(())
}Trait Implementations
Returns the “default value” for a type. Read more
