Search code examples
dartvisual-studio-codesyntaxcolorsvscode-debugger

Dart (flutter) VScode debug console syntax / expression coloring / highlighting


Does anyone know how to color or highlight syntax or expressions in the debug console in dart (flutter) for VScode efficiently? Most of the code comes out blue, but for instance, I want to set regular print statements as a different color.


Solution

  • ANSI escape code can be used,

    void main() {
      printWarning("Warning");
      printError("Error");
      printInfo("Info");
      printSuccess("Succes");
    }
    
    void printWarning(String text) {
      print('\x1B[33m$text\x1B[0m');
    }
    
    void printError(String text) {
      print('\x1B[31m$text\x1B[0m');
    }
    
    void printInfo(String text) {
      print('\x1B[34m$text\x1B[0m');
    }
    
    void printSuccess(String text) {
      print('\x1B[32m$text\x1B[0m');
    }
    

    The output is enter image description here

    Meaning of ANSI escape code is

    • \x1B: ANSI escape sequence starting and ending marker
    • [31m: Escape sequence for red
    • [0m: Escape sequence for reset (stop making the text red)

    For different colors:

    Black:   \x1B[30m
    Red:     \x1B[31m
    Green:   \x1B[32m
    Yellow:  \x1B[33m
    Blue:    \x1B[34m
    Magenta: \x1B[35m
    Cyan:    \x1B[36m
    White:   \x1B[37m
    Reset:   \x1B[0m
    

    For more reference refer https://stackoverflow.com/a/65622986/13431819