Skip to content

Commit

Permalink
Add uptime
Browse files Browse the repository at this point in the history
  • Loading branch information
K4rakara committed Aug 8, 2020
1 parent 0117592 commit 31559ed
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 11 deletions.
32 changes: 32 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ uname = "0.1.1"
cpuid = "0.1.1"
cmd_lib = "0.8.2"
mlua = "0.4.1"
chrono = "0.4.13"

[build-dependencies]
clml_rs = { git = "https://github.com/K4rakara/clml-rs" }
24 changes: 23 additions & 1 deletion src/assets/defaults/info_wip.clml
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
<cyan>hello<reset>@<cyan>world<reset>
========================
<cyan>OS<reset>: {distro.shortname} {distro.architecture}
<cyan>Kernel<reset>: {kernel.name} {kernel.version}
<cyan>Kernel<reset>: {kernel.name} {kernel.version}
<cyan>Uptime<reset>: lua`
-- Create a block to store locals in.
if true then
local function s(v) if v ~= 1 and v ~= 0 then return "s" else return "" end end
local output = {}
if uptime.days >= 1 then
table.insert(output, ""..uptime.days.." day"..s(uptime.days))
end
if uptime.hours >= 1 then
table.insert(output, ""..uptime.hours.." hour"..s(uptime.hours))
end
if uptime.minutes >= 1 then
table.insert(output, ""..uptime.minutes.." minute"..s(uptime.minutes))
end
for i,v in pairs(output) do
if i ~= #output then
print(""..v..", ")
else
print(""..v)
end
end
end`
140 changes: 130 additions & 10 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::clml_rs;
use crate::uname;
use crate::cmd_lib;
use crate::cpuid;
use crate::chrono;

use std::fs;
use std::env;
Expand All @@ -12,6 +13,8 @@ use clml_rs::{ CLML };
use uname::{ uname, Info as UnameInfo };
use cmd_lib::{ run_cmd, run_fun };
use cpuid::{ identify as cpu_identify };
use chrono::{ Utc, DateTime, Datelike, Timelike, TimeZone, };

use crate::{ Inject };

pub(crate) struct OS ( pub String, pub String, pub String );
Expand Down Expand Up @@ -165,26 +168,36 @@ impl Inject for Distro {
}
}

pub(crate) struct Kernel ( String, String );
pub(crate) struct Kernel {
pub name: String,
pub version: String,
pub architecture: String,
}

impl Kernel {
pub fn new() -> Self {
let os = get_os();
Kernel(os.0, os.1)
Kernel {
name: os.0,
version: os.1,
architecture: os.2,
}
}
}

impl Inject for Kernel {
fn inject(&self, clml: &mut CLML) -> Result<(), ()> {
// Inject env values.
clml
.env("kernel.name", self.0.as_str())
.env("kernel.version", self.1.as_str());
.env("kernel.name", self.name.as_str())
.env("kernel.version", self.version.as_str())
.env("kernel.architecture", self.architecture.as_str());

// Inject bash values.
clml
.bash_env("kernel_name", self.0.as_str())
.bash_env("kernel_version", self.1.as_str());
.bash_env("kernel_name", self.name.as_str())
.bash_env("kernel_version", self.version.as_str())
.bash_env("kernel_architecture", self.architecture.as_str());

// Inject Lua values.
{
Expand All @@ -193,11 +206,15 @@ impl Inject for Kernel {

match lua.create_table() {
Ok(t) => {
match t.set("name", self.0.as_str()) {
match t.set("name", self.name.as_str()) {
Ok(_) => (),
Err(e) => panic!(format!("The following Lua error occured:\n{}", e)),
}
match t.set("version", self.1.as_str()) {
match t.set("version", self.version.as_str()) {
Ok(_) => (),
Err(e) => panic!(format!("The following Lua error occured:\n{}", e)),
}
match t.set("architecture", self.architecture.as_str()) {
Ok(_) => (),
Err(e) => panic!(format!("The following Lua error occured:\n{}", e)),
}
Expand All @@ -214,19 +231,121 @@ impl Inject for Kernel {
}
}

pub(crate) struct Uptime ( pub DateTime<Utc> );

impl Uptime {
pub fn new(k: &Kernel) -> Self {
let uptime_seconds;
match k.name.as_str() {
"Linux"|"Windows"|"MINIX" => {
if Path::new("/proc/uptime").exists() {
match fs::read_to_string("/proc/uptime") {
Ok(uptime_string) => {
let uptime_seconds_string = String::from(uptime_string
.split(".")
.next()
.unwrap());
uptime_seconds = uptime_seconds_string.parse::<i64>().expect("Failed to convert /proc/uptime to a u64.");
}
Err(e) => panic!(format!("An I/O error occured while reading {}:\n{}", "/proc/uptime", e)),
}
} else {
let boot_time = {
let try_boot_time_output = Command::new("sh")
.arg("-c")
.arg(r#"printf "$(date -d"$(uptime -s)" +%s)""#)
.output();
match try_boot_time_output {
Ok(boot_time_output) => {
let boot_time_string = String::from_utf8(boot_time_output.stdout)
.expect("`boot_time_output.stdout` contained invalid UTF8!");
boot_time_string.parse::<i64>()
.expect("Failed to convert `boot_time_string` to `u64`!")
}
Err(e) => panic!(format!(""))
}
};
let now_time = Utc::now().timestamp();
uptime_seconds = boot_time - now_time;
}
}
// Unknown OS'es should have already exit(1)'d by now, this is just
// to satisfy the compiler.
_ => { uptime_seconds = 0; }
}
Uptime(Utc.timestamp(uptime_seconds, 0))
}
}

impl Inject for Uptime {
fn inject(&self, clml: &mut CLML) -> Result<(), ()> {
// Inject env values.
clml
.env("uptime.days", format!("{}", self.0.day() - 1).as_str())
.env("uptime.hours", format!("{}", self.0.hour()).as_str())
.env("uptime.minutes", format!("{}", self.0.minute()).as_str())
.env("uptime.seconds", format!("{}", self.0.second()).as_str());

// Inject bash values.
clml
.bash_env("uptime_days", format!("{}", self.0.day() - 1).as_str())
.bash_env("uptime_hours", format!("{}", self.0.hour()).as_str())
.bash_env("uptime_minutes", format!("{}", self.0.minute()).as_str())
.bash_env("uptime_seconds", format!("{}", self.0.second()).as_str());

// Inject Lua values.
{
let lua = &clml.lua_env;
let globals = lua.globals();

match lua.create_table() {
Ok(t) => {
match t.set("days", self.0.day() - 1) {
Ok(_) => (),
Err(e) => panic!(format!("The follwoing Lua error occured:\n{}", e)),
}
match t.set("hours", self.0.hour()) {
Ok(_) => (),
Err(e) => panic!(format!("The follwoing Lua error occured:\n{}", e)),
}
match t.set("minutes", self.0.minute()) {
Ok(_) => (),
Err(e) => panic!(format!("The follwoing Lua error occured:\n{}", e)),
}
match t.set("seconds", self.0.second()) {
Ok(_) => (),
Err(e) => panic!(format!("The follwoing Lua error occured:\n{}", e)),
}
match globals.set("uptime", t) {
Ok(_) => (),
Err(e) => panic!(format!("The following Lua error occured:\n{}", e)),
}
}
Err(e) => panic!(format!("The following Lua error occured:\n{}", e)),
}
}

Ok(())
}
}

pub(crate) struct Info {
ctx: CLML,
distro: Distro,
kernel: Kernel,
uptime: Uptime,
rendered: String,
}

impl Info {
pub fn new() -> Self {
let kernel = Kernel::new();
let uptime = Uptime::new(&kernel);
Info {
ctx: CLML::new(),
distro: Distro::new(),
kernel: Kernel::new(),
kernel: kernel,
uptime: uptime,
rendered: String::new(),
}
}
Expand All @@ -240,8 +359,9 @@ impl Info {

impl Inject for Info {
fn prep(&mut self) -> Result<(), ()> {
self.distro.inject(&mut self.ctx)?;
self.kernel.inject(&mut self.ctx)?;
self.distro.inject(&mut self.ctx)?;
self.uptime.inject(&mut self.ctx)?;
self.render()?;
Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub(crate) extern crate clml_rs;
pub(crate) extern crate cmd_lib;
pub(crate) extern crate chrono;
pub(crate) extern crate cpuid;
pub(crate) extern crate mlua;
pub(crate) extern crate term_size;
Expand Down

0 comments on commit 31559ed

Please sign in to comment.