Skip to content

Commit

Permalink
feat: php language segment
Browse files Browse the repository at this point in the history
  • Loading branch information
lnu authored and JanDeDobbeleer committed Oct 27, 2021
1 parent 457cd35 commit 42aac1f
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 1 deletion.
36 changes: 36 additions & 0 deletions 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`
1 change: 1 addition & 0 deletions docs/sidebars.js
Expand Up @@ -59,6 +59,7 @@ module.exports = {
"text",
"time",
"ytm",
"php",
],
},
{
Expand Down
3 changes: 3 additions & 0 deletions src/segment.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down
29 changes: 29 additions & 0 deletions 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<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[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()
}
32 changes: 32 additions & 0 deletions 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))
}
}
32 changes: 31 additions & 1 deletion themes/schema.json
Expand Up @@ -170,7 +170,8 @@
"rust",
"owm",
"memory",
"angularcli"
"angularcli",
"php"
]
},
"style": {
Expand Down Expand Up @@ -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": {
Expand Down

0 comments on commit 42aac1f

Please sign in to comment.