Last active
August 29, 2015 14:11
-
-
Save folsen/4d9a2acb8e0aa509f9d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(phase)] | |
#[phase(plugin)] | |
extern crate regex_macros; | |
extern crate regex; | |
use std::io::Command; | |
use std::fmt; | |
struct Process { | |
pid : String, | |
tty : String, | |
time : String, | |
binary : String | |
} | |
impl fmt::Show for Process { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
write!(f, "{}\t{}\t{}\t{}", | |
self.pid, | |
self.tty, | |
self.time, | |
self.binary) | |
} | |
} | |
fn str_to_process(ps: &&str) -> Process { | |
let re = regex!(r"[ ]+"); | |
let strs: Vec<&str> = re.split((*ps).trim_chars(' ')).collect(); | |
return Process { | |
pid : strs[0].into_string(), | |
tty : strs[1].into_string(), | |
time : strs[2].into_string(), | |
binary : strs[3].into_string() | |
} | |
} | |
fn main() { | |
let output = match Command::new("ps").arg("-A").output() { | |
Ok(output) => output, | |
Err(e) => panic!("failed to execute `ps -A`: {}", e), | |
}; | |
let raw_output = String::from_utf8_lossy(output.output.as_slice()).into_string(); | |
let raw_ps: Vec<&str> = raw_output.lines().collect(); | |
let ps : Vec<Process> = raw_ps.iter().map(str_to_process).collect(); | |
let ps_slice : &[Process] = ps.slice_from_or_fail(&1); // I really just want to drop 1 from the ps vec | |
for p in ps_slice.iter() { | |
println!("{}", p); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment