Skip to content

Commit

Permalink
Change order
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Dec 31, 2023
1 parent db1ccc6 commit 58766c4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
36 changes: 18 additions & 18 deletions src/core/builtins/cd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@
use crate::ShellCore;
use super::utils;

pub fn cd(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
if args.len() > 2 {
eprintln!("sush: cd: too many arguments");
return 1;
}

if args.len() == 1 { //only "cd"
return cd_1arg(core, args);
}

if args[1] == "-" { // cd -
cd_oldpwd(core, args)
}else{ // cd /some/dir
set_oldpwd(core);
change_directory(core, args)
}
}

fn cd_1arg(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
let var = "~".to_string();
args.push(var);
Expand Down Expand Up @@ -41,21 +59,3 @@ fn change_directory(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
1
}
}

pub fn cd(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
if args.len() > 2 {
eprintln!("sush: cd: too many arguments");
return 1;
}

if args.len() == 1 { //only "cd"
return cd_1arg(core, args);
}

if args[1] == "-" { // cd -
cd_oldpwd(core, args)
}else{ // cd /some/dir
set_oldpwd(core);
change_directory(core, args)
}
}
26 changes: 13 additions & 13 deletions src/core/builtins/pwd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@

use crate::ShellCore;

fn show_pwd(core: &mut ShellCore, physical: bool) -> i32 {
if let Some(mut path) = core.get_current_directory().clone() {
if physical && path.is_symlink() {
if let Ok(c) = path.canonicalize() {
path = c;
}
}
println!("{}", path.display());
return 0;
}
1
}

pub fn pwd(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
if args.len() == 1 || &args[1][..1] != "-" { // $ pwd, $ pwd aaa
return show_pwd(core, false);
Expand All @@ -32,3 +19,16 @@ pub fn pwd(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
},
}
}

fn show_pwd(core: &mut ShellCore, physical: bool) -> i32 {
if let Some(mut path) = core.get_current_directory().clone() {
if physical && path.is_symlink() {
if let Ok(c) = path.canonicalize() {
path = c;
}
}
println!("{}", path.display());
return 0;
}
1
}

0 comments on commit 58766c4

Please sign in to comment.