|
625 | 625 | <a href="#625" id="625">625</a>
|
626 | 626 | <a href="#626" id="626">626</a>
|
627 | 627 | <a href="#627" id="627">627</a>
|
| 628 | +<a href="#628" id="628">628</a> |
| 629 | +<a href="#629" id="629">629</a> |
| 630 | +<a href="#630" id="630">630</a> |
| 631 | +<a href="#631" id="631">631</a> |
| 632 | +<a href="#632" id="632">632</a> |
| 633 | +<a href="#633" id="633">633</a> |
| 634 | +<a href="#634" id="634">634</a> |
| 635 | +<a href="#635" id="635">635</a> |
| 636 | +<a href="#636" id="636">636</a> |
| 637 | +<a href="#637" id="637">637</a> |
| 638 | +<a href="#638" id="638">638</a> |
| 639 | +<a href="#639" id="639">639</a> |
| 640 | +<a href="#640" id="640">640</a> |
| 641 | +<a href="#641" id="641">641</a> |
| 642 | +<a href="#642" id="642">642</a> |
| 643 | +<a href="#643" id="643">643</a> |
| 644 | +<a href="#644" id="644">644</a> |
| 645 | +<a href="#645" id="645">645</a> |
| 646 | +<a href="#646" id="646">646</a> |
| 647 | +<a href="#647" id="647">647</a> |
| 648 | +<a href="#648" id="648">648</a> |
| 649 | +<a href="#649" id="649">649</a> |
| 650 | +<a href="#650" id="650">650</a> |
| 651 | +<a href="#651" id="651">651</a> |
| 652 | +<a href="#652" id="652">652</a> |
| 653 | +<a href="#653" id="653">653</a> |
| 654 | +<a href="#654" id="654">654</a> |
| 655 | +<a href="#655" id="655">655</a> |
628 | 656 | </pre></div><pre class="rust"><code><span class="comment">// * This file is part of the uutils coreutils package.
|
629 | 657 | // *
|
630 | 658 | // * (c) Martin Kysel <code@martinkysel.com>
|
|
641 | 669 | time::Duration,
|
642 | 670 | };
|
643 | 671 |
|
644 |
| -<span class="kw">use </span>clap::{crate_version, Arg, ArgAction, ArgMatches, Command}; |
| 672 | +<span class="kw">use </span>clap::{crate_version, value_parser, Arg, ArgAction, ArgMatches, Command}; |
645 | 673 | <span class="kw">use </span>crossterm::event::KeyEventKind;
|
646 | 674 | <span class="kw">use </span>crossterm::{
|
647 |
| - cursor::MoveTo, |
| 675 | + cursor::{MoveTo, MoveUp}, |
648 | 676 | event::{<span class="self">self</span>, Event, KeyCode, KeyEvent, KeyModifiers},
|
649 | 677 | execute, queue,
|
650 | 678 | style::Attribute,
|
|
680 | 708 | <span class="kw">const </span>MULTI_FILE_TOP_PROMPT: <span class="kw-2">&</span>str = <span class="string">"::::::::::::::\n{}\n::::::::::::::\n"</span>;
|
681 | 709 |
|
682 | 710 | <span class="kw">struct </span>Options {
|
683 |
| - silent: bool, |
684 | 711 | clean_print: bool,
|
| 712 | + lines: <span class="prelude-ty">Option</span><u16>, |
685 | 713 | print_over: bool,
|
| 714 | + silent: bool, |
686 | 715 | squeeze: bool,
|
687 | 716 | }
|
688 | 717 |
|
689 | 718 | <span class="kw">impl </span>Options {
|
690 | 719 | <span class="kw">fn </span>from(matches: <span class="kw-2">&</span>ArgMatches) -> <span class="self">Self </span>{
|
| 720 | + <span class="kw">let </span>lines = <span class="kw">match </span>( |
| 721 | + matches.get_one::<u16>(options::LINES).copied(), |
| 722 | + matches.get_one::<u16>(options::NUMBER).copied(), |
| 723 | + ) { |
| 724 | + <span class="comment">// We add 1 to the number of lines to display because the last line |
| 725 | + // is used for the banner |
| 726 | + </span>(<span class="prelude-val">Some</span>(number), <span class="kw">_</span>) <span class="kw">if </span>number > <span class="number">0 </span>=> <span class="prelude-val">Some</span>(number + <span class="number">1</span>), |
| 727 | + (<span class="prelude-val">None</span>, <span class="prelude-val">Some</span>(number)) <span class="kw">if </span>number > <span class="number">0 </span>=> <span class="prelude-val">Some</span>(number + <span class="number">1</span>), |
| 728 | + (<span class="kw">_</span>, <span class="kw">_</span>) => <span class="prelude-val">None</span>, |
| 729 | + }; |
691 | 730 | <span class="self">Self </span>{
|
692 | 731 | clean_print: matches.get_flag(options::CLEAN_PRINT),
|
| 732 | + lines, |
693 | 733 | print_over: matches.get_flag(options::PRINT_OVER),
|
694 | 734 | silent: matches.get_flag(options::SILENT),
|
695 | 735 | squeeze: matches.get_flag(options::SQUEEZE),
|
|
794 | 834 | .help(<span class="string">"Squeeze multiple blank lines into one"</span>)
|
795 | 835 | .action(ArgAction::SetTrue),
|
796 | 836 | )
|
| 837 | + .arg( |
| 838 | + Arg::new(options::LINES) |
| 839 | + .short(<span class="string">'n'</span>) |
| 840 | + .long(options::LINES) |
| 841 | + .value_name(<span class="string">"number"</span>) |
| 842 | + .num_args(<span class="number">1</span>) |
| 843 | + .value_parser(<span class="macro">value_parser!</span>(u16).range(<span class="number">0</span>..)) |
| 844 | + .help(<span class="string">"The number of lines per screen full"</span>), |
| 845 | + ) |
| 846 | + .arg( |
| 847 | + Arg::new(options::NUMBER) |
| 848 | + .long(options::NUMBER) |
| 849 | + .required(<span class="bool-val">false</span>) |
| 850 | + .num_args(<span class="number">1</span>) |
| 851 | + .value_parser(<span class="macro">value_parser!</span>(u16).range(<span class="number">0</span>..)) |
| 852 | + .help(<span class="string">"Same as --lines"</span>), |
| 853 | + ) |
797 | 854 | <span class="comment">// The commented arguments below are unimplemented:
|
798 | 855 | /*
|
799 | 856 | .arg(
|
|
814 | 871 | .long(options::PLAIN)
|
815 | 872 | .help("Suppress underlining and bold"),
|
816 | 873 | )
|
817 |
| - .arg( |
818 |
| - Arg::new(options::LINES) |
819 |
| - .short('n') |
820 |
| - .long(options::LINES) |
821 |
| - .value_name("number") |
822 |
| - .takes_value(true) |
823 |
| - .help("The number of lines per screen full"), |
824 |
| - ) |
825 |
| - .arg( |
826 |
| - Arg::new(options::NUMBER) |
827 |
| - .allow_hyphen_values(true) |
828 |
| - .long(options::NUMBER) |
829 |
| - .required(false) |
830 |
| - .takes_value(true) |
831 |
| - .help("Same as --lines"), |
832 |
| - ) |
833 | 874 | .arg(
|
834 | 875 | Arg::new(options::FROM_LINE)
|
835 | 876 | .short('F')
|
|
890 | 931 | next_file: <span class="prelude-ty">Option</span><<span class="kw-2">&</span>str>,
|
891 | 932 | options: <span class="kw-2">&</span>Options,
|
892 | 933 | ) -> UResult<()> {
|
893 |
| - <span class="kw">let </span>(cols, rows) = terminal::size().unwrap(); |
| 934 | + <span class="kw">let </span>(cols, <span class="kw-2">mut </span>rows) = terminal::size().unwrap(); |
| 935 | + <span class="kw">if let </span><span class="prelude-val">Some</span>(number) = options.lines { |
| 936 | + rows = number; |
| 937 | + } |
| 938 | + |
894 | 939 | <span class="kw">let </span>lines = break_buff(buff, usize::from(cols));
|
895 | 940 |
|
896 | 941 | <span class="kw">let </span><span class="kw-2">mut </span>pager = Pager::new(rows, lines, next_file, options);
|
|
954 | 999 | ..
|
955 | 1000 | }) => {
|
956 | 1001 | pager.page_up();
|
| 1002 | + paging_add_back_message(options, stdout)<span class="question-mark">?</span>; |
957 | 1003 | }
|
958 | 1004 | Event::Key(KeyEvent {
|
959 | 1005 | code: KeyCode::Char(<span class="string">'j'</span>),
|
|
974 | 1020 | pager.prev_line();
|
975 | 1021 | }
|
976 | 1022 | Event::Resize(col, row) => {
|
977 |
| - pager.page_resize(col, row); |
| 1023 | + pager.page_resize(col, row, options.lines); |
978 | 1024 | }
|
979 | 1025 | Event::Key(KeyEvent {
|
980 | 1026 | code: KeyCode::Char(k),
|
|
1074 | 1120 | }
|
1075 | 1121 |
|
1076 | 1122 | <span class="comment">// TODO: Deal with column size changes.
|
1077 |
| - </span><span class="kw">fn </span>page_resize(<span class="kw-2">&mut </span><span class="self">self</span>, <span class="kw">_</span>: u16, row: u16) { |
1078 |
| - <span class="self">self</span>.content_rows = row.saturating_sub(<span class="number">1</span>); |
| 1123 | + </span><span class="kw">fn </span>page_resize(<span class="kw-2">&mut </span><span class="self">self</span>, <span class="kw">_</span>: u16, row: u16, option_line: <span class="prelude-ty">Option</span><u16>) { |
| 1124 | + <span class="kw">if </span>option_line.is_none() { |
| 1125 | + <span class="self">self</span>.content_rows = row.saturating_sub(<span class="number">1</span>); |
| 1126 | + }; |
1079 | 1127 | }
|
1080 | 1128 |
|
1081 | 1129 | <span class="kw">fn </span>draw(<span class="kw-2">&mut </span><span class="self">self</span>, stdout: <span class="kw-2">&mut </span>std::io::Stdout, wrong_key: <span class="prelude-ty">Option</span><char>) {
|
|
1163 | 1211 | }
|
1164 | 1212 | }
|
1165 | 1213 |
|
| 1214 | +<span class="kw">fn </span>paging_add_back_message(options: <span class="kw-2">&</span>Options, stdout: <span class="kw-2">&mut </span>std::io::Stdout) -> UResult<()> { |
| 1215 | + <span class="kw">if </span>options.lines.is_some() { |
| 1216 | + <span class="macro">execute!</span>(stdout, MoveUp(<span class="number">1</span>))<span class="question-mark">?</span>; |
| 1217 | + stdout.write_all(<span class="string">"\n\r...back 1 page\n"</span>.as_bytes())<span class="question-mark">?</span>; |
| 1218 | + } |
| 1219 | + <span class="prelude-val">Ok</span>(()) |
| 1220 | +} |
| 1221 | + |
1166 | 1222 | <span class="comment">// Break the lines on the cols of the terminal
|
1167 | 1223 | </span><span class="kw">fn </span>break_buff(buff: <span class="kw-2">&</span>str, cols: usize) -> Vec<String> {
|
1168 | 1224 | <span class="kw">let </span><span class="kw-2">mut </span>lines = Vec::with_capacity(buff.lines().count());
|
|
0 commit comments