This module employs regular expressions to segment the input string into discrete components, such as words and spaces. It attempts to insert these components individually into the container window, ensuring proper word wrapping.
The simplest way to utilize this module is to download the word_wrapper.py file, and copying it into the same directory as the file requiring word wrapping.
Import the module inside your main python file with ìmport word_wrapper
To use the wrapper in your code, call the wordwrap function and provide the required arguments:
word_wrapper.wordwrap(window, string, attr=0)
Argument | Description |
---|---|
window | The name of the Curses window to print in. This can also be a name of a subwin or derwin. |
string | The name of the variable that is holding the string to be wrapped. Line breaks and other whitespace characters are processed normally. |
attr | An optional Curses text attribute, e.g. curses.A_REVERSE. If left blank, it deafults as 0 (no attribute). |
Example:
# import curses mobule
import curses
from curses import wrapper
# import the word wrapper
import word_wrapper as ww
def main(stdscr):
# string to wrap
message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n\nNam tincidunt dui quis vestibulum feugiat."
# clear and refresh the terminal
stdscr.clear()
stdscr.refresh()
# let's create a new window, clear it and draw a border
my_window = curses.newwin(10, 20, 7, 30)
my_window.clear()
my_window.box()
# maybe we want another window inside the first one
inner_window = my_window.derwin(8, 18, 1, 1)
inner_window.clear()
# use the word wrapper
ww.wordwrap(inner_window, message)
# refresh the windows
inner_window.refresh()
my_window.refresh()
# wait for user input
stdscr.getch()
# end curses application
wrapper(main)
About the above example: Please don't be misled by the 'wrapper' imported from the Curses library; it is unrelated to the Word wrapper. It simply serves as a function to automatically handle the initialization and exit processes of a Curses application.
Any comments and critique are welcome!