| 1 |
# Copyright 2014 Open Source Robotics Foundation, Inc. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
"""Provides regular expression functions for matching ansi escape sequences.""" |
| 16 |
|
| 17 |
import re |
| 18 |
|
| 19 |
_ansi_re = re.compile(r'\033\[\d{1,2}[m]') |
| 20 |
_ansi_re_group = re.compile(r'(\033\[\d{1,2}[m])') |
| 21 |
|
| 22 |
|
| 23 |
def split_by_ansi_escape_sequence(string, include_delimiters=False): |
| 24 |
""" |
| 25 |
Splits a string into a list using any ansi escape sequence as a delimiter. |
| 26 |
|
| 27 |
:param string: string to be split |
| 28 |
:type string: str |
| 29 |
:param include_delimiters: If True include matched escape sequences in |
| 30 |
the list (default: False) |
| 31 |
:type include_delimiters: bool |
| 32 |
:returns: list of strings, split from original string by escape sequences |
| 33 |
:rtype: list |
| 34 |
""" |
| 35 |
global _ansi_re, _ansi_re_group |
| 36 |
if include_delimiters: |
| 37 |
return _ansi_re_group.split(string) |
| 38 |
return _ansi_re.split(string) |
| 39 |
|
| 40 |
|
| 41 |
def remove_ansi_escape_sequences(string): |
| 42 |
""" |
| 43 |
Removes any ansi escape sequences found in the given string and returns it. |
| 44 |
""" |
| 45 |
global _ansi_re |
| 46 |
return _ansi_re.sub('', string) |
| 47 |
|
| 48 |
|
| 49 |
# Backwards compatibility |
| 50 |
remove_ansi_escape_senquences = remove_ansi_escape_sequences |