Add ansi_strip function

parent 57ab5db1
......@@ -211,6 +211,16 @@ tasks_run build package; # Runs the build and package tasks as above
tasks_run [email protected]; # Passes all arguments (except the script name / path itself) to the task runner
```
### `ansi_strip`
Originally from [this StackOverflow answer](https://stackoverflow.com/a/54766117/1460422), this utility function takes no arguments and simply strips all ANSI escape codes from stdin, and writes the output to stdout.
Useful for writing lantern output to a target which doesn't support escape codes, such as a log file.
```bash
tasks_run ci | ansi_strip >>"${log_filename}";
tasks_run package | ansi_strip >package.log;
```
## Contributing
Contributions are welcome! Simply fork this repository, make your changes, and submit a Pull Request (aka Merge Request).
......
......@@ -207,3 +207,15 @@ tasks_run() {
shift
done
}
# Originally from https://stackoverflow.com/a/54766117/1460422
# Strips ANSI escape codes from stdin, and writes the output to stdout.
# No arguments.
ansi_strip() {
shopt -s extglob; # Enable Bash extended globbing expressions
local line
local IFS=
while read -r line || [[ "$line" ]]; do
echo "${line//$'\e'[\[(]*([0-9;])[@-n]/}";
done
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment