Upgrade to latest Rust (s/~str/StrBuf/)

See https://github.com/mozilla/rust/pull/14310
This commit is contained in:
Ben S 2014-05-24 01:14:40 +01:00
parent f65d170b48
commit fdf58e887b
5 changed files with 28 additions and 26 deletions

View File

@ -16,19 +16,20 @@ pub struct StyleStruct {
} }
impl Style { impl Style {
pub fn paint(&self, input: ~str) -> ~str { pub fn paint(&self, input: &str) -> StrBuf {
match *self { match *self {
Plain => input, Plain => input.to_strbuf(),
Foreground(c) => c.paint(input), Foreground(c) => c.paint(input),
Style(s) => match s { Style(s) => match s {
StyleStruct { foreground, background, bold, underline } => { StyleStruct { foreground, background, bold, underline } => {
let bg: ~str = match background { let bg = match background {
Some(c) => format!("{};", c as int + 10), Some(c) => format!("{};", c as int + 10),
None => "".to_owned(), None => "".to_strbuf()
}; };
let bo = if bold { "1;" } else { "" }; let bo = if bold { "1;" } else { "" };
let un = if underline { "4;" } else { "" }; let un = if underline { "4;" } else { "" };
format!("\x1B[{}{}{}{}m{}\x1B[0m", bo, un, bg, foreground as int, input) let re = format!("\x1B[{}{}{}{}m{}\x1B[0m", bo, un, bg, foreground as int, input.to_strbuf());
return re.to_owned();
} }
} }
} }
@ -62,8 +63,9 @@ impl Style {
} }
impl Colour { impl Colour {
pub fn paint(&self, input: &str) -> ~str { pub fn paint(&self, input: &str) -> StrBuf {
format!("\x1B[{}m{}\x1B[0m", *self as int, input) let re = format!("\x1B[{}m{}\x1B[0m", *self as int, input);
return re.to_owned();
} }
pub fn underline(&self) -> Style { pub fn underline(&self) -> Style {
@ -83,7 +85,7 @@ impl Colour {
} }
} }
pub fn strip_formatting(input: &~str) -> ~str { pub fn strip_formatting(input: &StrBuf) -> StrBuf {
let re = regex!("\x1B\\[.+?m"); let re = regex!("\x1B\\[.+?m");
re.replace_all(*input, "").to_owned() re.replace_all(input.as_slice(), "").to_owned()
} }

4
exa.rs
View File

@ -58,7 +58,7 @@ fn list(opts: Options, path: Path) {
let columns = defaultColumns(); let columns = defaultColumns();
let table: Vec<Vec<~str>> = files.iter() let table: Vec<Vec<StrBuf>> = files.iter()
.map(|p| File::from_path(p)) .map(|p| File::from_path(p))
.filter(|f| !f.is_dotfile() || opts.showInvisibles ) .filter(|f| !f.is_dotfile() || opts.showInvisibles )
.map(|f| columns.iter().map(|c| f.display(c)).collect()) .map(|f| columns.iter().map(|c| f.display(c)).collect())
@ -76,7 +76,7 @@ fn list(opts: Options, path: Path) {
} else { } else {
print!(" "); print!(" ");
} }
print!("{}", cell); print!("{}", cell.as_slice());
for _ in range(cell.len(), *length) { for _ in range(cell.len(), *length) {
print!(" "); print!(" ");
} }

20
file.rs
View File

@ -35,17 +35,17 @@ impl<'a> File<'a> {
self.name.starts_with(".") self.name.starts_with(".")
} }
pub fn display(&self, column: &Column) -> ~str { pub fn display(&self, column: &Column) -> StrBuf {
match *column { match *column {
Permissions => self.permissions(), Permissions => self.permissions(),
FileName => self.file_colour().paint(self.name.to_owned()), FileName => self.file_colour().paint(self.name.as_slice()),
FileSize(si) => self.file_size(si), FileSize(si) => self.file_size(si),
User => get_user_name(self.stat.unstable.uid as i32).unwrap_or(self.stat.unstable.uid.to_str()), User => get_user_name(self.stat.unstable.uid as i32).unwrap_or(self.stat.unstable.uid.to_str()),
Group => get_group_name(self.stat.unstable.gid as u32).unwrap_or(self.stat.unstable.gid.to_str()), Group => get_group_name(self.stat.unstable.gid as u32).unwrap_or(self.stat.unstable.gid.to_str()),
} }
} }
fn file_size(&self, si: bool) -> ~str { fn file_size(&self, si: bool) -> StrBuf {
let sizeStr = if si { let sizeStr = if si {
formatBinaryBytes(self.stat.size) formatBinaryBytes(self.stat.size)
} else { } else {
@ -56,12 +56,12 @@ impl<'a> File<'a> {
Green.normal() Green.normal()
} else { } else {
Green.bold() Green.bold()
}.paint(sizeStr); }.paint(sizeStr.as_slice());
} }
fn type_char(&self) -> ~str { fn type_char(&self) -> StrBuf {
return match self.stat.kind { return match self.stat.kind {
io::TypeFile => ".".to_owned(), io::TypeFile => ".".to_strbuf(),
io::TypeDirectory => Blue.paint("d"), io::TypeDirectory => Blue.paint("d"),
io::TypeNamedPipe => Yellow.paint("|"), io::TypeNamedPipe => Yellow.paint("|"),
io::TypeBlockSpecial => Purple.paint("s"), io::TypeBlockSpecial => Purple.paint("s"),
@ -83,7 +83,7 @@ impl<'a> File<'a> {
} }
} }
fn permissions(&self) -> ~str { fn permissions(&self) -> StrBuf {
let bits = self.stat.perm; let bits = self.stat.perm;
return format!("{}{}{}{}{}{}{}{}{}{}", return format!("{}{}{}{}{}{}{}{}{}{}",
self.type_char(), self.type_char(),
@ -100,10 +100,10 @@ impl<'a> File<'a> {
} }
} }
fn bit(bits: io::FilePermission, bit: io::FilePermission, other: &'static str, style: Style) -> ~str { fn bit(bits: io::FilePermission, bit: io::FilePermission, other: &'static str, style: Style) -> StrBuf {
if bits.contains(bit) { if bits.contains(bit) {
style.paint(other.to_owned()) style.paint(other.as_slice())
} else { } else {
Black.bold().paint("-".to_owned()) Black.bold().paint("-".as_slice())
} }
} }

View File

@ -1,4 +1,4 @@
fn formatBytes(mut amount: u64, kilo: u64, prefixes: ~[&str]) -> ~str { fn formatBytes(mut amount: u64, kilo: u64, prefixes: ~[&str]) -> StrBuf {
let mut prefix = 0; let mut prefix = 0;
while amount > kilo { while amount > kilo {
amount /= kilo; amount /= kilo;
@ -7,10 +7,10 @@ fn formatBytes(mut amount: u64, kilo: u64, prefixes: ~[&str]) -> ~str {
return format!("{:4}{}", amount, prefixes[prefix]); return format!("{:4}{}", amount, prefixes[prefix]);
} }
pub fn formatBinaryBytes(amount: u64) -> ~str { pub fn formatBinaryBytes(amount: u64) -> StrBuf {
formatBytes(amount, 1024, ~[ "B ", "KiB", "MiB", "GiB", "TiB" ]) formatBytes(amount, 1024, ~[ "B ", "KiB", "MiB", "GiB", "TiB" ])
} }
pub fn formatDecimalBytes(amount: u64) -> ~str { pub fn formatDecimalBytes(amount: u64) -> StrBuf {
formatBytes(amount, 1000, ~[ "B ", "KB", "MB", "GB", "TB" ]) formatBytes(amount, 1000, ~[ "B ", "KB", "MB", "GB", "TB" ])
} }

View File

@ -34,7 +34,7 @@ mod c {
} }
} }
pub fn get_user_name(uid: i32) -> Option<~str> { pub fn get_user_name(uid: i32) -> Option<StrBuf> {
let pw = unsafe { c::getpwuid(uid) }; let pw = unsafe { c::getpwuid(uid) };
if pw.is_not_null() { if pw.is_not_null() {
return unsafe { Some(from_c_str(read(pw).pw_name)) }; return unsafe { Some(from_c_str(read(pw).pw_name)) };
@ -44,7 +44,7 @@ pub fn get_user_name(uid: i32) -> Option<~str> {
} }
} }
pub fn get_group_name(gid: u32) -> Option<~str> { pub fn get_group_name(gid: u32) -> Option<StrBuf> {
let gr = unsafe { c::getgrgid(gid) }; let gr = unsafe { c::getgrgid(gid) };
if gr.is_not_null() { if gr.is_not_null() {
return unsafe { Some(from_c_str(read(gr).gr_name)) }; return unsafe { Some(from_c_str(read(gr).gr_name)) };