aboutsummaryrefslogtreecommitdiff
path: root/crates/whirl_prompt/src/builtins/mod.rs
blob: 556408bcaf64803a565bbcabf4b67684352710cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
}