├── .gitignore ├── LICENSE ├── MANIFEST.in ├── README.md ├── README.rst ├── pyterm.jpg ├── setup.py └── term.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | test.py 3 | build/ 4 | dist/ 5 | py_term.egg-info 6 | py-term-0.1/ 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2021 René Tanczos 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include README.md 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # py-term 2 | Python module to style terminal output, moving and positioning the cursor. 3 | 4 | **Python 2 and 3 compatible** 5 | 6 | ![alt text](pyterm.jpg "See? amazing!") 7 | 8 | ## Installation 9 | 10 | Install through **pip**. 11 | 12 | ``` 13 | $ pip install py-term 14 | ``` 15 | 16 | or get from source 17 | 18 | ``` 19 | $ git clone https://github.com/gravmatt/py-term.git 20 | $ cd py-term 21 | $ python setup.py install 22 | ``` 23 | 24 | ## Import module 25 | 26 | Import the module into your python project. 27 | 28 | ``` 29 | import term 30 | ``` 31 | 32 | ## Usage 33 | 34 | ``` 35 | term.write('Hello, ') 36 | term.write('Python!') 37 | 38 | > Hello, Python! 39 | 40 | term.writeLine('Hello') 41 | term.writeLine('Python!') 42 | 43 | > Hello 44 | > Python! 45 | ``` 46 | 47 | ## Style output 48 | 49 | The first argument is the text output and all following arguments are for styling. 50 | 51 | ``` 52 | term.writeLine(text, *style) 53 | 54 | Or 55 | 56 | text = term.format(text, *style) 57 | term.writeLine(text) 58 | ``` 59 | 60 | ### Usage 61 | 62 | ``` 63 | term.writeLine('Hello, Python!') 64 | 65 | term.writeLine('This text line will be green', term.green) 66 | 67 | term.writeLine('Reverse the green color', term.green, term.reverse) 68 | ``` 69 | 70 | Or 71 | 72 | ``` 73 | output = term.format('Hello, ', term.green) + term.format('Python!', term.blue, term.bold) 74 | 75 | term.writeLine(output) 76 | 77 | term.write(term.format('All in one line', term.reverse)) 78 | ``` 79 | 80 | #### Text align 81 | 82 | **Center align** 83 | 84 | ``` 85 | # term.textCenter(text) 86 | 87 | term.writeLine(term.textCenter('Super Python!')) 88 | ``` 89 | 90 | **Right align** 91 | 92 | ``` 93 | # term.textRight(text) 94 | 95 | term.writeLine(term.textRight('Rene Tanczos (@gravmatt)')) 96 | 97 | ( Function term.right() to align text is depricated because of naming conflicts! ) 98 | ``` 99 | 100 | ##### Style attributes 101 | 102 | | Code | Description | 103 | | :-------------------- | :----------------------------------- | 104 | | term.off | All attributes off | 105 | | term.bold | Bold | 106 | | term.dim | Dim | 107 | | term.underscore | Underscore (monochrome display only) | 108 | | term.blink | Blink | 109 | | term.reverse | Reverse | 110 | | term.hide | Hide | 111 | 112 | ##### Text color 113 | 114 | | Code | Color | 115 | | :----------------- | :---------- | 116 | | term.black | Black | 117 | | term.red | Red | 118 | | term.green | Green | 119 | | term.yellow | Yellow | 120 | | term.blue | Blue | 121 | | term.magenta | Magenta | 122 | | term.cyan | Cyan | 123 | | term.white | White | 124 | 125 | ##### Background color 126 | 127 | | Code | Color | 128 | | :------------------- | :---------- | 129 | | term.bgblack | Black | 130 | | term.bgred | Red | 131 | | term.bggreen | Green | 132 | | term.bgyellow | Yellow | 133 | | term.bgblue | Blue | 134 | | term.bgMagenta | Magenta | 135 | | term.bgcyan | Cyan | 136 | | term.bgwhite | White | 137 | 138 | ## Remove style attributes 139 | 140 | Removes style characters. 141 | 142 | (Good to call before you count a string) 143 | ``` 144 | term.strip(formatted_text) 145 | 146 | hello = term.red + 'hello, world' + term.off 147 | print hello 148 | # '\x1b[31mhello, world\x1b[0m\x1b[27m' 149 | 150 | print term.strip(hello) 151 | # hello, world 152 | ``` 153 | 154 | ## Highlighting text 155 | 156 | Simple highlighting of unformatted text strings 157 | ``` 158 | def callback(matching_text): 159 | return term.blue + matching_text + term.off 160 | 161 | output, match_count, array_of_positions = term.highlight(regex_pattern, text, callback) 162 | ``` 163 | 164 | Return a tuple. 165 | 166 | output (index 0) = formatted text output 167 | 168 | match_count (index 1) = match count of the pattern 169 | 170 | array_of_positions (index 2) = array of tuples with start and stop points of the matches [(4, 6), (9, 11), ..] 171 | 172 | ## Set title 173 | 174 | ``` 175 | term.setTitle('Hello Terminal') 176 | 177 | # or clear it 178 | 179 | term.clearTitle() 180 | ``` 181 | 182 | ## Set tab name 183 | 184 | ``` 185 | term.setTab('Hello Tab') 186 | 187 | # or clear it 188 | 189 | term.clearTab() 190 | ``` 191 | 192 | ## Cursor position 193 | 194 | Move the cursor to a specific position. 195 | ``` 196 | term.pos(line, column) 197 | 198 | term.pos(2, 15) 199 | ``` 200 | 201 | Get the size of the terminal (lines and columns) 202 | 203 | ``` 204 | (30, 100) = term.getSize() 205 | 206 | # (lines, colums) = term.getSize() 207 | ``` 208 | 209 | Move the cursor to the home position (1, 1). 210 | 211 | ``` 212 | term.homePos() 213 | ``` 214 | 215 | Moves the current cursor position up, down, left or right by the specified value. 216 | 217 | ``` 218 | term.up(value=1) 219 | term.down(value=1) 220 | term.left(value=1) 221 | term.right(value=1) 222 | ``` 223 | 224 | Saves the current cursor position. 225 | 226 | ``` 227 | term.saveCursor() 228 | ``` 229 | 230 | Restore the previously stored cursor position. 231 | 232 | ``` 233 | term.restoreCursor() 234 | ``` 235 | 236 | Clear the terminal screen. 237 | 238 | ``` 239 | term.clear() 240 | ``` 241 | 242 | Clear the entire line on the current cursor position. 243 | 244 | ``` 245 | term.clearLine() 246 | ``` 247 | 248 | Clear line from the current cursor position to the end. 249 | 250 | ``` 251 | term.clearLineFromPos() 252 | ``` 253 | 254 | Clear line from begin to current cursor position. 255 | 256 | ``` 257 | term.clearLineToPos() 258 | ``` 259 | 260 | ## Licence 261 | 262 | The MIT License (MIT) 263 | 264 | Copyright (c) 2015-2021 René Tanczos 265 | 266 | Permission is hereby granted, free of charge, to any person obtaining a copy 267 | of this software and associated documentation files (the "Software"), to deal 268 | in the Software without restriction, including without limitation the rights 269 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 270 | copies of the Software, and to permit persons to whom the Software is 271 | furnished to do so, subject to the following conditions: 272 | 273 | The above copyright notice and this permission notice shall be included in all 274 | copies or substantial portions of the Software. 275 | 276 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 277 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 278 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 279 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 280 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 281 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 282 | SOFTWARE. 283 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | py-term 2 | ======= 3 | 4 | Python module to style terminal output, moving and positioning the 5 | cursor. 6 | 7 | **Python 2 and 3 compatible** 8 | 9 | Installation 10 | ------------ 11 | 12 | Install through **pip**. 13 | 14 | :: 15 | 16 | $ pip install py-term 17 | 18 | or get from source 19 | 20 | :: 21 | 22 | $ git clone https://github.com/gravmatt/py-term.git 23 | $ cd py-term 24 | $ python setup.py install 25 | 26 | Import module 27 | ------------- 28 | 29 | Import the module into your python project. 30 | 31 | :: 32 | 33 | import term 34 | 35 | Usage 36 | ----- 37 | 38 | :: 39 | 40 | term.write('Hello, ') 41 | term.write('Python!') 42 | 43 | > Hello, Python! 44 | 45 | term.writeLine('Hello') 46 | term.writeLine('Python!') 47 | 48 | > Hello 49 | > Python! 50 | 51 | Style output 52 | ------------ 53 | 54 | The first argument is the text output and all following arguments are 55 | for styling. 56 | 57 | :: 58 | 59 | term.writeLine(text, *style) 60 | 61 | Or 62 | 63 | text = term.format(text, *style) 64 | term.writeLine(text) 65 | 66 | Usage 67 | ~~~~~ 68 | 69 | :: 70 | 71 | term.writeLine('Hello, Python!') 72 | 73 | term.writeLine('This text line will be green', term.green) 74 | 75 | term.writeLine('Reverse the green color', term.green, term.reverse) 76 | 77 | Or 78 | 79 | :: 80 | 81 | output = term.format('Hello, ', term.green) + term.format('Python!', term.blue, term.bold) 82 | 83 | term.writeLine(output) 84 | 85 | term.write(term.format('All in one line', term.reverse)) 86 | 87 | Text align 88 | ^^^^^^^^^^ 89 | 90 | **Center align** 91 | 92 | :: 93 | 94 | # term.textCenter(text) 95 | 96 | term.writeLine(term.textCenter('Super Python!')) 97 | 98 | **Right align** 99 | 100 | :: 101 | 102 | # term.textRight(text) 103 | 104 | term.writeLine(term.textRight('Rene Tanczos (@gravmatt)')) 105 | 106 | ( Function term.right() to align text is depricated because of naming conflicts! ) 107 | 108 | Style attributes 109 | '''''''''''''''' 110 | 111 | +-------------------+----------------------------------------+ 112 | | Code | Description | 113 | +===================+========================================+ 114 | | term.off | All attributes off | 115 | +-------------------+----------------------------------------+ 116 | | term.bold | Bold | 117 | +-------------------+----------------------------------------+ 118 | | term.dim | Dim | 119 | +-------------------+----------------------------------------+ 120 | | term.underscore | Underscore (monochrome display only) | 121 | +-------------------+----------------------------------------+ 122 | | term.blink | Blink | 123 | +-------------------+----------------------------------------+ 124 | | term.reverse | Reverse | 125 | +-------------------+----------------------------------------+ 126 | | term.hide | Hide | 127 | +-------------------+----------------------------------------+ 128 | 129 | Text color 130 | '''''''''' 131 | 132 | +----------------+-----------+ 133 | | Code | Color | 134 | +================+===========+ 135 | | term.black | Black | 136 | +----------------+-----------+ 137 | | term.red | Red | 138 | +----------------+-----------+ 139 | | term.green | Green | 140 | +----------------+-----------+ 141 | | term.yellow | Yellow | 142 | +----------------+-----------+ 143 | | term.blue | Blue | 144 | +----------------+-----------+ 145 | | term.magenta | Magenta | 146 | +----------------+-----------+ 147 | | term.cyan | Cyan | 148 | +----------------+-----------+ 149 | | term.white | White | 150 | +----------------+-----------+ 151 | 152 | Background color 153 | '''''''''''''''' 154 | 155 | +------------------+-----------+ 156 | | Code | Color | 157 | +==================+===========+ 158 | | term.bgblack | Black | 159 | +------------------+-----------+ 160 | | term.bgred | Red | 161 | +------------------+-----------+ 162 | | term.bggreen | Green | 163 | +------------------+-----------+ 164 | | term.bgyellow | Yellow | 165 | +------------------+-----------+ 166 | | term.bgblue | Blue | 167 | +------------------+-----------+ 168 | | term.bgMagenta | Magenta | 169 | +------------------+-----------+ 170 | | term.bgcyan | Cyan | 171 | +------------------+-----------+ 172 | | term.bgwhite | White | 173 | +------------------+-----------+ 174 | 175 | Remove style attributes 176 | ----------------------- 177 | 178 | Removes style characters. 179 | 180 | (Good to call before you count a string) 181 | 182 | :: 183 | 184 | term.strip(formatted_text) 185 | 186 | hello = term.red + 'hello, world' + term.off 187 | print hello 188 | # '\x1b[31mhello, world\x1b[0m\x1b[27m' 189 | 190 | print term.strip(hello) 191 | # hello, world 192 | 193 | Highlighting text 194 | ----------------- 195 | 196 | Simple highlighting of unformatted text strings 197 | 198 | :: 199 | 200 | def callback(matching_text): 201 | return term.blue + matching_text + term.off 202 | 203 | output, match_count, array_of_positions = term.highlight(regex_pattern, text, callback) 204 | 205 | Return a tuple. 206 | 207 | output (index 0) = formatted text output 208 | 209 | match\_count (index 1) = match count of the pattern 210 | 211 | array\_of\_positions (index 2) = array of tuples with start and stop 212 | points of the matches [(4, 6), (9, 11), ..] 213 | 214 | Set title 215 | --------- 216 | 217 | :: 218 | 219 | term.setTitle('Hello Terminal') 220 | 221 | # or clear it 222 | 223 | term.clearTitle() 224 | 225 | Set tab name 226 | ------------ 227 | 228 | :: 229 | 230 | term.setTab('Hello Tab') 231 | 232 | # or clear it 233 | 234 | term.clearTab() 235 | 236 | Cursor position 237 | --------------- 238 | 239 | Move the cursor to a specific position. 240 | 241 | :: 242 | 243 | term.pos(line, column) 244 | 245 | term.pos(2, 15) 246 | 247 | Get the size of the terminal (lines and columns) 248 | 249 | :: 250 | 251 | (30, 100) = term.getSize() 252 | 253 | # (lines, colums) = term.getSize() 254 | 255 | Move the cursor to the home position (1, 1). 256 | 257 | :: 258 | 259 | term.homePos() 260 | 261 | Moves the current cursor position up, down, left or right by the 262 | specified value. 263 | 264 | :: 265 | 266 | term.up(value=1) 267 | term.down(value=1) 268 | term.left(value=1) 269 | term.right(value=1) 270 | 271 | Saves the current cursor position. 272 | 273 | :: 274 | 275 | term.saveCursor() 276 | 277 | Restore the previously stored cursor position. 278 | 279 | :: 280 | 281 | term.restoreCursor() 282 | 283 | Clear the terminal screen. 284 | 285 | :: 286 | 287 | term.clear() 288 | 289 | Clear the entire line on the current cursor position. 290 | 291 | :: 292 | 293 | term.clearLine() 294 | 295 | Clear line from the current cursor position to the end. 296 | 297 | :: 298 | 299 | term.clearLineFromPos() 300 | 301 | Clear line from begin to current cursor position. 302 | 303 | :: 304 | 305 | term.clearLineToPos() 306 | 307 | Licence 308 | ------- 309 | 310 | The MIT License (MIT) 311 | 312 | Copyright (c) 2015-2021 René Tanczos 313 | 314 | Permission is hereby granted, free of charge, to any person obtaining a 315 | copy of this software and associated documentation files (the 316 | "Software"), to deal in the Software without restriction, including 317 | without limitation the rights to use, copy, modify, merge, publish, 318 | distribute, sublicense, and/or sell copies of the Software, and to 319 | permit persons to whom the Software is furnished to do so, subject to 320 | the following conditions: 321 | 322 | The above copyright notice and this permission notice shall be included 323 | in all copies or substantial portions of the Software. 324 | 325 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 326 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 327 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 328 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 329 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 330 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 331 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 332 | -------------------------------------------------------------------------------- /pyterm.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gravmatt/py-term/a45c22fb66a14292619a6e02d7a113f038b75fad/pyterm.jpg -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys 4 | try: 5 | from setuptools import setup 6 | except ImportError: 7 | from distutils.core import setup 8 | 9 | if sys.version_info < (2, 6): 10 | raise NotImplementedError("Sorry, you need at least Python 2.6 or Python 3.2+ to use py-term.") 11 | 12 | import term 13 | 14 | with open('README.md', 'r') as f: 15 | longDesc = f.read() 16 | 17 | setup(name='py-term', 18 | version=term.__version__, 19 | description='Python module to style terminal output, moving and positioning the cursor.', 20 | long_description=longDesc, 21 | long_description_content_type='text/markdown', 22 | author='Rene Tanczos', 23 | author_email='gravmatt@gmail.com', 24 | url='https://github.com/gravmatt/py-term', 25 | py_modules=['term'], 26 | scripts=['term.py'], 27 | license='MIT', 28 | platforms=['MacOSX', 'UNIX/Linux'], 29 | classifiers=['Intended Audience :: Developers', 30 | 'License :: OSI Approved :: MIT License', 31 | 'Programming Language :: Python :: 2.5', 32 | 'Programming Language :: Python :: 2.6', 33 | 'Programming Language :: Python :: 2.7', 34 | 'Programming Language :: Python :: 3', 35 | 'Programming Language :: Python :: 3.1', 36 | 'Programming Language :: Python :: 3.2', 37 | 'Programming Language :: Python :: 3.3', 38 | 'Programming Language :: Python :: 3.4', 39 | 'Programming Language :: Python :: 3.5', 40 | 'Programming Language :: Python :: 3.6', 41 | 'Programming Language :: Python :: 3.7', 42 | 'Programming Language :: Python :: 3.8', 43 | 'Programming Language :: Python :: 3.9', 44 | 'Programming Language :: Python :: 3.10', 45 | 'Intended Audience :: Developers', 46 | 'Programming Language :: Python', 47 | 'Development Status :: 5 - Production/Stable', 48 | 'Operating System :: MacOS :: MacOS X', 49 | 'Operating System :: POSIX', 50 | 'Topic :: Terminals', 51 | 'Environment :: Console' 52 | ], 53 | ) 54 | -------------------------------------------------------------------------------- /term.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Copyright (c) 2015-2016, René Tanczos (Twitter @gravmatt) 5 | The MIT License (MIT) 6 | 7 | pyterm helps positioning the cursor and styling output inside the terminal. 8 | 9 | Project on github https://github.com/gravmatt/py-term 10 | """ 11 | 12 | __author__ = 'Rene Tanczos' 13 | __version__ = '0.7' 14 | __license__ = 'MIT' 15 | 16 | import sys 17 | import re 18 | import os 19 | from subprocess import Popen, PIPE 20 | 21 | 22 | off = '\033[0m\033[27m' 23 | bold = '\033[1m' 24 | dim = '\033[2m' 25 | underscore = '\033[4m' 26 | blink = '\033[5m' 27 | reverse = ' \033[7m' 28 | hide = '\033[8m' 29 | 30 | black = '\033[30m' 31 | red = '\033[31m' 32 | green = '\033[32m' 33 | yellow = '\033[33m' 34 | blue = '\033[34m' 35 | magenta = '\033[35m' 36 | cyan = '\033[36m' 37 | white = '\033[37m' 38 | 39 | bgblack = '\033[40m' 40 | bgred = '\033[41m' 41 | bggreen = '\033[42m' 42 | bgyellow = '\033[43m' 43 | bgblue = '\033[44m' 44 | bgmagenta = '\033[45m' 45 | bgcyan = '\033[46m' 46 | bgwhite = '\033[47m' 47 | 48 | 49 | def send(cmd): 50 | sys.stdout.write(cmd) 51 | sys.stdout.flush() 52 | 53 | 54 | def pos(line, column): 55 | send('\033[%s;%sf' % (line, column)) 56 | 57 | 58 | def homePos(): 59 | send('\033[H') 60 | 61 | 62 | def up(value=1): 63 | send('\033[%sA' % value) 64 | 65 | 66 | def down(value=1): 67 | send('\033[%sB' % value) 68 | 69 | 70 | def right(value=1): 71 | send('\033[%sC' % value) 72 | 73 | 74 | def left(value=1): 75 | send('\033[%sD' % value) 76 | 77 | 78 | def saveCursor(): 79 | send('\0337') 80 | # send('\033[s') 81 | 82 | 83 | def restoreCursor(): 84 | send('\0338') 85 | # send('\033[u') 86 | 87 | 88 | def clear(): 89 | send('\033[2J') 90 | 91 | 92 | def clearLineFromPos(): 93 | send('\033[K') 94 | 95 | 96 | def clearLineToPos(): 97 | send('\033[1K') 98 | 99 | 100 | def clearLine(): 101 | send('\033[2K') 102 | 103 | 104 | def write(text='', *style): 105 | send(format(text, *style)) 106 | 107 | 108 | def writeLine(text='', *style): 109 | write(str(text) + '\n', *style) 110 | 111 | 112 | def setTitle(name): 113 | send('\033]2;%s\007' % name) 114 | 115 | 116 | def clearTitle(): 117 | setTitle('') 118 | 119 | 120 | def setTab(name): 121 | send('\033]1;%s\007' % name) 122 | 123 | 124 | def clearTab(): 125 | setTab('') 126 | 127 | 128 | def strip(text): 129 | return re.sub('\x1b\[[0-9]{1,2}m', '', text) 130 | 131 | 132 | def textCenter(text): 133 | return ' ' * (int(getSize()[1] / 2) - int(len(strip(text)) / 2)) + text 134 | 135 | 136 | def center(text): 137 | ''' 138 | DEPRICATED: Use textCenter() instead! 139 | Will be removed in later verions! 140 | ''' 141 | textCenter(text) 142 | 143 | 144 | def textRight(text): 145 | return ' ' * (getSize()[1] - len(strip(text))) + text 146 | 147 | 148 | def getSize(): 149 | import platform 150 | os_sys = platform.system() 151 | if(os_sys in ['Linux', 'Darwin'] or os_sys.startswith('CYGWIN')): 152 | try: 153 | def __get_unix_terminal_size(fd): 154 | import fcntl, termios, struct 155 | return struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, 'rene')) 156 | cr = __get_unix_terminal_size(0) or __get_unix_terminal_size(1) or __get_unix_terminal_size(2) 157 | if(not cr): 158 | fd = os.open(os.ctermid(), os.O_RDONLY) 159 | cr = __get_unix_terminal_size(fd) 160 | os.close(fd) 161 | return cr 162 | except: 163 | pass 164 | else: 165 | raise Exception('operating system not supported') 166 | 167 | 168 | def format(text, *style): 169 | if(style): 170 | return '%s%s%s' % (''.join(style), text, off) 171 | else: 172 | return text 173 | 174 | 175 | def highlight(pattern, text, func): 176 | output = '' 177 | idx = 0 178 | matches = [(m.start(), m.end()) for m in re.finditer(pattern, text)] 179 | for p in matches: 180 | output += text[idx:p[0]] 181 | output += func(text[p[0]:p[1]]) 182 | idx = p[1] 183 | output += text[idx:] 184 | return (output, len(matches), matches) 185 | --------------------------------------------------------------------------------