aboutsummaryrefslogtreecommitdiff
path: root/crates/whirl_prompt/src/builtins
diff options
context:
space:
mode:
authorFuwn <[email protected]>2021-05-20 17:05:59 +0000
committerFuwn <[email protected]>2021-05-20 17:05:59 +0000
commit9dbc613765de8ab7dfa8e1374cf6661dcfd56bc8 (patch)
tree8cfff6a23bb72db2660e68c63a8cf9d0539a061f /crates/whirl_prompt/src/builtins
parentfeat(readme): add sqlfluff as a dev dep (diff)
downloadwhirl-9dbc613765de8ab7dfa8e1374cf6661dcfd56bc8.tar.xz
whirl-9dbc613765de8ab7dfa8e1374cf6661dcfd56bc8.zip
refactor(global): move crates around, stricter module isolation
Diffstat (limited to 'crates/whirl_prompt/src/builtins')
-rw-r--r--crates/whirl_prompt/src/builtins/mod.rs108
-rw-r--r--crates/whirl_prompt/src/builtins/structures.rs34
2 files changed, 142 insertions, 0 deletions
diff --git a/crates/whirl_prompt/src/builtins/mod.rs b/crates/whirl_prompt/src/builtins/mod.rs
new file mode 100644
index 0000000..0359443
--- /dev/null
+++ b/crates/whirl_prompt/src/builtins/mod.rs
@@ -0,0 +1,108 @@
+// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective
+// SPDX-License-Identifier: GPL-3.0-only
+
+pub mod structures;
+
+use std::io::Write;
+
+use sysinfo::SystemExt;
+use whirl_config::Config;
+
+use crate::constants::{FILES, HELPABLES_BUILTINS, HELPABLES_BUILTIN_CONFIG};
+
+pub fn builtin_echo(args: &[String]) -> i32 {
+ println!("{}", args.join(" "));
+ 0
+}
+
+pub fn builtin_history(history: &[String]) -> i32 {
+ for (index, cmd) in history.iter().enumerate() {
+ println!("{} {}", index, cmd.trim());
+ }
+ 0
+}
+
+pub fn builtin_help() -> i32 {
+ for help in HELPABLES_BUILTINS.iter() {
+ println!("{}", help);
+ }
+
+ 0
+}
+
+pub fn builtin_ls() -> i32 {
+ for file in &FILES {
+ print!("{} ", file);
+ }
+ println!();
+
+ 0
+}
+
+pub async fn builtin_cat(args: &[String]) -> i32 {
+ let file;
+ if let Some(file_name) = args.get(0) {
+ file = file_name.to_string();
+ } else {
+ return 0;
+ };
+
+ match file.as_str() {
+ "README.rst" => {
+ let mut easy = curl::easy::Easy::new();
+
+ easy
+ .url("https://raw.githubusercontent.com/Whirlsplash/whirl/develop/README.rst")
+ .unwrap();
+
+ let mut transfer = easy.transfer();
+ transfer
+ .write_function(|data| {
+ std::io::stdout().write_all(data).unwrap();
+ Ok(data.len())
+ })
+ .unwrap();
+ transfer.perform().unwrap();
+ }
+ "Whirl.toml" => {
+ colour::red_ln!("NOTE: This is just a wrapper for `config show`.");
+ println!("{:#?}", Config::get());
+ }
+ _ => println!("/cat: {}: no such file or directory", file),
+ }
+
+ 0
+}
+
+pub fn builtin_config(args: &[String]) -> i32 {
+ match args.get(0) {
+ Some(sub) =>
+ match sub.as_str() {
+ "show" => println!("{:#?}", Config::get()),
+ "help" | "--help" | "-h" =>
+ for help in HELPABLES_BUILTIN_CONFIG.iter() {
+ println!("{}", help);
+ },
+ "refresh" => Config::refresh(),
+ _ => println!("invalid arguments provided"),
+ },
+ None => println!("invalid amount arguments provided"),
+ }
+ 0
+}
+
+pub fn builtin_fetch() -> i32 {
+ // rfetch: https://github.com/Mangeshrex/rfetch
+
+ let mut sys = sysinfo::System::new();
+ sys.refresh_processes();
+
+ println!(" ");
+ println!(" .-. os {}", env!("CARGO_PKG_NAME"));
+ println!(" oo| ker {}", env!("CARGO_PKG_VERSION"));
+ println!(" / '\\ sh /wsh");
+ println!(" (\\_;/) up null");
+ println!(" ");
+
+ 0
+}
diff --git a/crates/whirl_prompt/src/builtins/structures.rs b/crates/whirl_prompt/src/builtins/structures.rs
new file mode 100644
index 0000000..4217a38
--- /dev/null
+++ b/crates/whirl_prompt/src/builtins/structures.rs
@@ -0,0 +1,34 @@
+// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective
+// SPDX-License-Identifier: GPL-3.0-only
+
+use std::str::FromStr;
+
+pub enum BuiltIn {
+ Echo,
+ History,
+ Exit,
+ Null,
+ Help,
+ Ls,
+ Cat,
+ Config,
+ Fetch,
+}
+impl FromStr for BuiltIn {
+ type Err = ();
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ match s {
+ "echo" => Ok(BuiltIn::Echo),
+ "history" => Ok(BuiltIn::History),
+ "exit" => Ok(BuiltIn::Exit),
+ "null" => Ok(BuiltIn::Null),
+ "help" => Ok(BuiltIn::Help),
+ "ls" => Ok(BuiltIn::Ls),
+ "cat" => Ok(BuiltIn::Cat),
+ "config" => Ok(BuiltIn::Config),
+ "fetch" => Ok(BuiltIn::Fetch),
+ _ => Err(()),
+ }
+ }
+}