Skip to content

Commit

Permalink
feat(builtin): add help documentation on all relevant builtins and fi…
Browse files Browse the repository at this point in the history
…x arg requirements on `configure`
  • Loading branch information
lthoerner committed Sep 13, 2023
1 parent 7effeea commit 233590d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 29 deletions.
2 changes: 1 addition & 1 deletion config/config.rush
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
truncation-factor: false
truncation: false
multiline-prompt: true
history-limit: false
show-errors: true
56 changes: 40 additions & 16 deletions src/exec/builtins/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ pub struct WorkingDirectoryArgs {}

#[derive(Parser, Debug)]
pub struct ChangeDirectoryArgs {
#[arg(help = "The path of the directory to switch to")]
pub path: PathBuf,
}

#[derive(Parser, Debug)]
pub struct ListDirectoryArgs {
#[arg(short = 'a', long = "all")]
#[arg(short = 'a', long = "all", help = "Show hidden files and directories")]
pub show_hidden: bool,
#[arg(help = "The path of the directory to read")]
pub path: Option<PathBuf>,
}

Expand All @@ -43,66 +45,82 @@ pub struct ClearTerminalArgs {}

#[derive(Parser, Debug)]
pub struct MakeFileArgs {
#[arg(help = "The path of the file to create")]
pub path: PathBuf,
}

#[derive(Parser, Debug)]
pub struct MakeDirectoryArgs {
#[arg(help = "The path of the directory to create")]
pub path: PathBuf,
}

#[derive(Parser, Debug)]
pub struct DeleteFileArgs {
#[arg(help = "The path of the file to delete")]
pub path: PathBuf,
}

#[derive(Parser, Debug)]
pub struct ReadFileArgs {
#[arg(help = "The path of the file to read")]
pub path: PathBuf,
}

#[derive(Parser, Debug)]
pub struct RunExecutableArgs {
#[arg(help = "The path to the executable")]
pub path: PathBuf,
#[arg(help = "The arguments to pass to the executable")]
pub arguments: Vec<String>,
}

#[derive(Parser, Debug)]
#[command(arg_required_else_help = true)]
pub struct ConfigureArgs {
#[arg(long = "truncation-factor")]
pub truncation_factor: Option<MaybeUsize>,
#[arg(long = "history-limit")]
#[arg(
long = "truncation",
help = "The number of characters to trim each directory name to in the prompt"
)]
pub truncation: Option<MaybeUsize>,
#[arg(
long = "history-limit",
help = "The maximum number of commands to store in the history"
)]
pub history_limit: Option<MaybeUsize>,
#[arg(long = "multiline-prompt")]
pub multiline_prompt: Option<FancyBool>,
#[arg(long = "show-errors")]
pub show_errors: Option<FancyBool>,
#[arg(
long = "multiline-prompt",
help = "Whether to display the prompt on multiple lines"
)]
pub multiline_prompt: Option<Bool>,
#[arg(long = "show-errors", help = "Whether to display error messages")]
pub show_errors: Option<Bool>,
}

#[derive(Debug, Clone)]
pub enum FancyBool {
pub enum Bool {
True,
False,
}

impl FromStr for FancyBool {
impl FromStr for Bool {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if TRUE_ARGS.contains(&s) {
Ok(FancyBool::True)
Ok(Bool::True)
} else if FALSE_ARGS.contains(&s) {
Ok(FancyBool::False)
Ok(Bool::False)
} else {
Err("invalid boolean value".to_owned())
}
}
}

impl From<FancyBool> for bool {
fn from(b: FancyBool) -> Self {
impl From<Bool> for bool {
fn from(b: Bool) -> Self {
match b {
FancyBool::True => true,
FancyBool::False => false,
Bool::True => true,
Bool::False => false,
}
}
}
Expand Down Expand Up @@ -137,6 +155,7 @@ impl From<MaybeUsize> for Option<usize> {

#[derive(Parser, Debug)]
pub struct EnvironmentVariableArgs {
#[arg(help = "The environment variable to display")]
pub variable: EnvVariable,
}

Expand Down Expand Up @@ -166,21 +185,26 @@ pub enum EditPathSubcommand {

#[derive(Args, Debug, Clone)]
pub struct AppendPathCommand {
#[arg(help = "The path to append to the PATH variable")]
pub path: PathBuf,
}

#[derive(Args, Debug, Clone)]
pub struct PrependPathCommand {
#[arg(help = "The path to prepend to the PATH variable")]
pub path: PathBuf,
}

#[derive(Args, Debug, Clone)]
pub struct InsertPathCommand {
#[arg(help = "The index to insert the provided path at")]
pub index: usize,
#[arg(help = "The path to insert into the PATH variable")]
pub path: PathBuf,
}

#[derive(Args, Debug, Clone)]
pub struct DeletePathCommand {
#[arg(help = "The index of the path to delete from the PATH variable")]
pub index: usize,
}
4 changes: 2 additions & 2 deletions src/exec/builtins/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ pub fn run_executable(shell: &mut ShellState, args: Vec<&str>) -> Result<()> {
pub fn configure(shell: &mut ShellState, args: Vec<&str>) -> Result<()> {
let arguments = clap_handle!(ConfigureArgs::try_parse_from(args));

if let Some(truncation_factor) = arguments.truncation_factor {
shell.config.truncation_factor = truncation_factor.into();
if let Some(truncation) = arguments.truncation {
shell.config.truncation = truncation.into();
}

if let Some(history_limit) = arguments.history_limit {
Expand Down
10 changes: 5 additions & 5 deletions src/state/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::errors::{Handle, Result};
/// Represents any settings for the shell, most of which can be configured by the user
pub struct Configuration {
/// The truncation length for the prompt
pub truncation_factor: Option<usize>,
pub truncation: Option<usize>,
/// How many directories to store in the back/forward history
pub history_limit: Option<usize>,
/// Whether to show the prompt tick on a new line
Expand All @@ -24,7 +24,7 @@ pub struct Configuration {
impl Default for Configuration {
fn default() -> Self {
Self {
truncation_factor: None,
truncation: None,
history_limit: None,
multiline_prompt: false,
show_errors: true,
Expand Down Expand Up @@ -61,11 +61,11 @@ impl Configuration {

// ? Should these be underscores instead of hyphens?
match key {
"truncation-factor" => {
"truncation" => {
if let Ok(length) = value.parse::<usize>() {
config.truncation_factor = Some(length);
config.truncation = Some(length);
} else if value == "false" {
config.truncation_factor = None;
config.truncation = None;
} else {
return Err(
file_err!(CouldNotReadFile: filename).set_context(&read_error_msg)
Expand Down
8 changes: 4 additions & 4 deletions src/state/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl Path {
/// Gets the shortened version of the path
/// If a truncation factor is provided, the path will be truncated
/// The shortened path will always have the home directory collapsed
pub fn collapse(&self, home_directory: &PathBuf, truncation_factor: Option<usize>) -> String {
pub fn collapse(&self, home_directory: &PathBuf, truncation: Option<usize>) -> String {
// ? Is there a less redundant way to write this?
let path = match self.absolute_path.strip_prefix(home_directory) {
Ok(path) => {
Expand All @@ -105,11 +105,11 @@ impl Path {
let directories: Vec<String> = path.split('/').map(|d| d.to_string()).collect();
let mut truncated_directories = Vec::new();

if let Some(factor) = truncation_factor {
if let Some(trunctation) = truncation {
for dir in directories {
let mut truncated_dir = dir.clone();
if dir.len() > factor {
truncated_dir.truncate(factor);
if dir.len() > trunctation {
truncated_dir.truncate(trunctation);
}

truncated_directories.push(truncated_dir);
Expand Down
2 changes: 1 addition & 1 deletion src/state/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl ShellState {
pub fn generate_prompt(&self) -> String {
let user = self.environment.USER.clone();
let home = &self.environment.HOME;
let truncation = self.config.truncation_factor;
let truncation = self.config.truncation;
let cwd = self.environment.CWD.collapse(home, truncation);
let prompt_delimiter = match self.config.multiline_prompt {
true => "\n",
Expand Down

0 comments on commit 233590d

Please sign in to comment.