Skip to content

Commit

Permalink
feat(perl): Adding perl section (#1135)
Browse files Browse the repository at this point in the history
* perl: Adding perl section to the project

** Added the perl section to grab perl version and
set the project specific variables for perl. Perl is
noted to be 5.x or previous where 6+ is no longer considered
Perl.

Added Docs for Perl.

* docs(perl): Remove translations

* docs(perl): Add docs to mkdocs

* refactor(perl): Use section correctly

* test(perl): Add tests for Perl

* docs(perl): Update docs

Co-authored-by: Denys Dovhan <denysdovhan@gmail.com>
  • Loading branch information
barbacbd and denysdovhan committed Oct 31, 2022
1 parent dd07d83 commit d3c2bf6
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/config/prompt.md
Expand Up @@ -55,6 +55,7 @@ SPACESHIP_PROMPT_ORDER=(
xcode # Xcode section
swift # Swift section
golang # Go section
perl # Perl section
php # PHP section
rust # Rust section
haskell # Haskell Stack section
Expand Down
6 changes: 6 additions & 0 deletions docs/registry/internal.json
Expand Up @@ -235,6 +235,12 @@
"description": "The version of the package when the current directory is a package.",
"internal": true
},
{
"name": "perl",
"url": "https://spaceship-prompt.sh/sections/perl",
"description": "The version of the Perl binary.",
"internal": true
},
{
"name": "php",
"url": "https://spaceship-prompt.sh/sections/php",
Expand Down
24 changes: 24 additions & 0 deletions docs/sections/perl.md
@@ -0,0 +1,24 @@
# Perl `perl`

!!! important "This section is rendered asynchronously by default"

!!! info
[**Perl**](https://www.perl.org/) is a general purpose programming language, originally developed for text manipulation.

The `perl` section displays the version of the Perl.

This section is displayed only when the current directory is within a Perl project, meaning:

* Upsearch finds a `META.json`, `META.yml`, `META.yaml`, `.perl-version`, or `cpanfile` file
* Current directory contains any `.pl` or `.pm` file

## Options

| Variable | Default | Meaning |
| :---------------------- | :--------------------------------: | ----------------------------------- |
| `SPACESHIP_PERL_SHOW` | `true` | Show section |
| `SPACESHIP_PERL_ASYNC` | `true` | Render section asynchronously |
| `SPACESHIP_PERL_PREFIX` | `$SPACESHIP_PROMPT_DEFAULT_PREFIX` | Section's prefix |
| `SPACESHIP_PERL_SUFFIX` | `$SPACESHIP_PROMPT_DEFAULT_SUFFIX` | Section's suffix |
| `SPACESHIP_PERL_SYMBOL` | `🐪·` | Symbol displayed before the section |
| `SPACESHIP_PERL_COLOR` | `blue` | Section's color |
1 change: 1 addition & 0 deletions mkdocs.yml
Expand Up @@ -71,6 +71,7 @@ nav:
- Xcode (xcode): sections/xcode.md
- Swift (swift): sections/swift.md
- Go (golang): sections/golang.md
- Perl (perl): sections/perl.md
- PHP (php): sections/php.md
- Python (python): sections/python.md
- Rust (rust): sections/rust.md
Expand Down
43 changes: 43 additions & 0 deletions sections/perl.zsh
@@ -0,0 +1,43 @@
#
# Perl
#
# Perl is a general purpose programming language, originally developed for
# text manipulation.
# Perl refers to Perl 5.x, where Perl 6+ officially changed the name to Raku.
#
# Link: https://www.perl.org/
# ------------------------------------------------------------------------------
# Configuration
# ------------------------------------------------------------------------------

SPACESHIP_PERL_SHOW="${SPACESHIP_PERL_SHOW=true}"
SPACESHIP_PERL_ASYNC="${SPACESHIP_PHP_ASYNC=true}"
SPACESHIP_PERL_PREFIX="${SPACESHIP_PERL_PREFIX="$SPACESHIP_PROMPT_DEFAULT_PREFIX"}"
SPACESHIP_PERL_SUFFIX="${SPACESHIP_PERL_SUFFIX="$SPACESHIP_PROMPT_DEFAULT_SUFFIX"}"
SPACESHIP_PERL_SYMBOL="${SPACESHIP_PERL_SYMBOL="🐪 "}"
SPACESHIP_PERL_COLOR="${SPACESHIP_PERL_COLOR="blue"}"

# ------------------------------------------------------------------------------
# Section
# ------------------------------------------------------------------------------

# Show current version of Perl
spaceship_perl() {
[[ $SPACESHIP_PERL_SHOW == false ]] && return

spaceship::exists perl || return

# Show only if perl files or composer.json exist in current directory
local is_perl_project="$(spaceship::upsearch META.json META.yml META.yaml .perl-version cpanfile)"
[[ -n "$is_perl_project" || -n *.pl(#qN^/) || -n *.pm(#qN^/) ]] || return

local perl_version=$(perl -v 2>&1 | awk '/This/ {print $9}' | sed -r 's/[(v]+//g;s/[)]//g')

spaceship::section \
--color "$SPACESHIP_PERL_COLOR" \
--prefix "$SPACESHIP_PERL_PREFIX" \
--suffix "$SPACESHIP_PERL_SUFFIX" \
--symbol "$SPACESHIP_PERL_SYMBOL" \
"v$perl_version"

}
1 change: 1 addition & 0 deletions spaceship.zsh
Expand Up @@ -54,6 +54,7 @@ if [ -z "$SPACESHIP_PROMPT_ORDER" ]; then
xcode # Xcode section
swift # Swift section
golang # Go section
perl # Perl section
php # PHP section
rust # Rust section
haskell # Haskell Stack section
Expand Down
86 changes: 86 additions & 0 deletions tests/perl.test.zsh
@@ -0,0 +1,86 @@
#!/usr/bin/env zsh

# Required for shunit2 to run correctly
setopt shwordsplit
SHUNIT_PARENT=$0

# ------------------------------------------------------------------------------
# SHUNIT2 HOOKS
# ------------------------------------------------------------------------------

oneTimeSetUp() {
export TERM="xterm-256color"
export PATH=$PWD/tests/stubs:$PATH

PERL_VERSION="5.30.3"

SPACESHIP_PROMPT_ASYNC=false
SPACESHIP_PROMPT_FIRST_PREFIX_SHOW=true
SPACESHIP_PROMPT_ADD_NEWLINE=false
SPACESHIP_PROMPT_ORDER=(perl)

source "spaceship.zsh"
}

setUp() {
SPACESHIP_PERL_SHOW="true"
SPACESHIP_PERL_PREFIX="via "
SPACESHIP_PERL_SUFFIX=""
SPACESHIP_PERL_SYMBOL="🐪 "
SPACESHIP_PERL_COLOR="blue"

cd $SHUNIT_TMPDIR
}

oneTimeTearDown() {
unset SPACESHIP_PROMPT_FIRST_PREFIX_SHOW
unset SPACESHIP_PROMPT_ADD_NEWLINE
unset SPACESHIP_PROMPT_ORDER
}

tearDown() {
unset SPACESHIP_PERL_SHOW
unset SPACESHIP_PERL_PREFIX
unset SPACESHIP_PERL_SUFFIX
unset SPACESHIP_PERL_SYMBOL
unset SPACESHIP_PERL_COLOR
}

# ------------------------------------------------------------------------------
# TEST CASES
# ------------------------------------------------------------------------------

test_no_files() {
local expected=""
local actual="$(spaceship::testkit::render_prompt)"
assertEquals "should not render without files" "$expected" "$actual"
}

test_dart_upsearch_file() {
FILES=(META.json META.yml META.yaml .perl-version cpanfile)
for file in $FILES; do
touch $file
local expected="%{%B%}$SPACESHIP_PERL_PREFIX%{%b%}%{%B%F{$SPACESHIP_PERL_COLOR}%}${SPACESHIP_PERL_SYMBOL}v$PERL_VERSION%{%b$SPACESHIP_PERL_SUFFIX%f%}"
local actual="$(spaceship::testkit::render_prompt)"
assertEquals "should render with $file" "$expected" "$actual"
rm $file
done
}

test_dart_file_extension() {
FILES=(first.pl second.pm)
for file in $FILES; do
touch $file
local expected="%{%B%}$SPACESHIP_PERL_PREFIX%{%b%}%{%B%F{$SPACESHIP_PERL_COLOR}%}${SPACESHIP_PERL_SYMBOL}v$PERL_VERSION%{%b$SPACESHIP_PERL_SUFFIX%f%}"
local actual="$(spaceship::testkit::render_prompt)"
assertEquals "should render with $file" "$expected" "$actual"
rm $file
done
}

# ------------------------------------------------------------------------------
# SHUNIT2
# Run tests with shunit2
# ------------------------------------------------------------------------------

source tests/shunit2/shunit2
3 changes: 3 additions & 0 deletions tests/stubs/perl
@@ -0,0 +1,3 @@
#!/usr/bin/env zsh

printf 'This is perl 5, version 30, subversion 3 (v5.30.3) built for darwin-thread-multi-2level\n'

0 comments on commit d3c2bf6

Please sign in to comment.