From 42aac1fbf4b716743a67e590076666b05c6f20b9 Mon Sep 17 00:00:00 2001 From: lnu Date: Wed, 27 Oct 2021 08:52:56 +0000 Subject: [PATCH] feat: php language segment --- docs/docs/segment-php.md | 36 ++++++++++++++++++++++++++++++++++++ docs/sidebars.js | 1 + src/segment.go | 3 +++ src/segment_php.go | 29 +++++++++++++++++++++++++++++ src/segment_php_test.go | 32 ++++++++++++++++++++++++++++++++ themes/schema.json | 32 +++++++++++++++++++++++++++++++- 6 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 docs/docs/segment-php.md create mode 100644 src/segment_php.go create mode 100644 src/segment_php_test.go diff --git a/docs/docs/segment-php.md b/docs/docs/segment-php.md new file mode 100644 index 00000000..8920d9e4 --- /dev/null +++ b/docs/docs/segment-php.md @@ -0,0 +1,36 @@ +--- +id: php +title: php +sidebar_label: php +--- + +## What + +Display the currently active php version. + +## Sample Configuration + +```json +{ + "type": "php", + "style": "powerline", + "powerline_symbol": "\uE0B0", + "foreground": "#ffffff", + "background": "#4063D8", + "properties": { + "prefix": " \ue73d ", + "enable_hyperlink": false + } +} +``` + +## Properties + +- home_enabled: `boolean` - display the segment in the HOME folder or not - defaults to `false` +- display_version: `boolean` - display the php version - defaults to `true` +- display_error: `boolean` - show the error context when failing to retrieve the version information - defaults to `true` +- missing_command_text: `string` - text to display when the command is missing - defaults to empty +- display_mode: `string` - determines when the segment is displayed + - `always`: the segment is always displayed + - `files`: the segment is only displayed when `*.php` files are present (default) +- enable_hyperlink: `bool` - display an hyperlink to the php release notes - defaults to `false` diff --git a/docs/sidebars.js b/docs/sidebars.js index 578ae258..789dc4c8 100644 --- a/docs/sidebars.js +++ b/docs/sidebars.js @@ -59,6 +59,7 @@ module.exports = { "text", "time", "ytm", + "php", ], }, { diff --git a/src/segment.go b/src/segment.go index f632e17d..3cee93b7 100644 --- a/src/segment.go +++ b/src/segment.go @@ -127,6 +127,8 @@ const ( Memory SegmentType = "memory" // Angular writes which angular cli version us currently active Angular SegmentType = "angular" + // PHP writes which php version is currently active + PHP SegmentType = "php" ) func (segment *Segment) string() string { @@ -259,6 +261,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) error { Rust: &rust{}, Memory: &memory{}, Angular: &angular{}, + PHP: &php{}, } if writer, ok := functions[segment.Type]; ok { props := &properties{ diff --git a/src/segment_php.go b/src/segment_php.go new file mode 100644 index 00000000..6bf5127e --- /dev/null +++ b/src/segment_php.go @@ -0,0 +1,29 @@ +package main + +type php struct { + language *language +} + +func (n *php) string() string { + return n.language.string() +} + +func (n *php) init(props *properties, env environmentInfo) { + n.language = &language{ + env: env, + props: props, + extensions: []string{"*.php", "composer.json", ".php-version"}, + commands: []*cmd{ + { + executable: "php", + args: []string{"--version"}, + regex: `(?:PHP (?P((?P[0-9]+).(?P[0-9]+).(?P[0-9]+))))`, + }, + }, + versionURLTemplate: "[%[1]s](https://www.php.net/ChangeLog-%[2]s.php#PHP_%[2]s_%[3]s)", + } +} + +func (n *php) enabled() bool { + return n.language.enabled() +} diff --git a/src/segment_php_test.go b/src/segment_php_test.go new file mode 100644 index 00000000..1ac185a1 --- /dev/null +++ b/src/segment_php_test.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestPhp(t *testing.T) { + cases := []struct { + Case string + ExpectedString string + Version string + }{ + {Case: "PHP 6.1.0", ExpectedString: "6.1.0", Version: "PHP 6.1.0(cli) (built: Jul 2 2021 03:59:48) ( NTS )"}, + {Case: "php 7.4.21", ExpectedString: "7.4.21", Version: "PHP 7.4.21 (cli) (built: Jul 2 2021 03:59:48) ( NTS )"}, + } + for _, tc := range cases { + params := &mockedLanguageParams{ + cmd: "php", + versionParam: "--version", + versionOutput: tc.Version, + extension: "*.php", + } + env, props := getMockedLanguageEnv(params) + j := &php{} + j.init(props, env) + assert.True(t, j.enabled(), fmt.Sprintf("Failed in case: %s", tc.Case)) + assert.Equal(t, tc.ExpectedString, j.string(), fmt.Sprintf("Failed in case: %s", tc.Case)) + } +} diff --git a/themes/schema.json b/themes/schema.json index de6efefa..69c668cd 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -170,7 +170,8 @@ "rust", "owm", "memory", - "angularcli" + "angularcli", + "php" ] }, "style": { @@ -828,6 +829,35 @@ } } }, + { + "if": { + "properties": { + "type": { "const": "php" } + } + }, + "then": { + "title": "PHP Segment", + "description": "https://ohmyposh.dev/docs/php", + "properties": { + "properties": { + "properties": { + "display_version": { + "$ref": "#/definitions/display_version" + }, + "display_mode": { + "$ref": "#/definitions/display_mode" + }, + "missing_command_text": { + "$ref": "#/definitions/missing_command_text" + }, + "enable_hyperlink": { + "$ref": "#/definitions/enable_hyperlink" + } + } + } + } + } + }, { "if": { "properties": {