Skip to content

Commit

Permalink
add color to 'shellrunner: command' output
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhl8 committed Oct 13, 2023
1 parent f6f585c commit 301a214
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: CI

on:
release:
types: [published]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Install rye
run: |
RYE_INSTALL_OPTION="--yes" /bin/bash -c "$(curl -fsSL https://rye-up.com/get)"
echo "$HOME/.rye/shims" >> $GITHUB_PATH
- name: rye sync
run: |
rye add --dev pyright
rye sync
- name: Lint
run: rye run lint

- name: Test
run: |
apt-add-repository ppa:fish-shell/release-3 -y
apt update
apt install fish -y
apt install zsh -y
rye run test
- name: Build
run: rye build

- name: Publish
run: rye publish -u __token__ --token ${{ secrets.PYPI_TOKEN }} --yes
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ result = X("(exit 1) | (exit 2) | echo hello")
# result.pipestatus = [1, 2, 0]
```

If using a shell that does not support `PIPESTATUS` such as `sh`, you will only ever get the status of the last command in a pipeline. **This also means that in this case ShellRunner cannot detect if an error occured in a pipeline:**
If using a shell that does not support `PIPESTATUS` such as `sh`, you will only ever get the status of the last command in a pipeline. **This also means that in this case ShellRunner cannot detect if an error occurred in a pipeline:**

```python
result = X("(exit 1) | echo hello")
Expand Down Expand Up @@ -207,11 +207,11 @@ X("echo hello world")
This will print the following to your terminal:

```
Executing: echo hello world
shellrunner: echo hello world
hello world
```

To hide the `Executing:` lines, set `show_commands=False`.
To hide the `shellrunner:` lines, set `show_commands=False`.

To hide actual command output, set `show_output=False`.

Expand Down
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "python-shellrunner"
version = "0.3.3"
version = "0.3.4"
description = "Write safe shell scripts in Python."
authors = [
{name = "adamhl8", email = "[email protected]"},
Expand Down Expand Up @@ -42,9 +42,10 @@ dev-dependencies = [
test = "pytest shellrunner"
"lint:pyright" = "pyright ."
"lint:ruff" = "ruff check ."
lint = { chain = ["lint:pyright", "lint:ruff"] }
"lint:black" = "black --check ."
"lint:pyroma" = "pyroma -n 10 ."
lint = { chain = ["lint:pyright", "lint:ruff", "lint:black", "lint:pyroma"] }
format = "black ."
"lint:pyroma" = "pyroma ."

[tool.pyright]
typeCheckingMode = "strict"
Expand Down
2 changes: 1 addition & 1 deletion shellrunner/_shellrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def run(

# Print command_list rather than commands so we don't see the appended status_checks.
if show_commands:
print(f"Executing: {'; '.join(command_list)}")
print(f"shellrunner: \033[33m{'; '.join(command_list)}\033[0m")

# By using the Popen context manager via with, standard file descriptors are automatically closed.
with subprocess.Popen(
Expand Down
10 changes: 8 additions & 2 deletions shellrunner/test_shellrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class ShellInfo(NamedTuple):
name: str


def remove_escape_sequences(string: str):
return re.sub(r"\x1b[^m]*m", "", string)


@pytest.fixture()
def shell_command_error_message():
return "Command exited with non-zero status:"
Expand Down Expand Up @@ -194,7 +198,8 @@ def test_show_output_false(self, shell: str, capsys: pytest.CaptureFixture[str])
assert result.status == 0
assert result.pipestatus == [0]
captured = capsys.readouterr()
assert captured.out == "Executing: echo test\n"
clean_out = remove_escape_sequences(captured.out)
assert clean_out == "shellrunner: echo test\n"

def test_show_commands_false(self, shell: str, capsys: pytest.CaptureFixture[str]):
result = X("echo test", shell=shell, show_commands=False)
Expand Down Expand Up @@ -260,7 +265,8 @@ def test_args_take_precedence_over_environment_variables(
assert result.status == 0
assert result.pipestatus == [0]
captured = capsys.readouterr()
assert captured.out == "Executing: echo test\ntest\n"
clean_out = remove_escape_sequences(captured.out)
assert clean_out == "shellrunner: echo test\ntest\n"

def test_invalid_bool_value_for_environment_variable_raises_error(
self,
Expand Down

0 comments on commit 301a214

Please sign in to comment.