Skip to content

Instantly share code, notes, and snippets.

@ashcrow
Last active June 25, 2018 21:15
Show Gist options
  • Save ashcrow/4e18689505e2fdfcc58b8d0c4f4119ea to your computer and use it in GitHub Desktop.
Save ashcrow/4e18689505e2fdfcc58b8d0c4f4119ea to your computer and use it in GitHub Desktop.
rhcos-shell-greeter idea but in rust
extern crate cursive;
use std::process::Command;
use cursive::Cursive;
use cursive::align::HAlign;
use cursive::views::{Dialog, TextView, LinearLayout, Button};
fn exec_shell_command(_s: &cursive::Cursive, shell_command: String) -> String {
let result = Command::new("sh")
.arg("-c")
.arg(shell_command)
.output()
.expect("Could not execute command");
let ret = String::from_utf8_lossy(&result.stdout);
return ret.to_string();
}
fn show_output(s: &mut cursive::Cursive, shell_command: String) {
let output = exec_shell_command(s, shell_command);
s.add_layer(
Dialog::around(
LinearLayout::horizontal()
.child(Dialog::text(output).h_align(HAlign::Left))
.child(Button::new(
"Close", |s| s.pop_layer()))));
}
// todo: Replace this process with the shell
// fn replace_process(s: &mut cursive::Cursive, shell_command: String) {
// }
fn main() {
let mut term = Cursive::new();
term.add_layer(
Dialog::around(
LinearLayout::vertical()
.child(TextView::new("Welcome to RHCOS!").h_align(HAlign::Left))
.child(Button::new(
"Reboot", |s| exec_shell_command(s, String::from("systemctl reboot -i"))))
.child(Button::new(
"Shutdown", |s| exec_shell_command(s, String::from("systemctl halt -i"))))
.child(Button::new(
"Openshift status", |s| show_output(s, String::from("oc status"))))
.child(Button::new(
"OSTree Status", |s| show_output(s, String::from("rpm-ostree status -v"))))
.child(Button::new(
"Shell", |s| show_output(s, String::from("echo todo"))))
.child(Button::new(
"Quit", |s| s.quit()))));
term.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment