prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.37k stars 716 forks source link

Float xcursor/ycursor #182

Open Carreau opened 9 years ago

Carreau commented 9 years ago

I think I understand the usage of (x|y)cursor in Float that are after used here in write_to_screen, do you think it could be a better/simpler idea to have a singleton cursor_pos = object() (or even a specific string "cursor") that could could be use for Floats top/left/bottom/right arguments ?

The advantage of the Cursor singleton object is you could implement __add__/__sub__ dunder,

And do things like:

from prompt_toolkit.shortcut import cursor as c
Float(left=c+1, top=c-3)

Thoughts?

ljluestc commented 1 year ago

from prompt_toolkit.layout import Float from prompt_toolkit.formatted_text import HTML

class Cursor: instance = None

def __new__(cls):
    if cls.instance is None:
        cls.instance = super(Cursor, cls).__new__(cls)
    return cls.instance

def __add__(self, value):
    return self

def __sub__(self, value):
    return self

cursor = Cursor()

def write_to_screen(text, cursor_pos, color): return [ Float( top=cursor_pos, left=cursor_pos, content=HTML(text), background_color=color, ) ]

layout = write_to_screen("Hello", cursor + 1, "red") + write_to_screen("World", cursor - 3, "blue")