Skip to content

Commit abc8d63

Browse files
committed
Rune - Added "du" as built-in. Right now is functional but in the future I will add results sorted by size.
1 parent 625f289 commit abc8d63

3 files changed

Lines changed: 46 additions & 5 deletions

File tree

‎source_code/Cargo.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎source_code/rune/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rune"
3-
version = "0.41.19"
3+
version = "0.42.19"
44
edition = "2021"
55
readme = "../README.md"
66
license = "GPL-3.0"

‎source_code/rune/src/builtins.rs‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use crate::io_mods::get_user_home;
6363
// Here we use a const and not let because is a global variable
6464
// As we know the size of each word we can use "&str" and then we specify the number
6565
// of elements. This is because a const must have know size at compiling time.
66-
const LBUILTINS: [&str; 38] = ["base64", "basename", "cd", "clear", "count", "cp", "date", "decodebase64", "disable_history", "echo_raw", "enable_history", "env", "exit", "expand", "false", "history", "head", "help", "home", "id", "join", "info", "mkdir", "mkfile", "move", "nl", "list", "ln", "ls", "proc", "pwd", "rm", "seq", "show","sleep", "tail", "which", "$?"];
66+
const LBUILTINS: [&str; 39] = ["base64", "basename", "cd", "clear", "count", "cp", "date", "decodebase64", "disable_history", "du", "echo_raw", "enable_history", "env", "exit", "expand", "false", "history", "head", "help", "home", "id", "join", "info", "mkdir", "mkfile", "move", "nl", "list", "ln", "ls", "proc", "pwd", "rm", "seq", "show","sleep", "tail", "which", "$?"];
6767

6868
const HBUILTINS: &str = "Help;
6969
Remember respect the positions of each argument
@@ -77,6 +77,7 @@ _cp [source] [destination]: copy file or directory from [source] to [destination
7777
_date: display the current time and date in UTC-0 (which is the same that GTM-0).
7878
_decodebase64 [input] [file]: decocde input from base64 to file.
7979
_disable_history: disable save commands to history without truncate the file.
80+
_du [path]: show disk usage ('du') in [path], read recusively.
8081
_enable_history: enable save commands to history.
8182
_echo_raw: show string into stdout without interpreting special characters.
8283
_env: show environment variables.
@@ -106,7 +107,7 @@ _tail [number] [file] : show the last [number] lines of [file].
106107
_which [binary]: show where is located the binary based in PATH environment variable.
107108
_$?: print the latest command exit return, not include builtins";
108109

109-
const RUNE_VERSION: &str = "v0.41.19";
110+
const RUNE_VERSION: &str = "v0.42.19";
110111

111112
// Builtins
112113
// Are private for only be executed by rbuiltins
@@ -333,6 +334,41 @@ fn decodebase64(input: &String) -> Option<String> {
333334
}
334335
}
335336

337+
fn disk_usage(input: &String) -> Option<String> {
338+
let mut input = input.clone();
339+
340+
if input.split(' ').map(|e| e.to_string()).collect::<Vec<String>>().len() > 1 {
341+
return Some("Too many arguments. Pass just one path".to_string());
342+
} else if input.len() == 0 {
343+
return Some("Too few arguments. Pass just one path".to_string());
344+
}
345+
346+
let fs_struct = input.readdir_recursive();
347+
let mut temp_buff = HashMap::new();
348+
let mut result = String::new();
349+
350+
for dir in fs_struct.dbuff {
351+
let size = fs::metadata(dir.clone()).unwrap().size().size_to_human();
352+
temp_buff.insert(size,dir);
353+
}
354+
355+
for file in fs_struct.fbuff {
356+
let size = fs::metadata(file.clone()).unwrap().size().size_to_human();
357+
temp_buff.insert(size,file);
358+
}
359+
360+
for (k,i) in temp_buff {
361+
result = result + &format!("{k} {i}\n");
362+
}
363+
364+
if result.is_empty() {
365+
return None;
366+
} else {
367+
return Some(result);
368+
}
369+
370+
}
371+
336372
fn environmentvar() -> String {
337373
let mut buffer: Vec<String> = Vec::new();
338374
let mut buffer2: String = String::new();
@@ -1407,10 +1443,15 @@ pub fn rbuiltins(command: &str, b_arguments: String) -> Result<String,&str> {
14071443
Some(d) => Ok(d),
14081444
None => Err("error converting"),
14091445
}
1446+
} else if command == "du"{
1447+
match disk_usage(&b_arguments) {
1448+
Some(d) => Ok(d),
1449+
None => Err("Error reading path"),
1450+
}
14101451
} else if command == "echo_raw" {
14111452
result = echo_raw(&b_arguments);
14121453
Ok(result)
1413-
}else if command == "expand" {
1454+
} else if command == "expand" {
14141455
result = expand(b_arguments);
14151456
Ok(result)
14161457
} else if command == "head" {

0 commit comments

Comments
 (0)