Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions submissions/week-2/day-3/sshdopey/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions submissions/week-2/day-3/sshdopey/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "practice-enums"
version = "0.1.0"
edition = "2024"

[dependencies]
86 changes: 86 additions & 0 deletions submissions/week-2/day-3/sshdopey/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::env;
use std::io::{self, Write};
use std::os::unix::process::CommandExt;
use std::process::{Command, exit};
use std::thread::sleep;
use std::time::Duration;

enum Actions {
Off,
Sleep,
Reboot,
Shutdown,
Hibernate,
}

fn loading(message: &str) {
print!(" >> {}", message);
let _ = io::stdout().flush();

for _ in 0..3 {
sleep(Duration::from_millis(500));
print!(".");
let _ = io::stdout().flush();
}
println!("");
}

fn sleep_mode(message: &str) {
loading(message);
println!(" >> System is asleep. Press Enter to wake up...");

let mut buffer = String::new();
let _ = io::stdin().read_line(&mut buffer);
println!(" >> System awake!");
}
fn shutdown(message: &str) {
loading(message);
exit(0);
}

fn reboot(message: &str) {
loading(message);
if let Ok(current_exe) = env::current_exe() {
let _ = Command::new(current_exe).exec();
}
}

fn execute(action: Actions) {
match action {
Actions::Off => shutdown("Powering off"),
Actions::Shutdown => shutdown("Shutting down"),
Actions::Sleep => sleep_mode("Entering sleep mode"),
Actions::Reboot => reboot("System rebooting"),
Actions::Hibernate => sleep_mode("Hibernating system"),
}
}

fn main() {
println!("\n --- SYSTEM READY ---");
println!(" >> Please enter a command (Off, Sleep, Reboot, Shutdown, Hibernate):");

loop {
print!(" >> ");
let _ = io::stdout().flush();

let mut input = String::new();
if io::stdin().read_line(&mut input).is_err() {
break;
}

input = input.trim().to_lowercase();

if input.is_empty() {
continue;
}

match input.as_str() {
"off" => execute(Actions::Off),
"sleep" => execute(Actions::Sleep),
"reboot" => execute(Actions::Reboot),
"shutdown" => execute(Actions::Shutdown),
"hibernate" => execute(Actions::Hibernate),
_ => println!(" >> Error: '{}' is not a recognized command.\n", input),
};
}
}
Loading