Function erg_common::chomp

source ·
pub fn chomp(src: &str) -> String
Expand description

cut \n

Examples found in repository?
traits.rs (line 450)
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
    fn run(cfg: ErgConfig) {
        let quiet_repl = cfg.quiet_repl;
        let mut instance = Self::new(cfg);
        let res = match instance.input() {
            Input::File(_) | Input::Pipe(_) | Input::Str(_) => instance.exec(),
            Input::REPL => {
                let output = stdout();
                let mut output = BufWriter::new(output.lock());
                if !quiet_repl {
                    log!(info_f output, "The REPL has started.\n");
                    output
                        .write_all(instance.start_message().as_bytes())
                        .unwrap();
                }
                output.write_all(instance.ps1().as_bytes()).unwrap();
                output.flush().unwrap();
                let mut in_block = false;
                let mut lines = String::new();
                loop {
                    let line = chomp(&instance.input().read());
                    match &line[..] {
                        ":quit" | ":exit" => {
                            instance.quit_successfully(output);
                        }
                        ":clear" => {
                            output.write_all("\x1b[2J\x1b[1;1H".as_bytes()).unwrap();
                            output.flush().unwrap();
                            output.write_all(instance.ps1().as_bytes()).unwrap();
                            output.flush().unwrap();
                            instance.clear();
                            continue;
                        }
                        _ => {}
                    }
                    let line = if let Some(comment_start) = line.find('#') {
                        &line[..comment_start]
                    } else {
                        &line[..]
                    };
                    lines.push_str(line);

                    if in_block {
                        if is_in_the_expected_block(line, &lines, &mut in_block) {
                            lines += "\n";
                            output.write_all(instance.ps2().as_bytes()).unwrap();
                            output.flush().unwrap();
                            continue;
                        }
                    } else if expect_block(line) {
                        in_block = true;
                        lines += "\n";
                        output.write_all(instance.ps2().as_bytes()).unwrap();
                        output.flush().unwrap();
                        continue;
                    }

                    match instance.eval(mem::take(&mut lines)) {
                        Ok(out) => {
                            output.write_all((out + "\n").as_bytes()).unwrap();
                            output.flush().unwrap();
                        }
                        Err(errs) => {
                            if errs
                                .first()
                                .map(|e| e.core().kind == ErrorKind::SystemExit)
                                .unwrap_or(false)
                            {
                                instance.quit_successfully(output);
                            }
                            errs.fmt_all_stderr();
                        }
                    }
                    output.write_all(instance.ps1().as_bytes()).unwrap();
                    output.flush().unwrap();
                    instance.clear();
                }
            }
            Input::Dummy => switch_unreachable!(),
        };
        if let Err(e) = res {
            e.fmt_all_stderr();
            instance.quit(1);
        }
    }