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

Register.copy() does not successfully copy a register. #57

Open
obskyr opened this issue Feb 27, 2025 · 0 comments
Open

Register.copy() does not successfully copy a register. #57

obskyr opened this issue Feb 27, 2025 · 0 comments

Comments

@obskyr
Copy link

obskyr commented Feb 27, 2025

The issue

It seems that Register.copy() does not, in fact, copy a register – it fails to copy the values. Try this:

from sty import fg

print(f"{fg.red}Hello!{fg.rs}")

fg_copy = fg.copy()
print(f"{fg_copy.red}Hello!{fg_copy.rs}")

The latter fails to contain color codes:

This can be confirmed with the Register.as_dict() method:

>>> fg_copy.as_dict()
{'black': '', 'blue': '', 'cyan': '', 'da_black': '', 'da_blue': '', 'da_cyan': '', 'da_green': '', 'da_grey': '', 'da_magenta': '', 'da_red': '', 'da_yellow': '', 'green': '', 'grey': '', 'li_blue': '', 'li_cyan': '', 'li_green': '', 'li_grey': '', 'li_magenta': '', 'li_red': '', 'li_yellow': '', 'magenta': '', 'red': '', 'rs': '', 'white': '', 'yellow': ''}

Details on my environment:

  • OS: Windows 10
  • Shell: Powershell 7.4.6
  • Python version: 3.13.2
  • Sty version: 1.0.6

A workaround

For now, the following function can be used as a workaround!

from sty import register

def copy_register(register: Register):
    ret = register.copy()
    for key, value in register.as_dict().items():
        setattr(ret, key, value)
    return ret

With it, the following now works:

from sty import fg

print(f"{fg.red}Hello!{fg.rs}")

fg_copy = copy_register(fg)
print(f"{fg_copy.red}Hello!{fg_copy.rs}")

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

1 participant