How to color the Git console?

前端 未结 11 1122
借酒劲吻你
借酒劲吻你 2020-11-30 16:07

I recently saw that the git console in Windows is colored, e.g. Green for additions, red for deletions, etc. How do I color my git console like tha

11条回答
  •  天命终不由人
    2020-11-30 16:29

    Well, if you are not satisfied with the default setting, you can use ANSI escape code to help you set the color, and if you want to modify some text, you can write bash to help you. see as below:

    Eaxmplae

    # .gitconfig
    
    [alias]
        st-color = "!f() { \
            echo -n -e '\\033[38;2;255;0;01m\\033[4m' ;\
            git status -s | grep ' D' | \
            sed -e 's/^ ./DELETE:/' ; \
            echo -n -e '\\033[m' ;\
            \
            echo -n -e '\\033[48;2;128;128;128m' ;\
            echo -n -e '\\033[38;2;0;255;01m' ;\
            git status -s | grep ' [AM]' | \
            sed -e 's/^ ./NEW OR MODIFY:/' ; \
            echo -n -e '\\033[m' ;\
            \
            echo -n -e '\\033[38;2;255;0;255m' ;\
            echo Rename ;\
            git status -s | grep 'R ' | \
            sed -e 's/^..//' ; \
            echo -n -e '\\033[m' ;\
        }; f"
    

    demo

    Explanation

    1. you can write the long script on .gitconfig use the syntax as below:

      [alias]
          your-cmd = !f() { \
              \
          }; f"
      
    2. echo -n -e (see more echo)

      • -n = Do not output a trailing newline.
      • -e Enable interpretation of the following backslash-escaped
    3. \\033[38;2;255;0;0m\\033[4m (see more SGR parameters)

      • \\033[38;2;255;0;0m : 38 mean fore color. 255;0;0 = Red | r;g;b
      • \\033[4m : underline
    4. grep : The grep command is used to search text.

    5. sed -e 's/be_replace_string/new_string/' replace string to new string.

提交回复
热议问题