Skip to content

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

@obskyr

Description

@obskyr

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions