Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DanNixon committed Aug 19, 2024
1 parent ee1d689 commit 94e93d1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
29 changes: 1 addition & 28 deletions overlays/pkgs/sway-helper/src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::CliRun;
use std::{os::linux::fs::MetadataExt, path::Path};
use crate::helpers::programs::list_programs_from_path;

pub(crate) struct Run {}

Expand All @@ -15,30 +15,3 @@ impl CliRun for Run {
sway.run_command(format!("exec \"{program}\"")).unwrap();
}
}

fn is_executable(path: &Path) -> bool {
match std::fs::metadata(path) {
Ok(meta) => (meta.st_mode() & 0o111) != 0,
Err(_) => false,
}
}

fn list_programs_from_path() -> Vec<String> {
std::env::var("PATH")
.expect("PATH environment variable should be set")
.split(':')
.flat_map(|path| match std::fs::read_dir(path) {
Ok(entries) => Some(entries.into_iter().flat_map(|e| e.ok()).collect::<Vec<_>>()),
Err(_) => None,
})
.flatten()
.flat_map(|entry| {
let path = entry.path();
if path.is_file() && is_executable(&path) {
Some(path.file_name().unwrap().to_str().unwrap().to_owned())
} else {
None
}
})
.collect()
}
27 changes: 27 additions & 0 deletions overlays/pkgs/sway-helper/src/helpers/programs.rs
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
use std::{os::linux::fs::MetadataExt, path::Path};

pub(crate) fn list_programs_from_path() -> Vec<String> {
std::env::var("PATH")
.expect("PATH environment variable should be set")
.split(':')
.flat_map(|path| match std::fs::read_dir(path) {
Ok(entries) => Some(entries.into_iter().flat_map(|e| e.ok()).collect::<Vec<_>>()),
Err(_) => None,
})
.flatten()
.flat_map(|entry| {
let path = entry.path();
if path.is_file() && is_executable(&path) {
Some(path.file_name().unwrap().to_str().unwrap().to_owned())
} else {
None
}
})
.collect()
}

fn is_executable(path: &Path) -> bool {
match std::fs::metadata(path) {
Ok(meta) => (meta.st_mode() & 0o111) != 0,
Err(_) => false,
}
}

0 comments on commit 94e93d1

Please sign in to comment.