oxedyne/fe2o3/fe2o3_tui/src/lib_tui/cmds.rs
4.2 KiB, 15 runs
created by r1870400018:1171, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use crate::lib_tui::{ |
| 2 | repl::{ |
| 3 | Evaluation, |
| 4 | }, |
| 5 | }; |
| 6 | |
| 7 | use oxedyne_fe2o3_core::prelude::*; |
| 8 | use oxedyne_fe2o3_jdat::prelude::*; |
| 9 | use oxedyne_fe2o3_units::{ |
| 10 | si::SI, |
| 11 | system::Units, |
| 12 | }; |
| 13 | use oxedyne_fe2o3_stds::chars::Term; |
| 14 | use oxedyne_fe2o3_syntax::{ |
| 15 | msg::{ |
| 16 | MsgCmd, |
| 17 | }, |
| 18 | }; |
| 19 | |
| 20 | use std::{ |
| 21 | fs, |
| 22 | }; |
| 23 | |
| 24 | // ┌───────────────────────┐ |
| 25 | // │ CONTROL │ |
| 26 | // └───────────────────────┘ |
| 27 | |
| 28 | pub fn exit_shell(exit_msg: &str) -> Outcome<Evaluation> { |
| 29 | println!("{}", exit_msg); |
| 30 | return Ok(Evaluation::Exit); |
| 31 | } |
| 32 | |
| 33 | // ┌───────────────────────┐ |
| 34 | // │ FILE SYSTEM │ |
| 35 | // └───────────────────────┘ |
| 36 | |
| 37 | pub fn change_directory(cmd: &MsgCmd) -> Outcome<Evaluation> { |
| 38 | if cmd.vals.len() > 0 { |
| 39 | if let Dat::Str(s) = &cmd.vals[0] { |
| 40 | match std::env::set_current_dir(s) { |
| 41 | Err(e) => println!( |
| 42 | "{}{}{}{}", |
| 43 | Term::SET_BRIGHT_FORE_RED, |
| 44 | Term::BOLD, |
| 45 | e, |
| 46 | Term::RESET, |
| 47 | ), |
| 48 | _ => {}, |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | Ok(Evaluation::None) |
| 53 | } |
| 54 | |
| 55 | pub fn list_directory_contents(cmd: &MsgCmd) -> Outcome<Evaluation> { |
| 56 | let mut lines = Vec::new(); |
| 57 | let mut max_raw_bytes_width = 0; |
| 58 | for entry in res!(fs::read_dir(".")) { |
| 59 | let mut line = Vec::new(); |
| 60 | let entry = res!(entry); |
| 61 | let mdata = res!(entry.metadata()); |
| 62 | let b = res!(Units::<SI>::bytes(mdata.len() as f64, 4)).humanise(); |
| 63 | // 0 size - raw |
| 64 | let bytes = mdata.len().to_string(); |
| 65 | if bytes.len() > max_raw_bytes_width { |
| 66 | max_raw_bytes_width = bytes.len() + b.symbol().len() + 1; |
| 67 | } |
| 68 | line.push(format!("{} {}", bytes, b.symbol())); |
| 69 | // 1 size - humanised |
| 70 | line.push(format!("{:5.prec$} {:>2}{:<1}", |
| 71 | b.val(), |
| 72 | b.prefix(), |
| 73 | b.symbol(), |
| 74 | prec = 1, |
| 75 | )); |
| 76 | // 2 type |
| 77 | line.push(format!("{}{}", |
| 78 | if mdata.is_dir() {"d"} else {" "}, |
| 79 | if mdata.permissions().readonly() {"r"} else {" "}, |
| 80 | )); |
| 81 | // 3 name |
| 82 | let name = match entry.path().file_name().and_then(|n| n.to_str()) { |
| 83 | Some(n) => n.to_string(), |
| 84 | None => return Err(err!( |
| 85 | "Directory entry {:?} has an invalid (empty or non-UTF-8) name.", |
| 86 | entry.path(); |
| 87 | Invalid, Input)), |
| 88 | }; |
| 89 | line.push(name); |
| 90 | lines.push(line); |
| 91 | } |
| 92 | |
| 93 | match cmd.get_arg_vals("sort") { |
| 94 | Some(vals) => { |
| 95 | if vals.len() > 0 { |
| 96 | if let Dat::Str(s) = &vals[0] { |
| 97 | match &s[..] { |
| 98 | "size" => { |
| 99 | lines.sort_by(|a, b| a[0].cmp(&b[0])); |
| 100 | }, |
| 101 | "type" => { |
| 102 | lines.sort_by(|a, b| a[2].cmp(&b[2])); |
| 103 | }, |
| 104 | "name" => { |
| 105 | lines.sort_by(|a, b| a[3].cmp(&b[3])); |
| 106 | }, |
| 107 | _ => unimplemented!(), |
| 108 | } |
| 109 | if cmd.has_arg(&fmt!("reverse")) { |
| 110 | lines.reverse(); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | }, |
| 115 | None => {}, |
| 116 | } |
| 117 | for line in lines { |
| 118 | if cmd.has_arg(&fmt!("bytes")) { |
| 119 | println!( |
| 120 | "{:>width$} {} {}", |
| 121 | line[0], line[2], line[3], |
| 122 | width = max_raw_bytes_width, |
| 123 | ); |
| 124 | } else { |
| 125 | println!("{} {} {}", line[1], line[2], line[3]); |
| 126 | } |
| 127 | } |
| 128 | Ok(Evaluation::None) |
| 129 | } |
| 130 | |
| 131 | pub fn print_working_directory() -> Outcome<Evaluation> { |
| 132 | let path = res!(std::env::current_dir()); |
| 133 | let output = if let Some(s) = path.to_str() { |
| 134 | fmt!("{}", s) |
| 135 | } else { |
| 136 | fmt!("{:?}", path) |
| 137 | }; |
| 138 | return Ok(Evaluation::Output(output)); |
| 139 | } |
| 140 | |
| 141 | // ┌───────────────────────┐ |
| 142 | // │ WORKSPACE │ |
| 143 | // └───────────────────────┘ |