Coder Social home page Coder Social logo

bext's Introduction

Bext

A cross-platform Python 2/3 module for colorful, boring, text-based terminal programs.

Basically, use Bext if you want to move the cursor around the terminal window and have colorful text, like some kind of limited curses module (but it works on Windows also.)

Bext does not currently work in the new Windows Terminal. It does work in the older Command Prompt.

Note: Currently something in Colorama 0.4.6 causes Bext to fail. Please use 0.4.5 or earlier.

Installation

To install with pip, run:

pip install bext

Functions

  • fg(color)

Sets the foreground color, that is, the color of the text. The color is a string of one of the following colors: black, red, green, yellow, blue, purple, cyan, white, reset, random.

  • bg(color)

Sets the background color, that is, the color of the cell behind the text characters. You "paint" a cell with the background color by printing a space character.

  • size()

Returns a tuple of the (width, height) of the current terminal.

  • resize(columns, rows)

Resizes the terminal window to the given number of columns and rows. Returns True if the resize was a success and False if not. Note that on Ubuntu Linux, this function doesn't work if the Terminal window is maximized. On Windows, if the Command Prompt is maximized the window won't change size but the screen buffer size will be resized. This function is not very reliable.

  • clear()

Erase all the text on the screen, paint the entire terminal to the background color, and moves the cursor to (0, 0) at the top-left corner of the screen.

  • clear_line()

Erases the line where the cursor is located.

  • goto(x, y)

Move the cursor to x, y coordinates on the screen. (0, 0) is the top-left corner of the screen.

  • title(text)

Sets the title of the terminal window to text.

  • hide_cursor()

Hides the cursor.

  • show_cursor()

Shows the cursor after hiding it.

  • get_key(blocking=True)

Waits until the user presses a single key on the keyboard, then returns that key as a string. If blocking is False, the function returns immediately (returning None if no key has been pressed.) NOTE: blocking=False is currently broken and doesn't work reliably.

Example

    import bext, random

    width, height = bext.size()

    try:
        while True:
            bext.fg('random')
            bext.bg('random')
            x = random.randint(0, width - 1)
            y = random.randint(0, height - 1)

            if x == width -1 and y == height - 1:
                continue # Windows has weird behavior where a character at the end of the row always moves the cursor to the next row.
            bext.goto(x, y)
            print('*', end='')
    except KeyboardInterrupt:
        pass

Contribute

If you'd like to contribute to Bext, check out https://github.com/asweigart/bext

Support

If you find this project helpful and would like to support its development, consider donating to its creator on Patreon.

bext's People

Contributors

asweigart avatar farisachugthai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

bext's Issues

Can't detect arrow keys in CMD/Windows Terminal

Hi! :) First of all, thanks for the wonderful app!

I have encountered this issue: when I run a script on Windows in CMD or Windows terminal and getKey any of the arrows, the following happens: any arrow is rendered as "a". However, when I run the same code in my IDE, I get '\xe0K', '\xe0M' and so on.

As far as I can see, the problem is that when you try to get key in the native environment, you get "àK", and not "\xe0K".

Any idea how we can fix that? :)

UPD: I've just tested: the "a" that I get in the console is not real "a". Therefore, my theory about "àK" may be onto something...

UPD2: This seems to be the inherited problem of the msvcrt module :(

Python 3.10.7 sys.out.write error

Here is an error message from bext module when bext.hide() is used in analogClock.py example.

File` "...Documents/python/tutorial-env/lib/python3.10/site-packages/bext/__init__.py", line 588, in hide
sys.out.write('\033[?25l')
AttributeError: module 'sys' has no attribute 'out'`

Also, if I experiment and change the 'sys.out.write...' to 'sys.stdout.write...' the next line results in the same error message

File "/home/serkank2/Documents/python/tutorial-env/lib/python3.10/site-packages/bext/__init__.py", line 589, in hide
sys.out.flush()
AttributeError: module 'sys' has no attribute 'out'

Thank you for the great resources.

goto is broken in 0.0.7 for unix/macos

in init.py, I was able to fix goto by going to line 650 and re-inserting:

elif currentPlatform == 'unix':
# macOS and Linux:
import tty, termios, select, codecs # Used by getKey()
getKey = GetKeyUnix().getkey
goto = _goto_control_code

That restored it and I was able to get the bouncing DVD logo's program from chapter 5 of your book to run then. Not sure what the right fix is, but 0.0.7 appears broken at the moment. It also fixed the example rotating_cube.py too. This is on linux with with python3.9 and bext-0.0.7.

AttributeError: 'GetKeyUnix' object has no attribute 'getcharsUnix'

Good evening, using your library I came across two errors the
1st in line 568 and 569 was about sys.out.*** I fixed it on sys.stdout.*** after that the error "the out argument was not found" disappeared
Еhe 2nd problem that I can't solve at the moment and to understand what can be done about it :
line 373, in getkey
for c in self.getcharsUnix(blocking):
AttributeError: 'GetKeyUnix' object has no attribute 'getcharsUnix'

I work on the Ubuntu operating system

Yellow foreground color doesn't work on Windows

Yellow appears as white on Windows. However, this only happens when I run cmd.exe directly, and not when I run the "Command Prompt" shortcut from the Start menu. I'm not sure what the difference is.

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.