Skip to content

Commit

Permalink
updated dependencies, optimized regex, better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
TimB87 committed Jan 5, 2024
1 parent 041e53e commit 9d341e9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
9 changes: 5 additions & 4 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 @@ -5,6 +5,7 @@ authors = ["Tim Biermann <[email protected]>"]
edition = "2021"

[dependencies]
lazy_static = "1.4.0"
libnotify = "1.0.3"
regex = { version = "1.10.2", default-features = false }

Expand Down
36 changes: 24 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use lazy_static::lazy_static;
use regex::Regex;
use std::env;
use std::error::Error;
Expand All @@ -16,7 +17,7 @@ fn find_ports_in_repositories(package_name: &str) -> Result<String, Box<dyn Erro

for line in reader.lines().flatten() {
if line.starts_with("prtdir ") {
let prtdir = line.trim().to_string();
let prtdir = line.trim();
// `prtdir ` is 7 characters long
prtdirs.push(prtdir[7..].to_string());
}
Expand All @@ -35,24 +36,27 @@ fn find_ports_in_repositories(package_name: &str) -> Result<String, Box<dyn Erro
Ok(repository)
}

lazy_static! {
static ref VERSION_REGEX: Regex = Regex::new(r#"version=(.+)"#).unwrap();
static ref RELEASE_REGEX: Regex = Regex::new(r#"release=(.+)"#).unwrap();
}

fn extract_pkgfile_version(port_dir: &str) -> Option<String> {
let pkgfile_path = format!("{port_dir}/Pkgfile");
if !Path::new(&pkgfile_path).exists() {
return None;
}

let pkgfile_content = std::fs::read_to_string(pkgfile_path).ok()?;
let version_regex = Regex::new(r#"version=(.+)"#).ok()?;
let release_regex = Regex::new(r#"release=(.+)"#).ok()?;

let version = match version_regex.captures(&pkgfile_content) {
let version = match VERSION_REGEX.captures(&pkgfile_content) {
Some(captures) => captures.get(1).map(|m| m.as_str()).unwrap_or(""),
None => {
eprintln!("Failed to extract version from Pkgfile for: {port_dir}");
""
}
};
let release = release_regex
let release = RELEASE_REGEX
.captures(&pkgfile_content)?
.get(1)
.map(|m| m.as_str())
Expand Down Expand Up @@ -82,7 +86,9 @@ fn list_installed_packages(filename: &str) -> Result<Vec<PackageInfo>, Box<dyn E
} else if current_package.is_none() {
current_package = Some((line, None));
} else {
let package = current_package.as_mut().unwrap();
let package = current_package
.as_mut()
.expect("Error: no package was evaluated");
if package.1.is_none() {
package.1 = Some(line);
}
Expand All @@ -102,11 +108,17 @@ fn list_installed_packages(filename: &str) -> Result<Vec<PackageInfo>, Box<dyn E
}

fn notify_mode(output: Vec<String>) -> Result<(), Box<dyn Error>> {
libnotify::init("scun").unwrap();
if let Err(e) = libnotify::init("scun") {
eprintln!("Failed to initialize libnotify: {}", e);
return Err(e.into());
}
let notification_body: String = output.join("\n");
let n = libnotify::Notification::new("Port Updates", &notification_body as &str, None);
n.set_timeout(5000);
n.show().unwrap();
if let Err(e) = n.show() {
eprintln!("Failed to show notification: {}", e);
return Err(e.into());
}
libnotify::uninit();

Ok(())
Expand Down Expand Up @@ -157,11 +169,11 @@ fn main() -> Result<(), Box<dyn Error>> {
let args: Vec<String> = env::args().collect();

if args.len() < 2 {
let message = "Usage: scun [notify|print]
let message = r#"Usage: scun [notify|print]
notify: send a list via libnotify
print: prints the number of available updates
--icon|-i: adds an icon
--long|-l: prints the whole list";
--long|-l: prints the whole list"#;
eprintln!("{message}");
process::exit(1);
}
Expand All @@ -173,9 +185,9 @@ print: prints the number of available updates
_ => Option::None,
};

if mode == "notify" {
if mode == "notify" || mode == "n" {
notify_mode(output)?;
} else if mode == "print" {
} else if mode == "print" || mode == "p" {
print_mode(output, submode)?;
} else {
eprintln!("Invalid mode: {mode}. Use 'notify' or 'print'.");
Expand Down

0 comments on commit 9d341e9

Please sign in to comment.