From 1c03a867836ab9621bc54b97d8d900d5aee6a4fc Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Sat, 13 Oct 2018 14:45:57 -0500 Subject: [PATCH] Add Elm Section (#528) --- README.md | 1 + docs/Options.md | 12 ++++ docs/README.md | 1 + sections/elm.zsh | 36 ++++++++++++ spaceship.zsh | 1 + tests/elm.test.zsh | 136 +++++++++++++++++++++++++++++++++++++++++++++ tests/stubs/elm | 3 + 7 files changed, 190 insertions(+) create mode 100644 sections/elm.zsh create mode 100755 tests/elm.test.zsh create mode 100755 tests/stubs/elm diff --git a/README.md b/README.md index 1590067fc..27976d48f 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ Spaceship is a minimalistic, powerful and extremely customizable [Zsh][zsh-url] * Indicator for jobs in the background (`โœฆ`). * Current Node.js version, through nvm/nodenv/n (`โฌข`). * Current Ruby version, through rvm/rbenv/chruby/asdf (`๐Ÿ’Ž`). +* Current Elm version (`๐ŸŒณ`) * Current Elixir version, through kiex/exenv/elixir (`๐Ÿ’ง`). * Current Swift version, through swiftenv (`๐Ÿฆ`). * Current Xcode version, through xenv (`๐Ÿ› `). diff --git a/docs/Options.md b/docs/Options.md index 19606f9e2..5abf5a625 100644 --- a/docs/Options.md +++ b/docs/Options.md @@ -263,6 +263,18 @@ Ruby section is shown only in directories that contain `Gemfile`, or `Rakefile`, | `SPACESHIP_RUBY_SYMBOL` | `๐Ÿ’Žยท` | Character to be shown before Ruby version | | `SPACESHIP_RUBY_COLOR` | `red` | Color of Ruby section | +### Elm (`elm`) + +Elm section is shown only in directories that contain `elm.json` file, `elm-package.json` file, `elm-stuff` directory, or any other file with `.elm` extension. + +| Variable | Default | Meaning | +| :------- | :-----: | ------- | +| `SPACESHIP_ELM_SHOW` | `true` | Show installed Elm version | +| `SPACESHIP_ELM_PREFIX` | `$SPACESHIP_PROMPT_DEFAULT_PREFIX` | Prefix before Elm section | +| `SPACESHIP_ELM_SUFFIX` | `$SPACESHIP_PROMPT_DEFAULT_SUFFIX` | Suffix after Elm section | +| `SPACESHIP_ELM_SYMBOL` | `๐ŸŒณยท` | Character to be shown before Elm version | +| `SPACESHIP_ELM_COLOR` | `cyan` | Color of Elm section | + ### Elixir (`elixir`) Elixir section is shown only in directories that contain `mix.exs`, or any other file with `.ex` or `.exs` extension. If the current elixir version is the same as the version set in `SPACESHIP_ELIXIR_DEFAULT_VERSION`, the elixir section will be hidden. diff --git a/docs/README.md b/docs/README.md index a16c2901d..8aaaffcbd 100644 --- a/docs/README.md +++ b/docs/README.md @@ -18,6 +18,7 @@ * [Package version (package)](/docs/Options.md#package-version-package) * [Node (node)](/docs/Options.md#nodejs-node) * [Ruby (ruby)](/docs/Options.md#ruby-ruby) + * [Elm (elm)](/docs/Options.md#elm-elm) * [Elixir (elixir)](/docs/Options.md#elixir-elixir) * [Xcode (xcode)](/docs/Options.md#xcode-xcode) * [Swift (swift)](/docs/Options.md#swift-swift) diff --git a/sections/elm.zsh b/sections/elm.zsh new file mode 100644 index 000000000..0ffd735da --- /dev/null +++ b/sections/elm.zsh @@ -0,0 +1,36 @@ +# +# Elm +# +# A delightful language for reliable webapps. +# Link: https://elm-lang.org/ + +# ------------------------------------------------------------------------------ +# Configuration +# ------------------------------------------------------------------------------ + +SPACESHIP_ELM_SHOW="${SPACESHIP_ELM_SHOW=true}" +SPACESHIP_ELM_PREFIX="${SPACESHIP_ELM_PREFIX="$SPACESHIP_PROMPT_DEFAULT_PREFIX"}" +SPACESHIP_ELM_SUFFIX="${SPACESHIP_ELM_SUFFIX="$SPACESHIP_PROMPT_DEFAULT_SUFFIX"}" +SPACESHIP_ELM_SYMBOL="${SPACESHIP_ELM_SYMBOL="๐ŸŒณ "}" +SPACESHIP_ELM_COLOR="${SPACESHIP_ELM_COLOR="cyan"}" + +# ------------------------------------------------------------------------------ +# Section +# ------------------------------------------------------------------------------ + +# Show current version of Elm. +spaceship_elm() { + [[ $SPACESHIP_ELM_SHOW == false ]] && return + + [[ -f elm.json || -f elm-package.json || -d elm-stuff || -n *.elm(#qN^/) ]] || return + + spaceship::exists elm || return + + local elm_version=$(elm --version 2> /dev/null) + + spaceship::section \ + "$SPACESHIP_ELM_COLOR" \ + "$SPACESHIP_ELM_PREFIX" \ + "${SPACESHIP_ELM_SYMBOL}v${elm_version}" \ + "$SPACESHIP_ELM_SUFFIX" +} diff --git a/spaceship.zsh b/spaceship.zsh index b39de5bb0..34ad75dff 100644 --- a/spaceship.zsh +++ b/spaceship.zsh @@ -49,6 +49,7 @@ if [ -z "$SPACESHIP_PROMPT_ORDER" ]; then package # Package version node # Node.js section ruby # Ruby section + elm # Elm section elixir # Elixir section xcode # Xcode section swift # Swift section diff --git a/tests/elm.test.zsh b/tests/elm.test.zsh new file mode 100755 index 000000000..5e0cbd65c --- /dev/null +++ b/tests/elm.test.zsh @@ -0,0 +1,136 @@ +#!/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 + + SPACESHIP_PROMPT_FIRST_PREFIX_SHOW=true + SPACESHIP_PROMPT_ADD_NEWLINE=false + SPACESHIP_PROMPT_ORDER=(elm) + + source spaceship.zsh +} + +setUp() { + SPACESHIP_ELM_SHOW=true + SPACESHIP_ELM_PREFIX="via " + SPACESHIP_ELM_SUFFIX="" + SPACESHIP_ELM_SYMBOL="๐ŸŒณ " + SPACESHIP_ELM_COLOR="cyan" + + cd $SHUNIT_TMPDIR + touch test.elm +} + +oneTimeTearDown() { + unset SPACESHIP_PROMPT_FIRST_PREFIX_SHOW + unset SPACESHIP_PROMPT_ADD_NEWLINE + unset SPACESHIP_PROMPT_ORDER +} + +tearDown() { + unset SPACESHIP_ELM_SHOW + unset SPACESHIP_ELM_PREFIX + unset SPACESHIP_ELM_SUFFIX + unset SPACESHIP_ELM_SYMBOL + unset SPACESHIP_ELM_COLOR +} + +# ------------------------------------------------------------------------------ +# TEST CASES +# ------------------------------------------------------------------------------ + +test_elm() { + local expected="%{%B%}via %{%b%}%{%B%F{cyan}%}๐ŸŒณ v0.20.0%{%b%f%}%{%B%}%{%b%}" + local actual="$(spaceship_prompt)" + + assertEquals "render elm" "$expected" "$actual" +} + +test_elm_show_false() { + SPACESHIP_ELM_SHOW=false + + local expected="" + local actual="$(spaceship_prompt)" + + assertEquals "don't render elm" "$expected" "$actual" +} + +test_elm_files() { + local expected="%{%B%}via %{%b%}%{%B%F{cyan}%}๐ŸŒณ v0.20.0%{%b%f%}%{%B%}%{%b%}" + local actual="$(spaceship_prompt)" + + assertEquals "render elm when test.elm exists" "$expected" "$actual" + + rm test.elm + touch elm.json + + local actual="$(spaceship_prompt)" + + assertEquals "render elm when elm.json exists" "$expected" "$actual" + + rm elm.json + touch elm-package.json + + local actual="$(spaceship_prompt)" + + assertEquals "render elm when elm-package.json exists" "$expected" "$actual" + + rm elm-package.json + mkdir elm-stuff + + local actual="$(spaceship_prompt)" + + assertEquals "render elm when elm-stuff exists" "$expected" "$actual" + + rm -r elm-stuff + + local expected="" + local actual="$(spaceship_prompt)" + + assertEquals "don't render when no elm files are present" "$expected" "$actual" +} + +test_elm_symbol() { + SPACESHIP_ELM_SYMBOL="๐ŸŒต " + + local expected="%{%B%}via %{%b%}%{%B%F{cyan}%}๐ŸŒต v0.20.0%{%b%f%}%{%B%}%{%b%}" + local actual="$(spaceship_prompt)" + + assertEquals "render elm with custom symbol" "$expected" "$actual" +} + +test_elm_prefix() { + SPACESHIP_ELM_PREFIX='prefix' + SPACESHIP_ELM_SUFFIX='' + + local expected="%{%B%}prefix%{%b%}%{%B%F{cyan}%}๐ŸŒณ v0.20.0%{%b%f%}%{%B%}%{%b%}" + local actual="$(spaceship_prompt)" + + assertEquals "render elm with prefix" "$expected" "$actual" +} + +test_elm_suffix() { + SPACESHIP_ELM_PREFIX='' + SPACESHIP_ELM_SUFFIX='suffix' + + local expected="%{%B%}%{%b%}%{%B%F{cyan}%}๐ŸŒณ v0.20.0%{%b%f%}%{%B%}suffix%{%b%}" + local actual="$(spaceship_prompt)" + + assertEquals "render elm with suffix" "$expected" "$actual" +} + +# ------------------------------------------------------------------------------ +# SHUNIT2 +# Run tests with shunit2 +# ------------------------------------------------------------------------------ + +source modules/shunit2/shunit2 diff --git a/tests/stubs/elm b/tests/stubs/elm new file mode 100755 index 000000000..9f06f87d5 --- /dev/null +++ b/tests/stubs/elm @@ -0,0 +1,3 @@ +#!/usr/bin/env zsh + +echo 0.20.0