Skip to content

Commit

Permalink
Short descriptions for all commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Arsynth committed May 30, 2023
1 parent 30fbff1 commit 696c0a9
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 15 deletions.
21 changes: 14 additions & 7 deletions src/commands/common/help_string_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ use super::EXEC_NAME;

pub(crate) struct HelpStringBuilder {
command: String,
title: Option<String>,
pub(crate) description: Option<String>,
pub(crate) show_usage_title: bool,
items: Vec<OptionItem>,
}

impl HelpStringBuilder {
pub(crate) fn new(command: String, title: Option<String>) -> Self {
pub(crate) fn new(command: String) -> Self {
Self {
command,
title,
description: None,
show_usage_title: false,
items: Vec::new(),
}
}
Expand All @@ -25,11 +27,16 @@ impl HelpStringBuilder {
}

pub(crate) fn build_string(&self) -> String {
let title = match &self.title {
Some(title) => format!("{}:\n", title),
None => "".to_string(),
let description = match &self.description {
Some(description) => format!("\nDescription:\n{} - {}\n", self.command.bright_green(), description.bright_white()),
None => "".into(),
};
let mut result = format! {"{title}{EXEC_NAME} {} {} ", self.command, "FILE".bright_white()};

let usage = match &self.show_usage_title {
true => format!("\nUsage:\n"),
false => "".into(),
};
let mut result = format! {"{description}{usage}{EXEC_NAME} {} {} ", self.command.bright_green(), "FILE".bright_white()};

let arg_list: Vec<String> = self
.items
Expand Down
4 changes: 4 additions & 0 deletions src/commands/dylibs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ impl Handler for DylibsHandler {
SUBCOMM_NAME.to_string()
}

fn description(&self) -> String {
"Prints used dynamic libraries".to_string()
}

fn can_handle_with_name(&self, name: &str) -> bool {
SUBCOMM_NAME == name
}
Expand Down
4 changes: 4 additions & 0 deletions src/commands/fat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ impl Handler for ArchsHandler {
SUBCOMM_NAME.to_string()
}

fn description(&self) -> String {
"Prints all fat headers if they exists".to_string()
}

fn can_handle_with_name(&self, name: &str) -> bool {
SUBCOMM_NAME == name
}
Expand Down
1 change: 1 addition & 0 deletions src/commands/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use super::common::*;

pub(super) trait Handler {
fn command_name(&self) -> String;
fn description(&self) -> String;

fn can_handle_with_name(&self, name: &str) -> bool;
/// Function takes remainder of args, that it, without exec and subcommand names
Expand Down
4 changes: 4 additions & 0 deletions src/commands/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ impl Handler for HeadersHandler {
SUBCOMM_NAME.to_string()
}

fn description(&self) -> String {
"Prints object headers in file".to_string()
}

fn can_handle_with_name(&self, name: &str) -> bool {
SUBCOMM_NAME == name
}
Expand Down
18 changes: 10 additions & 8 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ pub fn handle_with_args() -> Result<()> {
}

let handler = matched_handler(&args[1]);
let (command_name, mut option_items) = match &handler {
Some(h) => (Some(h.command_name()), h.accepted_option_items()),
None => (None, handler::default_option_items()),
let (command_name, description, mut option_items) = match &handler {
Some(h) => (Some(h.command_name()), h.description(), h.accepted_option_items()),
None => (None, "".to_string(), handler::default_option_items()),
};

let mut opts = Options::new();
option_items.add_to_opts(&mut opts);

let help_request = match command_name {
Some(command_name) => Some(HelpStringRequest(command_name.clone(), &mut option_items)),
Some(command_name) => Some(HelpStringRequest(command_name.clone(), description, &mut option_items)),
None => None,
};

Expand Down Expand Up @@ -105,20 +105,22 @@ pub fn handle_with_args() -> Result<()> {
}
}

struct HelpStringRequest<'a>(String, &'a mut Vec<OptionItem>);
struct HelpStringRequest<'a>(String, String, &'a mut Vec<OptionItem>);

fn help_string(request: Option<HelpStringRequest>) -> String {
match request {
Some(request) => {
let mut help_string_builder =
HelpStringBuilder::new(request.0.clone(), Some("Usage".to_string()));
help_string_builder.add_option_items(request.1);
HelpStringBuilder::new(request.0.clone());
help_string_builder.description = Some(request.1);
help_string_builder.show_usage_title = true;
help_string_builder.add_option_items(request.2);
help_string_builder.build_string()
}
None => {
let mut result = "".to_string();
for handler in available_handlers() {
let mut help_string_builder = HelpStringBuilder::new(handler.command_name(), None);
let mut help_string_builder = HelpStringBuilder::new(handler.command_name());
help_string_builder.add_option_items(&mut handler.accepted_option_items());
result += &help_string_builder.build_string();
result += "\n";
Expand Down
4 changes: 4 additions & 0 deletions src/commands/rpaths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ impl Handler for RpathsHandler {
SUBCOMM_NAME.to_string()
}

fn description(&self) -> String {
"Prints all relative paths".to_string()
}

fn can_handle_with_name(&self, name: &str) -> bool {
SUBCOMM_NAME == name
}
Expand Down
4 changes: 4 additions & 0 deletions src/commands/segs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ impl Handler for SegsHandler {
SUBCOMM_NAME.to_string()
}

fn description(&self) -> String {
"Prints segment commands with sections".to_string()
}

fn can_handle_with_name(&self, name: &str) -> bool {
SUBCOMM_NAME == name
}
Expand Down
4 changes: 4 additions & 0 deletions src/commands/syms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ impl Handler for SymsHandler {
SUBCOMM_NAME.to_string()
}

fn description(&self) -> String {
"Prints all symbols".to_string()
}

fn can_handle_with_name(&self, name: &str) -> bool {
SUBCOMM_NAME == name
}
Expand Down

0 comments on commit 696c0a9

Please sign in to comment.