Skip to content

Commit

Permalink
jj: add jj current change ID
Browse files Browse the repository at this point in the history
[Jujutsu](https://github.com/martinvonz/jj) is a new VCS that is change-based.

I want to do several PRs to add various parts of its informations to starship, starting with the 
simplest one, the current change ID.
  • Loading branch information
poliorcetics committed Mar 2, 2024
1 parent f38bf82 commit 15b298e
Show file tree
Hide file tree
Showing 15 changed files with 300 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,19 @@
}
]
},
"jj_change": {
"default": {
"change_id_length": 7,
"disabled": true,
"format": "[\\($change_id\\)]($style) ",
"style": "bold purple"
},
"allOf": [
{
"$ref": "#/definitions/JujutsuChangeConfig"
}
]
},
"jobs": {
"default": {
"disabled": false,
Expand Down Expand Up @@ -3939,6 +3952,30 @@
},
"additionalProperties": false
},
"JujutsuChangeConfig": {
"type": "object",
"properties": {
"change_id_length": {
"default": 7,
"type": "integer",
"format": "uint",
"minimum": 0.0
},
"disabled": {
"default": true,
"type": "boolean"
},
"format": {
"default": "[\\($change_id\\)]($style) ",
"type": "string"
},
"style": {
"default": "bold purple",
"type": "string"
}
},
"additionalProperties": false
},
"JobsConfig": {
"type": "object",
"properties": {
Expand Down
3 changes: 3 additions & 0 deletions docs/.vuepress/public/presets/toml/bracketed-segments.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ format = '\[[$symbol$branch]($style)\]'
[java]
format = '\[[$symbol($version)]($style)\]'

[jj_change]
format = '\[[$change_id]($style)\]'

[julia]
format = '\[[$symbol($version)]($style)\]'

Expand Down
5 changes: 5 additions & 0 deletions docs/.vuepress/public/presets/toml/gruvbox-rainbow.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ $directory\
[](fg:color_yellow bg:color_aqua)\
$git_branch\
$git_status\
$jj_change\
[](fg:color_aqua bg:color_blue)\
$c\
$rust\
Expand Down Expand Up @@ -124,6 +125,10 @@ symbol = " "
style = "bg:color_blue"
format = '[[ $symbol( $version) ](fg:color_fg0 bg:color_blue)]($style)'

[jj_change]
style = "bg:color_purple"
format = '[[ $change_id ](fg:color_fg0 bg:color_purple)]($style)'

[kotlin]
symbol = ""
style = "bg:color_blue"
Expand Down
4 changes: 4 additions & 0 deletions docs/.vuepress/public/presets/toml/jetpack.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ $git_state\
$git_metrics\
$git_status\
$hg_branch\
$jj_change\
$pijul_channel\
$docker_context\
$package\
Expand Down Expand Up @@ -263,6 +264,9 @@ format = " hs [$symbol($version )]($style)"
symbol = ""
format = " java [${symbol}(${version} )]($style)"

[jj_change]
format = " jj [$change_id]($style)"

[julia]
symbol = ""
format = " jl [$symbol($version )]($style)"
Expand Down
5 changes: 5 additions & 0 deletions docs/.vuepress/public/presets/toml/pastel-powerline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ $directory\
[](fg:#DA627D bg:#FCA17D)\
$git_branch\
$git_status\
$jj_change\
[](fg:#FCA17D bg:#86BBD8)\
$c\
$elixir\
Expand Down Expand Up @@ -112,6 +113,10 @@ symbol = " "
style = "bg:#86BBD8"
format = '[ $symbol ($version) ]($style)'

[jj_change]
style = "bg:#FCA17D"
format = '[ $change_id ]($style)'

[julia]
symbol = ""
style = "bg:#86BBD8"
Expand Down
34 changes: 34 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ $git_state\
$git_metrics\
$git_status\
$hg_branch\
$jj_change\
$pijul_channel\
$docker_context\
$package\
Expand Down Expand Up @@ -2445,6 +2446,39 @@ number_threshold = 4
symbol_threshold = 0
```

## Jujutsu Change

The `jj_change` module shows the current change ID in a [Jujutsu](martinvonz.github.io/jj/) repository.
When active (`disabled = false`), the module will be shown when your current directory is a Jujutsu directory.

### Options

| Option | Default | Description |
| ------------------ | ------------------------ | -------------------------------------------------------------------------------------------- |
| `change_id_length` | `7` | Minimum size of the unique prefix to use. Pass `0` to always use the shortest possible size. |
| `format` | `'[$change_id]($style)'` | The format for the module. |
| `style` | `'bold purple'` | The style for the module. |
| `disabled` | `true` | Disables the `jj_change` module. |

### Variables

| Variable | Example | Description |
| --------- | -------- | ----------------------------------- |
| change_id | `mvlssa` | The current change ID |
| style\* | | Mirrors the value of option `style` |

*: This variable can only be used as a part of a style string

### Example

```toml
# ~/.config/starship.toml

[jj_change]
change_id_length = 12
disabled = false
```

## Julia

The `julia` module shows the currently installed version of [Julia](https://julialang.org/).
Expand Down
29 changes: 29 additions & 0 deletions src/configs/jj_change.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Deserialize, Serialize)]
#[cfg_attr(
feature = "config-schema",
derive(schemars::JsonSchema),
schemars(deny_unknown_fields)
)]
#[serde(default)]
pub struct JujutsuChangeConfig<'a> {
pub change_id_length: usize,

pub disabled: bool,
pub format: &'a str,
pub style: &'a str,
}

impl Default for JujutsuChangeConfig<'_> {
fn default() -> Self {
Self {
// Copy the git default
change_id_length: 7,

disabled: true,
format: "[\\($change_id\\)]($style) ",
style: "bold purple",
}
}
}
3 changes: 3 additions & 0 deletions src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub mod helm;
pub mod hg_branch;
pub mod hostname;
pub mod java;
pub mod jj_change;
pub mod jobs;
pub mod julia;
pub mod kotlin;
Expand Down Expand Up @@ -196,6 +197,8 @@ pub struct FullConfig<'a> {
#[serde(borrow)]
java: java::JavaConfig<'a>,
#[serde(borrow)]
jj_change: jj_change::JujutsuChangeConfig<'a>,
#[serde(borrow)]
jobs: jobs::JobsConfig<'a>,
#[serde(borrow)]
julia: julia::JuliaConfig<'a>,
Expand Down
1 change: 1 addition & 0 deletions src/configs/starship_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"git_metrics",
"git_status",
"hg_branch",
"jj_change",
"pijul_channel",
"docker_context",
"package",
Expand Down
14 changes: 14 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ pub struct Context<'a> {
/// Private field to store Git information for modules who need it
repo: OnceCell<Repo>,

/// Private field indicating whether a JJ repo was found. Useful to skip computing JJ modules
/// when unnecessary because a previous one already checked.
found_jj_repo: OnceCell<bool>,

/// The shell the user is assumed to be running
pub shell: Shell,

Expand Down Expand Up @@ -168,6 +172,7 @@ impl<'a> Context<'a> {
logical_dir,
dir_contents: OnceCell::new(),
repo: OnceCell::new(),
found_jj_repo: OnceCell::new(),
shell,
target,
width,
Expand Down Expand Up @@ -363,6 +368,15 @@ impl<'a> Context<'a> {
})
}

pub fn found_jj_repo(&self) -> bool {
*self
.found_jj_repo
.get_or_init(|| match self.exec_cmd("jj", &["root"]) {
Some(out) => out.stdout.lines().next().is_some_and(|s| !s.is_empty()),
None => false,

Check warning on line 376 in src/context.rs

View check run for this annotation

Codecov / codecov/patch

src/context.rs#L376

Added line #L376 was not covered by tests
})
}

pub fn dir_contents(&self) -> Result<&DirContents, std::io::Error> {
self.dir_contents.get_or_try_init(|| {
let timeout = self.root_config.scan_timeout;
Expand Down
1 change: 1 addition & 0 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub const ALL_MODULES: &[&str] = &[
"hg_branch",
"hostname",
"java",
"jj_change",
"jobs",
"julia",
"kotlin",
Expand Down

0 comments on commit 15b298e

Please sign in to comment.