forked from starship/starship
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add PHP version module (starship#244)
- Loading branch information
1 parent
78ca70a
commit 46904e5
Showing
10 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; | ||
|
||
use ansi_term::{Color, Style}; | ||
use starship_module_config_derive::ModuleConfig; | ||
|
||
#[derive(Clone, ModuleConfig)] | ||
pub struct PhpConfig<'a> { | ||
pub symbol: SegmentConfig<'a>, | ||
pub style: Style, | ||
pub disabled: bool, | ||
} | ||
|
||
impl<'a> RootModuleConfig<'a> for PhpConfig<'a> { | ||
fn new() -> Self { | ||
PhpConfig { | ||
symbol: SegmentConfig::new("🐘 "), | ||
style: Color::Red.bold(), | ||
disabled: false, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ pub const ALL_MODULES: &[&str] = &[ | |
"python", | ||
"ruby", | ||
"rust", | ||
"php", | ||
"time", | ||
"username", | ||
]; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::process::Command; | ||
|
||
use super::{Context, Module, RootModuleConfig, SegmentConfig}; | ||
|
||
use crate::configs::php::PhpConfig; | ||
|
||
/// Creates a module with the current PHP version | ||
/// | ||
/// Will display the PHP version if any of the following criteria are met: | ||
/// - Current directory contains a `.php` file | ||
/// - Current directory contains a `composer.json` file | ||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { | ||
let is_php_project = context | ||
.try_begin_scan()? | ||
.set_files(&["composer.json"]) | ||
.set_extensions(&["php"]) | ||
.is_match(); | ||
|
||
if !is_php_project { | ||
return None; | ||
} | ||
|
||
match get_php_version() { | ||
Some(php_version) => { | ||
let mut module = context.new_module("php"); | ||
let config: PhpConfig = PhpConfig::try_load(module.config); | ||
|
||
module.set_style(config.style); | ||
|
||
let formatted_version = format_php_version(&php_version)?; | ||
module.create_segment("symbol", &config.symbol); | ||
module.create_segment("version", &SegmentConfig::new(&formatted_version)); | ||
|
||
Some(module) | ||
} | ||
None => None, | ||
} | ||
} | ||
|
||
fn get_php_version() -> Option<String> { | ||
match Command::new("php") | ||
.arg("-r") | ||
.arg("echo PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION;") | ||
.output() | ||
{ | ||
Ok(output) => Some(String::from_utf8(output.stdout).unwrap()), | ||
Err(_) => None, | ||
} | ||
} | ||
|
||
fn format_php_version(php_version: &str) -> Option<String> { | ||
let mut formatted_version = String::with_capacity(php_version.len() + 1); | ||
formatted_version.push('v'); | ||
formatted_version.push_str(php_version); | ||
Some(formatted_version) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_format_php_version() { | ||
let input = "7.3.8"; | ||
assert_eq!(format_php_version(input), Some("v7.3.8".to_string())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::fs::File; | ||
use std::io; | ||
|
||
use ansi_term::Color; | ||
|
||
use crate::common; | ||
use crate::common::TestCommand; | ||
|
||
#[test] | ||
fn folder_without_php_files() -> io::Result<()> { | ||
let dir = common::new_tempdir()?; | ||
|
||
let output = common::render_module("php") | ||
.arg("--path") | ||
.arg(dir.path()) | ||
.output()?; | ||
let actual = String::from_utf8(output.stdout).unwrap(); | ||
|
||
let expected = ""; | ||
assert_eq!(expected, actual); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
#[ignore] | ||
fn folder_with_composer_file() -> io::Result<()> { | ||
let dir = common::new_tempdir()?; | ||
File::create(dir.path().join("composer.json"))?; | ||
|
||
let output = common::render_module("php") | ||
.arg("--path") | ||
.arg(dir.path()) | ||
.output()?; | ||
let actual = String::from_utf8(output.stdout).unwrap(); | ||
|
||
let expected = format!("via {} ", Color::Red.bold().paint("🐘 v7.3.8")); | ||
assert_eq!(expected, actual); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
#[ignore] | ||
fn folder_with_php_file() -> io::Result<()> { | ||
let dir = common::new_tempdir()?; | ||
File::create(dir.path().join("any.php"))?; | ||
|
||
let output = common::render_module("php") | ||
.arg("--path") | ||
.arg(dir.path()) | ||
.output()?; | ||
let actual = String::from_utf8(output.stdout).unwrap(); | ||
|
||
let expected = format!("via {} ", Color::Red.bold().paint("🐘 v7.3.8")); | ||
assert_eq!(expected, actual); | ||
Ok(()) | ||
} |