Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Float xcursor/ycursor #182

Open
Carreau opened this issue Nov 4, 2015 · 1 comment
Open

Float xcursor/ycursor #182

Carreau opened this issue Nov 4, 2015 · 1 comment

Comments

@Carreau
Copy link
Contributor

Carreau commented Nov 4, 2015

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?

Carreau added a commit to Carreau/python-prompt-toolkit that referenced this issue Nov 4, 2015
@ljluestc
Copy link

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")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants