Skip to content

Commit

Permalink
feat: include more details in version info (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
reubeno authored Jul 2, 2024
1 parent fb61360 commit c9b5363
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 6 deletions.
48 changes: 48 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions brush-core/src/builtins/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ pub(crate) struct HelpCommand {
topic_patterns: Vec<String>,
}

const VERSION: &str = env!("CARGO_PKG_VERSION");

#[async_trait::async_trait]
impl builtin::Command for HelpCommand {
async fn execute(
Expand All @@ -48,7 +46,9 @@ impl HelpCommand {
) -> Result<(), crate::error::Error> {
const COLUMN_COUNT: usize = 3;

writeln!(context.stdout(), "brush version {VERSION}\n")?;
if let Some(display_str) = &context.shell.shell_product_display_str {
writeln!(context.stdout(), "{display_str}\n")?;
}

writeln!(
context.stdout(),
Expand All @@ -62,7 +62,8 @@ impl HelpCommand {
for j in 0..COLUMN_COUNT {
if let Some((name, builtin)) = builtins.get(i + j * items_per_column) {
let prefix = if builtin.disabled { "*" } else { " " };
write!(context.stdout(), " {prefix}{name:<20}")?; // adjust 20 to the desired column width
write!(context.stdout(), " {prefix}{name:<20}")?; // adjust 20 to the desired
// column width
}
}
writeln!(context.stdout())?;
Expand Down
7 changes: 7 additions & 0 deletions brush-core/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ pub struct Shell {
/// Shell name
pub shell_name: Option<String>,

/// Detailed display string for the shell
pub shell_product_display_str: Option<String>,

/// Script call stack.
pub script_call_stack: VecDeque<String>,

Expand Down Expand Up @@ -84,6 +87,7 @@ impl Clone for Shell {
last_exit_status: self.last_exit_status,
positional_parameters: self.positional_parameters.clone(),
shell_name: self.shell_name.clone(),
shell_product_display_str: self.shell_product_display_str.clone(),
function_call_stack: self.function_call_stack.clone(),
script_call_stack: self.script_call_stack.clone(),
directory_stack: self.directory_stack.clone(),
Expand Down Expand Up @@ -122,6 +126,8 @@ pub struct CreateOptions {
pub read_commands_from_stdin: bool,
/// The name of the shell.
pub shell_name: Option<String>,
/// Optionally provides a display string describing the version and variant of the shell.
pub shell_product_display_str: Option<String>,
/// Whether to run in maximal POSIX sh compatibility mode.
pub sh_mode: bool,
/// Whether to print verbose output.
Expand Down Expand Up @@ -163,6 +169,7 @@ impl Shell {
last_exit_status: 0,
positional_parameters: vec![],
shell_name: options.shell_name.clone(),
shell_product_display_str: options.shell_product_display_str.clone(),
function_call_stack: VecDeque::new(),
script_call_stack: VecDeque::new(),
directory_stack: vec![],
Expand Down
2 changes: 2 additions & 0 deletions brush-shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ brush-parser = { version = "^0.2.2", path = "../brush-parser" }
brush-core = { version = "^0.2.2", path = "../brush-core" }
# N.B. Pin to 4.4.18 for now to keep to 1.72.0 as MSRV; 4.5.x requires a later version.
clap = { version = "=4.4.18", features = ["derive", "wrap_help"] }
const_format = "0.2.32"
git-version = "0.3.9"
tokio = { version = "1.37.0", features = ["rt", "rt-multi-thread"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
Expand Down
10 changes: 9 additions & 1 deletion brush-shell/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
#![deny(missing_docs)]

mod productinfo;

use std::{collections::HashSet, io::IsTerminal, path::Path};

use clap::{builder::styling, Parser};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};

/// Parsed command-line arguments for the brush shell.
#[derive(Parser)]
#[clap(version, about, disable_help_flag = true, disable_version_flag = true, styles = brush_help_styles())]
#[clap(name = productinfo::PRODUCT_NAME,
version = const_format::concatcp!(productinfo::PRODUCT_VERSION, " (", productinfo::PRODUCT_GIT_VERSION, ")"),
about,
disable_help_flag = true,
disable_version_flag = true,
styles = brush_help_styles())]
struct CommandLineArgs {
/// Display usage information.
#[clap(long = "help", action = clap::ArgAction::HelpLong)]
Expand Down Expand Up @@ -254,6 +261,7 @@ async fn run(
print_commands_and_arguments: args.print_commands_and_arguments,
read_commands_from_stdin,
shell_name: argv0,
shell_product_display_str: Some(productinfo::get_product_display_str()),
sh_mode: args.sh_mode,
verbose: args.verbose,
},
Expand Down
27 changes: 27 additions & 0 deletions brush-shell/src/productinfo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! Information about this shell project.
/// The formal name of this product.
pub const PRODUCT_NAME: &str = "brush";

const PRODUCT_HOMEPAGE: &str = env!("CARGO_PKG_HOMEPAGE");
const PRODUCT_REPO: &str = env!("CARGO_PKG_REPOSITORY");

/// The URI to display as the product's homepage.
#[allow(clippy::const_is_empty)]
pub const PRODUCT_DISPLAY_URI: &str = if !PRODUCT_HOMEPAGE.is_empty() {
PRODUCT_HOMEPAGE
} else {
PRODUCT_REPO
};

/// The version of the product, in string form.
pub const PRODUCT_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Info regarding the specific version of sources used to build this product.
pub const PRODUCT_GIT_VERSION: &str = git_version::git_version!();

pub(crate) fn get_product_display_str() -> String {
std::format!(
"{PRODUCT_NAME} version {PRODUCT_VERSION} ({PRODUCT_GIT_VERSION}) - {PRODUCT_DISPLAY_URI}"
)
}
2 changes: 1 addition & 1 deletion deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ version = 2
# List of explicitly allowed licenses
# See https://spdx.org/licenses/ for list of possible licenses
# [possible values: any SPDX 3.11 short identifier (+ optional exception)].
allow = ["Apache-2.0", "BSD-3-Clause", "BSL-1.0", "MIT", "Unicode-DFS-2016"]
allow = ["Apache-2.0", "BSD-2-Clause", "BSD-3-Clause", "BSL-1.0", "MIT", "Unicode-DFS-2016", "Zlib"]
# The confidence threshold for detecting a license from license text.
# The higher the value, the more closely the license text must be to the
# canonical license text of a valid SPDX license file.
Expand Down

0 comments on commit c9b5363

Please sign in to comment.