cirho / powerline-rust

powerline-shell written in Rust
MIT License
50 stars 25 forks source link

[CWD] Change depth calculation in get_segments #8

Closed Jesse-Millwood closed 5 years ago

Jesse-Millwood commented 5 years ago

When one is subtracted from the count of forward slashes in the cwd path and cwd is the home directory a 'attempt to subtract with overflow' runtime error occurs.

Jesse-Millwood commented 5 years ago

This snippet shows the functionality that I was able to isolate:

extern crate powerline;
extern crate dirs;
use powerline::part::Part;
use powerline::segments::*;
use std::env;
use std::path;

#[test]
fn cwd() {

    let current_dir = path::PathBuf::from(env::var("HOME").unwrap());
    println!("Current Directory: {:?}", current_dir);
    let mut cwd = current_dir.to_str().unwrap();
    println!("CWD: {:?}", cwd);
    if let Some(home_path) = env::home_dir() {
        let home_str = home_path.to_str().unwrap();
        if cwd.starts_with(home_str) {
            println!("in home directory");
            println!("home_str: {:?} ({})", home_str, home_str.len());
            cwd = &cwd[home_str.len() - 1..]
        }
    }
    println!("CWD: {:?}", cwd);
    let depth = cwd.matches("/").count() - 1;
    println!("depth: {}", depth);

}
cirho commented 5 years ago

Thanks for discovering it and submitting a pull request. I will resolve this issue in a few hours.

On Wed, Mar 20, 2019, 12:48 Jesse Millwood notifications@github.com wrote:

This snippet shows the functionality that I was able to isolate:

extern crate powerline; extern crate dirs; use powerline::part::Part; use powerline::segments::*; use std::env; use std::path;

[test]

fn cwd() {

let current_dir = path::PathBuf::from(env::var("HOME").unwrap());
println!("Current Directory: {:?}", current_dir);
let mut cwd = current_dir.to_str().unwrap();
println!("CWD: {:?}", cwd);
if let Some(home_path) = env::home_dir() {
    let home_str = home_path.to_str().unwrap();
    if cwd.starts_with(home_str) {
        println!("in home directory");
        println!("home_str: {:?} ({})", home_str, home_str.len());
        cwd = &cwd[home_str.len() - 1..]
    }
}
println!("CWD: {:?}", cwd);
let depth = cwd.matches("/").count() - 1;
println!("depth: {}", depth);

}

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Xeoeen/powerline-rust/pull/8#issuecomment-474796427, or mute the thread https://github.com/notifications/unsubscribe-auth/AQK504yed6Y7AFEgDFschlw6uwu9wPxhks5vYiAFgaJpZM4b9mA- .