1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-28 07:46:28 +00:00

feat(r-lang): add module for R programming language (#1475)

* feat(formatter): Allow scoped variables (#1094)

* feat: Allow scoped variables

, with the following improvements to the format string parser.

- Add documentation to spec
- Simplify some syntax in the spec
- Rewrite for loop with iterators

* Added support for R programming language.

* Removed unnecessary debug log used during dev process.

* Make the `R` command upper case as the *nix OS executables are case sensitives and the correct command is upper-case.

* Changed comments to reflect R features (rather than node.js where code was coming from).

* feat(format_string): Allow positional segments (#1138)

* feat(format_string): Allow using variables in a style string (#1130)

* fix(format_string): Allow multiple variable mappers (#1142)

* refactor: Add error handling to variables (#1148)

* Squashed commit of changes with meta variables:

commit 5beb3bca18f0b0c822b740afb3778ccb1e3a7d19
Author: heyrict <xiezh0831@yahoo.co.jp>
Date:   Mon Apr 27 09:52:59 2020 +0800

    fix: Cache variables in meta variables properly

commit 49b9324942dd55350c87107d0e8c7d1592d92e8a
Merge: cc575bc 260a1ab
Author: heyrict <xiezh0831@yahoo.co.jp>
Date:   Sun Apr 26 21:34:52 2020 +0800

    Merge branch 'feat/format-string' into meta-variables

commit cc575bc27cbf87c4197e96d2fa5416d4932e45d7
Merge: 3ed2d32 e0c1901
Author: heyrict <xiezh0831@yahoo.co.jp>
Date:   Sun Apr 26 12:16:12 2020 +0800

    Merge branch 'feat/format-string' into meta-variables

commit 3ed2d326c9f625930bdd72cea736c1d0eab6d381
Author: heyrict <xiezh0831@yahoo.co.jp>
Date:   Sun Apr 26 11:06:28 2020 +0800

    refactor(format_string): Allow returning error in variable mapper

commit 766732fe697df947538fe12ca92a8eb8e7bfea3e
Author: heyrict <xiezh0831@yahoo.co.jp>
Date:   Sat Apr 25 22:56:02 2020 +0800

    fix: Add test for StyleVariableHolder

commit 444334ad206a68132fa6257b83c3992b7b790981
Merge: 479d4a7 9796a66
Author: heyrict <xiezh0831@yahoo.co.jp>
Date:   Sat Apr 25 22:52:27 2020 +0800

    Merge branch 'positional-segments' into style-variables

commit 9796a66a9679597676e7fd859197fd542e8042dc
Author: heyrict <xiezh0831@yahoo.co.jp>
Date:   Sat Apr 25 22:51:26 2020 +0800

    test: Add tests for VariableHolder

commit 479d4a72fa58fd8aa777acd8228d4834407a7b6a
Author: heyrict <xiezh0831@yahoo.co.jp>
Date:   Sat Apr 25 22:41:47 2020 +0800

    feat: Add trait StyleVariableHolder

commit 21d40c6f4e2d12b34fdec4e2e38b6ad0f91217a3
Merge: 3b459f4 e7dd987
Author: heyrict <xiezh0831@yahoo.co.jp>
Date:   Sat Apr 25 22:17:11 2020 +0800

    Merge branch 'positional-segments' into style-variables

commit e7dd987fd7b01a82c6012ba7055d1dd9b5fd84aa
Author: heyrict <xiezh0831@yahoo.co.jp>
Date:   Sat Apr 25 15:10:12 2020 +0800

    misc: Minor changes on docs and codes

commit 71020b0397a86e850ad5beda926aa9416250025c
Author: heyrict <xiezh0831@yahoo.co.jp>
Date:   Fri Apr 24 20:51:45 2020 +0800

    feat(format_string): Add syntax for positional segments

commit 3b459f4379b08defce50c57a903502513ad1b2b6
Author: heyrict <xiezh0831@yahoo.co.jp>
Date:   Wed Apr 22 17:49:15 2020 +0800

    fix: Fix clippy

commit 2fb052d68cb46680c081f5a0e25e2c3fbdc9e204
Author: heyrict <xiezh0831@yahoo.co.jp>
Date:   Wed Apr 22 17:02:09 2020 +0800

    feat: Add map_style method to feed values in style string

* fix: Change error type of StringFormatter::new

* fix: Fix rustfmt

* tests: Add tests to variable errors

* docs: Add documentation

* chore: Rename positional to conditional (#1166)

* docs: Add docs for format strings (#1083)


Co-authored-by: Thomas O'Donnell <andytom@users.noreply.github.com>

* refactor(rust): Use format strings (#1063)

* Updated to latest string formatter's changes.

* feat(format-string): add format string support to battery module (#1158)

* update battery module with format string

* update battery module docs

* update battery module with format string

* update battery module docs

* fix battery module with new StringFormatter api

* fix clippy warnings

* Update docs/config/README.md

Co-authored-by: Zhenhui Xie <xiezh0831@yahoo.co.jp>

* battery symbols now supports format-string

* battery symbols now support format-string
remove space between symbol and percentage
fix battery config

Co-authored-by: Zhenhui Xie <xiezh0831@yahoo.co.jp>

* refactor(golang): Use format strings (#1066)

* refactor(golang): Use format strings

* docs(golang): Update docs

* docs(golang): Update docs

* fix: Update to upstream API changes

* docs(golang): Update docs

Co-authored-by: heyrict <xiezh0831@yahoo.co.jp>

* Fixed a few inconsistencies.

* Removed string clone in favor of a reference.

* Update src/modules/r.rs

Reverting the r version string formatting to a more idiomatic way of handling it.

Co-authored-by: Thomas O'Donnell <andytom@users.noreply.github.com>

* Update src/configs/r.rs

Co-authored-by: Zhenhui Xie <xiezh0831@yahoo.co.jp>

* Updated documentation to reflect changes in config.

* refactor(java): Added formatter support for Java module. (#1084)

* refactor(haskell): Added formatter support for the Haskell module. (#1111)

* Added formatter support for the Haskell module.

* Updated haskell module with latest formatter code changes.

* Changed documentation for latest Haskell string formatter changes.

* Fixed a few inconsistencies.

* Removed unnecessary variable cloning for using reference instead.

* refactor(env_var): Added formatter support for the env_var module (#1180)

* refactor(memory_usage): Added formatter support for memory_usage module (#1182)

* Migrated the memory usage module to string formatter' support.

* Fixed a few inconsistencies.

* Removed cloning of variables to instead use references.

* refactor(cmd_duration): Use format strings (#1200)

* Fixed format issue in code.

* Fixed compilation error after adding new 'r' module in root config.

* Added .Rproj extension file to be detected with R prog lang.

* Aligned R module code with existing ones.

* Update src/configs/r.rs

Co-authored-by: David Knaack <davidkna@users.noreply.github.com>

* fix: Added rconfig to fullconfig and fixed broken api calls

* Apply suggestions from code review

Co-authored-by: Dario Vladović <d.vladimyr@gmail.com>

* Update src/modules/r.rs

Co-authored-by: Dario Vladović <d.vladimyr@gmail.com>

* Addressed PR comments.

Cleaned up code and fixed code errors.

* Updated docs for consistency purpose.

Co-authored-by: Milo <50248166+Milo123459@users.noreply.github.com>

* refactor: Renamed the `r` module to `rlang`

* test: Provided R fixture and R module renderer test

* doc: Updated rlang mod config to reflect latest detection changes

* fix: Added missing rlang entry in config/mod

* feat: Added version formatted fined grained configuration

* Added version_format in R lang documentation.

Co-authored-by: David Knaack <davidkna@users.noreply.github.com>

* review: Addressed later comments

* fix: README was missing a previously present section for Python

* Fix: Test was not updated for previous version string upgrade.

* fix: Upgraded R version in remaining test.

Co-authored-by: Zhenhui Xie <xiezh0831@yahoo.co.jp>
Co-authored-by: Thomas O'Donnell <andytom@users.noreply.github.com>
Co-authored-by: Luca Rinaldi <lucarin@protonmail.com>
Co-authored-by: John Letey <johnletey@gmail.com>
Co-authored-by: Tilmann Meyer <47182955+ATiltedTree@users.noreply.github.com>
Co-authored-by: David Knaack <davidkna@users.noreply.github.com>
Co-authored-by: Dario Vladović <d.vladimyr@gmail.com>
Co-authored-by: Milo <50248166+Milo123459@users.noreply.github.com>
This commit is contained in:
Jimmy Royer 2021-05-25 14:13:30 -04:00 committed by GitHub
parent c68180bbe3
commit 8d3aa3b304
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 255 additions and 0 deletions

View File

@ -2285,6 +2285,49 @@ detect_extensions = []
python_binary = ["./venv/bin/python", "python", "python3", "python2"]
```
## R
The `rlang` module shows the currently installed version of R. The module will be shown if
any of the following conditions are met:
- The current directory contains a file with the `.R` extension.
- The current directory contains a file with the `.Rd` extension.
- The current directory contains a file with the `.Rmd` extension.
- The current directory contains a file with the `.Rproj` extension.
- The current directory contains a file with the `.Rsx` extension.
- The current directory contains a `.Rprofile` file
- The current directory contains a `.Rproj.user` folder
### Options
| Option | Default | Description |
|---------------------|--------------------------------------|-----------------------------------------------|
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `version_format` | `"v${raw}"` | The version format. Available vars are `raw`, `major`, `minor`, & `patch`|
| `symbol` | `"📐"` | A format string representing the symbol of R. |
| `style` | `"blue bold"` | The style for the module. |
| `detect_extensions` | `["R", "Rd", "Rmd", "Rproj", "Rsx"]` | Which extensions should trigger this module |
| `detect_files` | `[".Rprofile"]` | Which filenames should trigger this module |
| `detect_folders` | `[".Rproj.user"]` | Which folders should trigger this module |
| `disabled` | `false` | Disables the `r` module. |
### Variables
| Variable | Example | Description |
| -------- | ------------- | ------------------------------------ |
| version | `v4.0.5` | The version of `R` |
| symbol | | Mirrors the value of option `symbol` |
| style | `"blue bold"` | Mirrors the value of option `style` |
### Example
```toml
# ~/.config/starship.toml
[rlang]
format = "with [📐 $version](blue bold) "
```
## Red
By default the `red` module shows the currently installed version of [Red](https://www.red-lang.org/).

View File

@ -47,6 +47,7 @@ pub mod php;
pub mod purescript;
pub mod python;
pub mod red;
pub mod rlang;
pub mod ruby;
pub mod rust;
pub mod scala;
@ -117,6 +118,7 @@ pub struct FullConfig<'a> {
php: php::PhpConfig<'a>,
purescript: purescript::PureScriptConfig<'a>,
python: python::PythonConfig<'a>,
rlang: rlang::RLangConfig<'a>,
red: red::RedConfig<'a>,
ruby: ruby::RubyConfig<'a>,
rust: rust::RustConfig<'a>,
@ -186,6 +188,7 @@ impl<'a> Default for FullConfig<'a> {
purescript: Default::default(),
python: Default::default(),
red: Default::default(),
rlang: Default::default(),
ruby: Default::default(),
rust: Default::default(),
scala: Default::default(),

31
src/configs/rlang.rs Normal file
View File

@ -0,0 +1,31 @@
use crate::config::ModuleConfig;
use serde::Serialize;
use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig, Serialize)]
pub struct RLangConfig<'a> {
pub format: &'a str,
pub version_format: &'a str,
pub style: &'a str,
pub symbol: &'a str,
pub disabled: bool,
pub detect_extensions: Vec<&'a str>,
pub detect_files: Vec<&'a str>,
pub detect_folders: Vec<&'a str>,
}
impl<'a> Default for RLangConfig<'a> {
fn default() -> Self {
RLangConfig {
format: "via [$symbol($version )]($style)",
version_format: "v${raw}",
style: "blue bold",
symbol: "📐 ",
disabled: false,
detect_extensions: vec!["R", "Rd", "Rmd", "Rproj", "Rsx"],
detect_files: vec![".Rprofile"],
detect_folders: vec![".Rproj.user"],
}
}
}

View File

@ -52,6 +52,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"php",
"purescript",
"python",
"rlang",
"red",
"ruby",
"rust",

View File

@ -52,6 +52,7 @@ pub const ALL_MODULES: &[&str] = &[
"perl",
"purescript",
"python",
"rlang",
"red",
"ruby",
"crystal",

View File

@ -43,6 +43,7 @@ mod php;
mod purescript;
mod python;
mod red;
mod rlang;
mod ruby;
mod rust;
mod scala;
@ -118,6 +119,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"php" => php::module(context),
"purescript" => purescript::module(context),
"python" => python::module(context),
"rlang" => rlang::module(context),
"red" => red::module(context),
"ruby" => ruby::module(context),
"rust" => rust::module(context),
@ -201,6 +203,7 @@ pub fn description(module: &str) -> &'static str {
"php" => "The currently installed version of PHP",
"purescript" => "The currently installed version of PureScript",
"python" => "The currently installed version of Python",
"rlang" => "The currently installed version of R",
"red" => "The currently installed version of Red",
"ruby" => "The currently installed version of Ruby",
"rust" => "The currently installed version of Rust",

159
src/modules/rlang.rs Normal file
View File

@ -0,0 +1,159 @@
use super::{Context, Module, RootModuleConfig};
use crate::formatter::VersionFormatter;
use crate::configs::rlang::RLangConfig;
use crate::formatter::StringFormatter;
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("rlang");
let config: RLangConfig = RLangConfig::try_load(module.config);
let is_r_project = context
.try_begin_scan()?
.set_files(&config.detect_files)
.set_extensions(&config.detect_extensions)
.set_folders(&config.detect_folders)
.is_match();
if !is_r_project {
return None;
}
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|var, _| match var {
"symbol" => Some(config.symbol),
_ => None,
})
.map_style(|variable| match variable {
"style" => Some(Ok(config.style)),
_ => None,
})
.map(|variable| match variable {
"version" => {
let r_version = get_r_version(context)?;
VersionFormatter::format_module_version(
module.get_name(),
&r_version,
config.version_format,
)
.map(Ok)
}
_ => None,
})
.parse(None)
});
module.set_segments(match parsed {
Ok(segments) => segments,
Err(error) => {
log::warn!("Error in module `rlang`:\n{}", error);
return None;
}
});
Some(module)
}
fn get_r_version(context: &Context) -> Option<String> {
let r_version = context.exec_cmd("R", &["--version"])?.stderr;
parse_version(&r_version)
}
fn parse_version(r_version: &str) -> Option<String> {
r_version
.lines()
// take first line
.next()?
// split into ["R", "version", "3.6.3", "(2020-02-29)", ...]
.split_whitespace()
// and pick version entry at index 2, i.e. "3.6.3".
.nth(2)
.map(ToString::to_string)
}
#[cfg(test)]
mod tests {
use super::parse_version;
use crate::test::ModuleRenderer;
use ansi_term::Color;
use std::fs;
use std::fs::File;
use std::io;
#[test]
fn test_parse_r_version() {
let r_v3 = r#"R version 4.1.0 (2021-05-18) -- "Camp Pontanezen"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)\n
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/."#;
assert_eq!(parse_version(r_v3), Some(String::from("4.1.0")));
}
#[test]
fn folder_with_r_files() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("analysis.R"))?.sync_all()?;
check_r_render(&dir);
dir.close()
}
#[test]
fn folder_with_rd_files() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("analysis.Rd"))?.sync_all()?;
check_r_render(&dir);
dir.close()
}
#[test]
fn folder_with_rmd_files() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("analysis.Rmd"))?.sync_all()?;
check_r_render(&dir);
dir.close()
}
#[test]
fn folder_with_rproj_files() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("analysis.Rproj"))?.sync_all()?;
check_r_render(&dir);
dir.close()
}
#[test]
fn folder_with_rsx_files() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("analysis.Rsx"))?.sync_all()?;
check_r_render(&dir);
dir.close()
}
#[test]
fn folder_with_rprofile_files() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join(".Rprofile"))?.sync_all()?;
check_r_render(&dir);
dir.close()
}
#[test]
fn folder_with_rproj_user_folder() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let rprofile = dir.path().join(".Rproj.user");
fs::create_dir_all(&rprofile)?;
check_r_render(&dir);
dir.close()
}
fn check_r_render(dir: &tempfile::TempDir) {
let actual = ModuleRenderer::new("rlang").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Blue.bold().paint("📐 v4.1.0 ")));
assert_eq!(expected, actual);
}
}

View File

@ -175,6 +175,20 @@ active boot switches: -d:release\n",
stdout: String::from("Python 3.8.0\n"),
stderr: String::default(),
}),
"R --version" => Some(CommandOutput {
stdout: String::default(),
stderr: String::from(
r#"R version 4.1.0 (2021-05-18) -- "Camp Pontanezen"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)\n
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/."#
),
}),
"red --version" => Some(CommandOutput {
stdout: String::from("0.6.4\n"),
stderr: String::default()