aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2018-05-14 17:22:50 -0400
committerJesse Luehrs <doy@tozt.net>2018-05-14 17:22:50 -0400
commit3888214fe87b27be5d5423a5c80eb4f17958b9d2 (patch)
tree1097c89800e7db663f8b8bbd65a3b4457d37156a
parent4c29fe2213473b175dbb7672561cba3375419a41 (diff)
downloadfancy-prompt-3888214fe87b27be5d5423a5c80eb4f17958b9d2.tar.gz
fancy-prompt-3888214fe87b27be5d5423a5c80eb4f17958b9d2.zip
factor out os-specific calls
also use the unix form instead of linux to hopefully fix osx
-rw-r--r--src/main.rs1
-rw-r--r--src/prompt.rs40
-rw-r--r--src/sys/mod.rs10
-rw-r--r--src/sys/unix.rs34
4 files changed, 55 insertions, 30 deletions
diff --git a/src/main.rs b/src/main.rs
index 146e633..84fceea 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -17,6 +17,7 @@ mod colors;
mod data;
mod power;
mod prompt;
+mod sys;
mod vcs;
fn main() {
diff --git a/src/prompt.rs b/src/prompt.rs
index 706ed04..620bea5 100644
--- a/src/prompt.rs
+++ b/src/prompt.rs
@@ -1,13 +1,11 @@
use regex;
use std;
-use users;
use std::fmt::Write;
-use std::os::linux::fs::MetadataExt;
-use std::os::unix::fs::PermissionsExt;
use colors;
use data;
+use sys;
use vcs;
pub struct Prompt {
@@ -242,33 +240,15 @@ fn battery_discharge_color(usage: f64, charging: bool) -> &'static str {
fn path_color(path: Option<&std::path::Path>) -> String {
path.as_ref()
- .and_then(|path| {
- std::fs::metadata(path)
- .map(|stat| {
- // XXX there really has to be a better option here
- let euid = users::get_effective_uid();
- let egid = users::get_effective_gid();
- let file_uid = stat.st_uid();
- let file_gid = stat.st_gid();
- let file_mode = stat.permissions().mode();
-
- if euid == 0 {
- String::from("default")
- }
- else if (file_uid == euid) && (file_mode & 0o200 != 0) {
- String::from("default")
- }
- else if (file_gid == egid) && (file_mode & 0o020 != 0) {
- String::from("default")
- }
- else if file_mode & 0o002 != 0 {
- String::from("default")
- }
- else {
- String::from("path_not_writable")
- }
- })
- .ok()
+ .map(|path| {
+ match sys::path_writable(path) {
+ sys::PathWritability::Writable
+ => String::from("default"),
+ sys::PathWritability::NotWritable
+ => String::from("path_not_writable"),
+ sys::PathWritability::NotExist
+ => String::from("path_not_exist"),
+ }
})
.unwrap_or_else(|| String::from("path_not_exist"))
}
diff --git a/src/sys/mod.rs b/src/sys/mod.rs
new file mode 100644
index 0000000..a63f171
--- /dev/null
+++ b/src/sys/mod.rs
@@ -0,0 +1,10 @@
+#[cfg(unix)]
+mod unix;
+#[cfg(unix)]
+pub use self::unix::*;
+
+pub enum PathWritability {
+ Writable,
+ NotWritable,
+ NotExist,
+}
diff --git a/src/sys/unix.rs b/src/sys/unix.rs
new file mode 100644
index 0000000..0f433b3
--- /dev/null
+++ b/src/sys/unix.rs
@@ -0,0 +1,34 @@
+use users;
+use std;
+
+use std::os::unix::fs::MetadataExt;
+use std::os::unix::fs::PermissionsExt;
+
+pub fn path_writable(path: &std::path::Path) -> super::PathWritability {
+ std::fs::metadata(path)
+ .map(|stat| {
+ // XXX there really has to be a better option here
+ let euid = users::get_effective_uid();
+ let egid = users::get_effective_gid();
+ let file_uid = stat.uid();
+ let file_gid = stat.gid();
+ let file_mode = stat.permissions().mode();
+
+ if euid == 0 {
+ super::PathWritability::Writable
+ }
+ else if (file_uid == euid) && (file_mode & 0o200 != 0) {
+ super::PathWritability::Writable
+ }
+ else if (file_gid == egid) && (file_mode & 0o020 != 0) {
+ super::PathWritability::Writable
+ }
+ else if file_mode & 0o002 != 0 {
+ super::PathWritability::Writable
+ }
+ else {
+ super::PathWritability::NotWritable
+ }
+ })
+ .unwrap_or(super::PathWritability::NotExist)
+}