Skip to content

Commit

Permalink
Move methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 7, 2024
1 parent f5e2c03 commit 725a36f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
24 changes: 1 addition & 23 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod jobtable;

use std::collections::HashMap;
use std::os::fd::RawFd;
use std::{io, process, env, path};
use std::{process, env, path};
use nix::{fcntl, unistd};
use nix::sys::{signal, wait};
use nix::sys::signal::{Signal, SigHandler};
Expand Down Expand Up @@ -187,26 +187,4 @@ impl ShellCore {
self.set_subshell_vars();
self.job_table.clear();
}

pub fn get_current_directory(&mut self) -> &Option<path::PathBuf> {
if self.tcwd.is_none() {
match env::current_dir() {
Ok(path) => {
self.tcwd = Some(path);
},
Err(err) => {
eprintln!("pwd: error retrieving current directory: {:?}", err);
}
}
}
&self.tcwd
}

pub fn set_current_directory(&mut self, path: &path::PathBuf) -> Result<(), io::Error> {
let res = env::set_current_dir(path);
if res.is_ok() {
self.tcwd = Some(path.clone());
}
res
}
}
23 changes: 23 additions & 0 deletions src/core/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod pwd;
mod utils;

use crate::ShellCore;
use std::{io, env, path};

impl ShellCore {
pub fn set_builtins(&mut self) {
Expand All @@ -17,6 +18,28 @@ impl ShellCore {
self.builtins.insert("pwd".to_string(), pwd::pwd);
self.builtins.insert("true".to_string(), true_);
}

pub fn get_current_directory(&mut self) -> &Option<path::PathBuf> {
if self.tcwd.is_none() {
match env::current_dir() {
Ok(path) => {
self.tcwd = Some(path);
},
Err(err) => {
eprintln!("pwd: error retrieving current directory: {:?}", err);
}
}
}
&self.tcwd
}

pub fn set_current_directory(&mut self, path: &path::PathBuf) -> Result<(), io::Error> {
let res = env::set_current_dir(path);
if res.is_ok() {
self.tcwd = Some(path.clone());
}
res
}
}

pub fn exit(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
Expand Down

0 comments on commit 725a36f

Please sign in to comment.