Skip to content

Commit

Permalink
Shit, I forgot to commit a few things.
Browse files Browse the repository at this point in the history
Massive commit go brrrrrrrrr
  • Loading branch information
K4rakara committed Aug 8, 2020
1 parent 992293d commit 2a1b266
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 12 deletions.
20 changes: 20 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 @@ -13,6 +13,7 @@ cpuid = "0.1.1"
cmd_lib = "0.8.2"
mlua = "0.4.1"
chrono = "0.4.13"
users = "0.10.0"

[build-dependencies]
clml_rs = { git = "https://github.com/K4rakara/clml-rs" }
3 changes: 2 additions & 1 deletion src/assets/ascii_art/buildlist
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
# Lines beginning with '#' are ignored.
arch.clml -> .arch.clml
linux_classic.clml -> .linux_classic.clml
linux_256.clml -> .linux_256.clml
linux_256.clml -> .linux_256.clml
manjaro.clml -> .manjaro.clml
7 changes: 7 additions & 0 deletions src/assets/ascii_art/manjaro.clml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<green><green-bg>########<reset> <green><green-bg>###<reset>
<green><green-bg>########<reset> <green><green-bg>###<reset>
<green><green-bg>###<reset> <green><green-bg>###<reset>
<green><green-bg>###<reset> <green><green-bg>###<reset> <green><green-bg>###<reset>
<green><green-bg>###<reset> <green><green-bg>###<reset> <green><green-bg>###<reset>
<green><green-bg>###<reset> <green><green-bg>###<reset> <green><green-bg>###<reset>
<green><green-bg>###<reset> <green><green-bg>###<reset> <green><green-bg>###<reset>
1 change: 1 addition & 0 deletions src/assets/ascii_art/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ static ASCII_ART: &[(&'static str, &'static str, bool)] = &[
( "linux", include_str!("./.linux_classic.clml"), false, ),
( "linux_classic", include_str!("./.linux_classic.clml"), false, ),
( "linux_256", include_str!("./.linux_256.clml"), false, ),
( "manjaro", include_str!("./.manjaro.clml"), false, ),
];

pub(crate) fn get_ascii_art(of: &str) -> &'static str {
Expand Down
18 changes: 12 additions & 6 deletions src/assets/defaults/info_wip.clml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<cyan>hello<reset>@<cyan>world<reset>
<green>{user}<reset>@<green>{host}<reset>
========================
<cyan>OS<reset>: {distro.shortname} {distro.architecture}
<cyan>Kernel<reset>: {kernel.name} {kernel.version}
<cyan>Uptime<reset>: lua`
<green>OS<reset>: {distro.shortname} {distro.architecture}
<green>Kernel<reset>: {kernel.name} {kernel.version}
<green>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
Expand All @@ -26,7 +26,7 @@
end
end
end`
<cyan>Packages<reset>: lua`
<green>Packages<reset>: lua`
if #packageManagers ~= 0 then
for i,packageManager in pairs(packageManagers) do
if packageManager.packages == 0 then
Expand All @@ -40,4 +40,10 @@
print(""..packageManager.packages.." ("..packageManager.name..")")
end
end
else print "0" end`
else print "0" end`
<green>Shell<reset>: {shell.name} {shell.version} bash`
if [[ "$shell_name" = "zsh" ]]; then
if [[ -d ~/.oh-my-zsh ]]; then
printf "with $(cat ~/.zshrc | grep -m 1 --color=none ZSH_THEME= | cut -d"\"" -f 2)";
fi;
fi;`
5 changes: 2 additions & 3 deletions src/assets/defaults/layout.clml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{art}
<save><up 13>
<forward lua`print(""..math.floor(terminal.width / 2))`>lua`print(string.gsub(info, "\n", "\n<forward "..math.floor(terminal.width / 2)..">"))`
lua`print(string.gsub(art, "\n", "\n "))`
<save><up 9><forward 17>lua`print(string.gsub(info, "\n", "\n<forward 17>"))`
<restore>
168 changes: 167 additions & 1 deletion src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::uname;
use crate::cmd_lib;
use crate::cpuid;
use crate::chrono;
use crate::users;

use std::fs;
use std::env;
Expand All @@ -17,6 +18,62 @@ use chrono::{ Utc, DateTime, Datelike, Timelike, TimeZone, };

use crate::{ Inject };

pub(crate) struct User ( pub String );

impl User {
pub fn new() -> Self {
User(String::from(users::get_current_username()
.expect("Failed to get current username!")
.to_string_lossy()))
}
}

impl Inject for User {
fn inject(&self, clml: &mut CLML) -> Result<(), ()> {
// Inject CLML value.
clml.env("user", self.0.as_str());

// Inject Bash value.
clml.bash_env("user", self.0.as_str());

// Inject Lua value.
match clml.lua_env.globals().set("user", self.0.as_str()) {
Ok(_) => (),
Err(e) => panic!(format!("The following Lua error occured:\n{}", e)),
}

Ok(())
}
}

pub(crate) struct Host ( pub String );

impl Host {
pub fn new() -> Self {
let hostname = &fs::read_to_string("/etc/hostname")
.expect("Failed to read \"/etc/hostname\"!");
Host(String::from(&hostname[0..(hostname.len() - 1)]))
}
}

impl Inject for Host {
fn inject(&self, clml: &mut CLML) -> Result<(), ()> {
// Inject CLML value.
clml.env("host", self.0.as_str());

// Inject Bash value.
clml.bash_env("host", self.0.as_str());

// Inject Lua value.
match clml.lua_env.globals().set("host", self.0.as_str()) {
Ok(_) => (),
Err(e) => panic!(format!("The following Lua error occured:\n{}", e)),
}

Ok(())
}
}

pub(crate) struct OS ( pub String, pub String, pub String );
pub(crate) fn get_os() -> OS {
let uname = uname().expect("Failed to run `crate::uname::uname()`.");
Expand Down Expand Up @@ -419,6 +476,18 @@ impl PackageManagers {

PackageManagers(to_return)
}

pub fn get_main(&self) -> String {
for package_manager in self.0.iter() {
match package_manager.name.as_str() {
"snap"|"flatpak"|"npm" => (),
_ => {
return package_manager.name.clone();
}
}
}
String::new()
}
}

impl Inject for PackageManagers {
Expand Down Expand Up @@ -451,7 +520,6 @@ impl Inject for PackageManagers {
format!("\"{}:{}\"", package_manager.name, package_manager.packages));
}
to_return = format!("{})", to_return);
println!("{}", to_return);
clml.bash_env("package_managers", to_return.as_str());
}

Expand Down Expand Up @@ -493,12 +561,103 @@ impl Inject for PackageManagers {
}
}

pub(crate) struct Shell {
pub name: String,
pub version: String,
}

impl Shell {
pub fn new(k: &Kernel) -> Self {
let name;
let version;
match k.name.as_str() {
"Linux"|"BSD"|"Windows" => {
let shell_bin = String::from(
Path::new(
&match env::var("SHELL") {
Ok(v) => v,
Err(e) => panic!(format!("Failed to get $SHELL. Details:\n{}", e)),
}
)
.file_name()
.expect("$SHELL is invalid!")
.to_string_lossy());
name = shell_bin;
match name.as_str() {
"zsh" => version = {
let try_output = Command::new("zsh")
.arg("-c")
.arg("printf $ZSH_VERSION")
.output();
match try_output {
Ok(output) => {
String::from_utf8(output.stdout)
.expect("The output of \"zsh -c printf $ZSH_VERSION\" contained invalid UTF8.")
}
Err(e) => panic!("Failed to get ZSH_VERSION."),
}
},
_ => version = String::new(),
}
}
_ => { name = String::new(); version = String::new(); }
}
Shell {
name: name,
version: version,
}
}
}

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

// Inject bash values.
clml
.bash_env("shell_name", self.name.as_str())
.bash_env("shell_version", self.version.as_str());

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

match lua.create_table() {
Ok(t) => {
match t.set("name", self.name.as_str()) {
Ok(_) => (),
Err(e) => panic!(format!("{}", e)),
}
match t.set("version", self.version.as_str()) {
Ok(_) => (),
Err(e) => panic!(format!("{}", e)),
}
match globals.set("shell", t) {
Ok(_) => (),
Err(e) => panic!(format!("{}", e)),
}
}
Err(e) => panic!(format!("{}", e)),
}
}

Ok(())
}
}

pub(crate) struct Info {
ctx: CLML,
user: User,
host: Host,
distro: Distro,
kernel: Kernel,
uptime: Uptime,
package_managers: PackageManagers,
shell: Shell,
rendered: String,
}

Expand All @@ -507,12 +666,16 @@ impl Info {
let kernel = Kernel::new();
let uptime = Uptime::new(&kernel);
let package_managers = PackageManagers::new(&kernel);
let shell = Shell::new(&kernel);
Info {
ctx: CLML::new(),
user: User::new(),
host: Host::new(),
distro: Distro::new(),
kernel: kernel,
uptime: uptime,
package_managers: package_managers,
shell: shell,
rendered: String::new(),
}
}
Expand All @@ -526,10 +689,13 @@ impl Info {

impl Inject for Info {
fn prep(&mut self) -> Result<(), ()> {
self.user.inject(&mut self.ctx)?;
self.host.inject(&mut self.ctx)?;
self.kernel.inject(&mut self.ctx)?;
self.distro.inject(&mut self.ctx)?;
self.uptime.inject(&mut self.ctx)?;
self.package_managers.inject(&mut self.ctx)?;
self.shell.inject(&mut self.ctx)?;
self.render()?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(crate) struct Layout {
impl Layout {
pub fn new() -> Self {
Layout {
art: Art::new("linux_256"),
art: Art::new("manjaro"),
info: Info::new(),
terminal: Terminal::new(),
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub(crate) extern crate cpuid;
pub(crate) extern crate mlua;
pub(crate) extern crate term_size;
pub(crate) extern crate uname;
pub(crate) extern crate users;

pub(crate) mod art;
pub(crate) mod assets;
Expand Down

0 comments on commit 2a1b266

Please sign in to comment.