Code Monkey home page Code Monkey logo

python-curses-wordwrap's Introduction

Custom word wrapper for Python Curses

Python Curses word wrapper image

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.

Using the word wrapper

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)

The arguments

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!

python-curses-wordwrap's People

Contributors

ikarosainasoja avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.