Mayil-AI-Sandbox / loguru-Jan2023

MIT License
0 stars 0 forks source link

Supress bold while preserving colours (hashtag1169) #149

Open vikramsubramanian opened 2 weeks ago

vikramsubramanian commented 2 weeks ago

I am quite happy with the default colours, however the bold text looks off.

Is there a way to preserve all decorations, but remove the bold? )

mayil-ai[bot] commented 2 weeks ago

Some past issues to consider:

Here are some code snippets that may help you get started:

To address the feature request of suppressing bold text in log messages while preserving other text decorations like colors, follow these steps:

Steps to Implement:

  1. Add the new environment variable in loguru/_defaults.py:

    • Define LOGURU_NO_BOLD and set default color settings.
    • Adjust color settings based on the new environment variable.
    # loguru/_defaults.py
    LOGURU_NO_BOLD = env("LOGURU_NO_BOLD", bool, False)
    
    LOGURU_INFO_COLOR = env("LOGURU_INFO_COLOR", str, "<bold>")
    LOGURU_WARNING_COLOR = env("LOGURU_WARNING_COLOR", str, "<yellow><bold>")
    LOGURU_ERROR_COLOR = env("LOGURU_ERROR_COLOR", str, "<red><bold>")
    
    if LOGURU_NO_BOLD:
       LOGURU_INFO_COLOR = LOGURU_INFO_COLOR.replace("<bold>", "")
       LOGURU_WARNING_COLOR = LOGURU_WARNING_COLOR.replace("<bold>", "")
       LOGURU_ERROR_COLOR = LOGURU_ERROR_COLOR.replace("<bold>", "")
  2. Update the feed method in loguru/_colorizer.py to strip out the bold attribute if the environment variable is set:

    # loguru/_colorizer.py
    def feed(self, text, *, raw=False):
       if raw:
           self._tokens.append((TokenType.TEXT, text))
           return
    
       position = 0
    
       for match in self._regex_tag.finditer(text):
           markup, tag = match.group(0), match.group(1)
    
           if LOGURU_NO_BOLD and "bold" in tag:
               tag = tag.replace("bold", "")
    
           self._tokens.append((TokenType.TEXT, text[position : match.start()]))
           position = match.end()
    
           if markup[0] == "\\":
               self._tokens.append((TokenType.TEXT, markup[1:]))
               continue
    
           if markup[1] == "/":
               if self._tags and (tag == "" or tag == self._tags[-1]):
                   self._tags.pop()
                   self._color_tokens.pop()
                   self._tokens.append((TokenType.CLOSING, "\033[0m"))
                   self._tokens.extend(self._color_tokens)
                   continue
               elif tag in self._tags:
                   raise ValueError('Closing tag "%s" violates nesting rules' % markup)
               else:
                   raise ValueError('Closing tag "%s" has no corresponding opening tag' % markup)
    
           if tag in {"lvl", "level"}:
               token = (TokenType.LEVEL, None)
           else:
               ansi = self._get_ansicode(tag)
    
               if ansi is None:
                   raise ValueError(
                       'Tag "%s" does not correspond to any known ansi directive, '
                       "make sure you did not misspelled it (or prepend '\\' to escape it)"
                       % markup
                   )
    
               token = (TokenType.ANSI, ansi)
    
           self._tags.append(tag)
           self._color_tokens.append(token)
           self._tokens.append(token)
    
       self._tokens.append((TokenType.TEXT, text[position:]))
    
    def done(self, *, strict=True):
       if strict and self._tags:
           faulty_tag = self._tags.pop(0)
           raise ValueError('Opening tag "<%s>" has no corresponding closing tag' % faulty_tag)
       return self._tokens
    
    def current_color_tokens(self):
       return list(self._color_tokens)

By following these steps, you will successfully suppress bold text in log messages while preserving other text decorations based on the new environment variable LOGURU_NO_BOLD.

💡 To rerun Mayil, comment mayil-ai rerun. Mayil will incorporate any new context added to the ticket. Include details in your rerun comment to guide Mayil!