Skip to content
Snippets Groups Projects
Commit 02f8587f authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Fix env::args to match libstd

parent fc2fc688
No related branches found
No related tags found
No related merge requests found
......@@ -8,7 +8,10 @@ use std::io::{Read, Write};
use std::env;
use std::time::Duration;
use std::process;
use std::to_num::ToNum;
use self::to_num::ToNum;
pub mod to_num;
macro_rules! readln {
() => ({
......@@ -72,7 +75,7 @@ impl<'a> Command<'a> {
main: Box::new(|args: &Vec<String>| {
match args.get(1) {
Some(path) => {
if ! env::set_current_dir(&path) {
if env::set_current_dir(&path).is_err() {
println!("Bad path: {}", path);
}
}
......@@ -618,9 +621,16 @@ impl<'a> Application<'a> {
/// Run the application
pub fn main(&mut self) {
println!("Type help for a command list");
if let Some(arg) = env::args().get(1) {
for arg in env::args().skip(1) {
let cwd = match env::current_dir() {
Ok(path) => format!("{}", &path),
Err(_) => "?".to_string()
};
let command = "run ".to_string() + arg;
println!("user@redox:{}# {}", &env::current_dir().unwrap_or("?".to_string()), command);
println!("user@redox:{}# {}", cwd, command);
self.on_command(&command);
}
......@@ -632,7 +642,14 @@ impl<'a> Application<'a> {
print!("- ");
}
}
print!("user@redox:{}# ", &env::current_dir().unwrap_or("?".to_string()));
let cwd = match env::current_dir() {
Ok(path) => format!("{}", &path),
Err(_) => "?".to_string()
};
print!("user@redox:{}# ", cwd);
if let Some(command_original) = readln!() {
let command = command_original.trim();
if command == "exit" {
......
//! Types convertable to integers
/// Parse the string to a integer using a given radix
pub trait ToNum {
fn to_num_radix(&self, radix: usize) -> usize;
fn to_num_radix_signed(&self, radix: usize) -> isize;
fn to_num(&self) -> usize;
fn to_num_signed(&self) -> isize;
}
impl ToNum for str {
fn to_num_radix(&self, radix: usize) -> usize {
if radix == 0 {
return 0;
}
let mut num = 0;
for c in self.chars() {
let digit;
if c >= '0' && c <= '9' {
digit = c as usize - '0' as usize
} else if c >= 'A' && c <= 'Z' {
digit = c as usize - 'A' as usize + 10
} else if c >= 'a' && c <= 'z' {
digit = c as usize - 'a' as usize + 10
} else {
break;
}
if digit >= radix {
break;
}
num *= radix;
num += digit;
}
num
}
/// Parse the string as a signed integer using a given radix
fn to_num_radix_signed(&self, radix: usize) -> isize {
if self.starts_with('-') {
-(self[1 ..].to_num_radix(radix) as isize)
} else {
self.to_num_radix(radix) as isize
}
}
/// Parse it as a unsigned integer in base 10
fn to_num(&self) -> usize {
self.to_num_radix(10)
}
/// Parse it as a signed integer in base 10
fn to_num_signed(&self) -> isize {
self.to_num_radix_signed(10)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment