perf(custom): evaluate command lazily (#2173)

This commit is contained in:
David Knaack 2021-01-20 19:01:49 +01:00 committed by GitHub
parent bb160d9207
commit 8302a3ccb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 41 deletions

View File

@ -2559,7 +2559,7 @@ If you have an interesting example not covered there, feel free to share it ther
### Options ### Options
| Option | Default | Description | | Option | Default | Description |
| ------------- | ----------------------------- | -------------------------------------------------------------------------------------------------------------------------- | | ------------- | ------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| `command` | | The command whose output should be printed. The command will be passed on stdin to the shell. | | `command` | | The command whose output should be printed. The command will be passed on stdin to the shell. |
| `when` | | A shell command used as a condition to show the module. The module will be shown if the command returns a `0` status code. | | `when` | | A shell command used as a condition to show the module. The module will be shown if the command returns a `0` status code. |
| `shell` | | [See below](#custom-command-shell) | | `shell` | | [See below](#custom-command-shell) |
@ -2569,7 +2569,7 @@ If you have an interesting example not covered there, feel free to share it ther
| `extensions` | `[]` | The extensions that will be searched in the working directory for a match. | | `extensions` | `[]` | The extensions that will be searched in the working directory for a match. |
| `symbol` | `""` | The symbol used before displaying the command output. | | `symbol` | `""` | The symbol used before displaying the command output. |
| `style` | `"bold green"` | The style for the module. | | `style` | `"bold green"` | The style for the module. |
| `format` | `"[$symbol$output]($style) "` | The format for the module. | | `format` | `"[$symbol($output )]($style)"` | The format for the module. |
| `disabled` | `false` | Disables this `custom` module. | | `disabled` | `false` | Disables this `custom` module. |
### Variables ### Variables

View File

@ -29,7 +29,7 @@ pub struct CustomConfig<'a> {
impl<'a> RootModuleConfig<'a> for CustomConfig<'a> { impl<'a> RootModuleConfig<'a> for CustomConfig<'a> {
fn new() -> Self { fn new() -> Self {
CustomConfig { CustomConfig {
format: "[$symbol$output]($style) ", format: "[$symbol($output )]($style)",
symbol: "", symbol: "",
command: "", command: "",
when: None, when: None,

View File

@ -46,10 +46,6 @@ pub fn module<'a>(name: &str, context: &'a Context) -> Option<Module<'a>> {
let mut module = Module::new(name, config.description, Some(toml_config)); let mut module = Module::new(name, config.description, Some(toml_config));
let output = exec_command(config.command, &config.shell.0)?;
let trimmed = output.trim();
if !trimmed.is_empty() {
let parsed = StringFormatter::new(config.format).and_then(|formatter| { let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter formatter
.map_meta(|var, _| match var { .map_meta(|var, _| match var {
@ -61,9 +57,16 @@ pub fn module<'a>(name: &str, context: &'a Context) -> Option<Module<'a>> {
_ => None, _ => None,
}) })
.map(|variable| match variable { .map(|variable| match variable {
// This may result in multiple calls to `get_module_version` when a user have "output" => {
// multiple `$version` variables defined in `format`. let output = exec_command(config.command, &config.shell.0)?;
"output" => Some(Ok(trimmed)), let trimmed = output.trim();
if trimmed.is_empty() {
None
} else {
Some(Ok(trimmed.to_string()))
}
}
_ => None, _ => None,
}) })
.parse(None) .parse(None)
@ -75,7 +78,6 @@ pub fn module<'a>(name: &str, context: &'a Context) -> Option<Module<'a>> {
log::warn!("Error in module `custom.{}`:\n{}", name, error); log::warn!("Error in module `custom.{}`:\n{}", name, error);
} }
}; };
}
let elapsed = start.elapsed(); let elapsed = start.elapsed();
log::trace!("Took {:?} to compute custom module {:?}", elapsed, name); log::trace!("Took {:?} to compute custom module {:?}", elapsed, name);
module.duration = elapsed; module.duration = elapsed;