csh Prompt全描述--定製自己的linux命令列提示符

harriszh發表於2022-04-21

cshell Prompt主要由兩部分組成, 一個是顏色定義,一個是資訊顯示。下面我們分別講講述這兩部分。

顏色(ASCII color)

所有顏色都以\033[xxxm的形式輸寫,xxx是以分號分隔的引數

C

printf("\033[31;1;4mHello\033[0m");

C++

std::cout<<"\033[31;1;4mHello\033[0m";

python3

print("\033[31;1;4mHello\033[0m")

shell

echo -e "\033[31;1;4mHello\033[0m"

字型效果

CodeEffectNote
0Reset / Normalall attributes off
1Bold or increased intensity
2Faint (decreased intensity)Not widely supported.
3ItalicNot widely supported. Sometimes treated as inverse.
4Underline
5Slow Blinkless than 150 per minute
6Rapid BlinkMS-DOS ANSI.SYS; 150+ per minute; not widely supported
7[[reverse video]]swap foreground and background colors
8ConcealNot widely supported.
9Crossed-outCharacters legible, but marked for deletion. Not widely supported.
10Primary(default) font
11–19Alternate fontSelect alternate font n-10
20Frakturhardly ever supported
21Bold off or Double UnderlineBold off not widely supported; double underline hardly ever supported.
22Normal color or intensityNeither bold nor faint
23Not italic, not Fraktur
24Underline offNot singly or doubly underlined
25Blink off
27Inverse off
28Revealconceal off
29Not crossed out
30–37Set foreground colorSee color table below
38Set foreground colorNext arguments are 5;<n> or 2;<r>;<g>;, see below
39Default foreground colorimplementation defined (according to standard)
40–47Set background colorSee color table below
48Set background colorNext arguments are 5;<n> or 2;<r>;<g>;, see below
49Default background colorimplementation defined (according to standard)
51Framed
52Encircled
53Overlined
54Not framed or encircled
55Not overlined
60ideogram underlinehardly ever supported
61ideogram double underlinehardly ever supported
62ideogram overlinehardly ever supported
63ideogram double overlinehardly ever supported
64ideogram stress markinghardly ever supported
65ideogram attributes offreset the effects of all of 60-64
90–97Set bright foreground coloraixterm (not in standard)
100–107Set bright background coloraixterm (not in standard)

4bit 顏色碼

原始規定中只支援8種顏色, SGR用30-37定義前景色,用40-47定義背景色。

  • 如果要在白色背景上得到黑色定休,可以用\033[33;47m, 30是前景色, 47是背景色。
  • 要得到鮮豔紅色,用\033[1;31m, 1表示鮮豔。
  • 要重置所有屬性, 用\033[0m
  • 要直接使用鮮豔色,可以用90-97和100-107

4bit

根據上面圖形, 如果要用紅色前景色,綠色背景色, 可以這樣寫:\033[31;42m

8bit 顏色碼

前景色:\033[38;5;XXXm
背景色: \033[48;5;XXXm

8bit

根據上面圖形,

  • 如果要用粉紅字型, 可以這樣寫: \033[38;5;206m, 206是它的前景色
  • 如果要用清晨綠背景,可以這樣寫:\033[48;5;57m, 57是它的背景色

可以合併起來:\033[38;5;206;48;5;57m
8bit_show

8bit 顏色碼大約範圍如下:

範圍描述
0x00-0x07標準色,同4bit
0x08-0x0F高度色
0x10-0xE76x6x6 空間(216色): 16 + 36 x r + 6 x g + b (0<=r, g, b <=5)
0xE8-0xFF從黑到白的24階灰度

24bit 全色系

如果支援true color, 那麼就可以用下面的方式來描述顏色:

前景色: \033[38;2;<r>;<g>;<b>m
背景色: \033[48;2;<r>;<g>;<b>m

可以混合描述前景和背景色:

\033[38;2;255;82;197;48;2;155;106;0mHello

24bit_show

scripts to print

#!/usr/bin/python

print "\\033[XXm"

for i in range(30,37+1):
    print "\033[%dm%d\t\t\033[%dm%d" % (i,i,i+60,i+60);

print "\033[39m\\033[49m - Reset colour"
print "\\033[2K - Clear Line"
print "\\033[<L>;<C>H OR \\033[<L>;<C>f puts the cursor at line L and column C."
print "\\033[<N>A Move the cursor up N lines"
print "\\033[<N>B Move the cursor down N lines"
print "\\033[<N>C Move the cursor forward N columns"
print "\\033[<N>D Move the cursor backward N columns"
print "\\033[2J Clear the screen, move to (0,0)"
print "\\033[K Erase to end of line"
print "\\033[s Save cursor position"
print "\\033[u Restore cursor position"
print " "
print "\\033[4m  Underline on"
print "\\033[24m Underline off"
print "\\033[1m  Bold on"
print "\\033[21m Bold off"

終端及資訊格式

cshell裡, 在%`{...%}裡的內容表示它不佔空間(0-width).
\e[...m\033[...m表示顏色.如上面所描述。

顏色裡\033[0m是用來reset顏色

prompt 格式

符號描述
%/Current working directory.
%~cwd. If it starts with $HOME, that part is replaced by a ~. In addition if a directory name prefix matches a user's home directory, that part of the directory will be substituted with ~user. NOTE: The ~user substitution will only happen if the shell has performed a ~ expansion for that user name in this session.
%c or %.Trailing component of cwd, may be followed by by a digit to get more than one component, if it starts with $HOME, that part is replaced with a ~.
%CTrailing component of cwd, may be followed by a digit to get more than one component, no ~ substitution.
%h, %!, !Current history event number.
%MThe full machine hostname.
%mThe hostname up to the first ".".
%S (%s)Start (stop) standout mode.
%B (%b)Start (stop) boldfacing mode. (Only if
tcsh was compiled to be eight bit clean.)
%U (%u)Start (stop) underline mode. (Only if
tcsh was compiled to be eight bit clean.)
%t or %@Current time of day, in 12-hour, am/pm format.
%TCurrent time of day, in 24-hour format.
(But see the ampm shell variable below.)
%pCurrent time in 12-hour format, am/pm format with seconds.
%PCurrent time in 24-hour format, with seconds.
\c`c' is parsed the same way as in bindkey.
^c`c' is parsed the same way as in bindkey.
%%A single %.
%nThe user name, contents of $user.
%dThe weekday in <Day> format.
%DThe day in dd format.
%wThe month in <Mon> format.
%WThe month in mm format.
%yThe year in yy format.
%YThe year in yyyy format.
%lThe line (tty) the user is logged on.
%Lclear from prompt to end of display or end of line.
%#A #' if tcsh is run as a root shell, a >' if not.
%{..%}Include string as a literal escape sequence. Note that the enclosed escape sequence, should only be used to change terminal attributes and should not move the cursor location. Also, this cannot be the last character in the prompt string. (Available only if tcsh was compiled to be eight bit clean.)
%?return code of the last command executed just before the prompt.
%RIn prompt3 this is the corrected string; in prompt2 it is the status of the parser.

常用顏色

30 - black
31 - red
32 - green
33 - yellow
34 - blue
35 - magenta
36 - cyan
37 - white

常用效果

0 - normal
1 - bold
2 - normal again
3 - background color
4 - underline the text
5 - blinking

特殊

如果想在prompt中使用\n, 那麼不能單獨給它定義顏色,否則會出現游標錯亂。

例子

set prompt="\n%{\033[0;32m%}%n @ %m:%{\033[0;33m%}%~%{\033[1;30m%} [%P]\n%{\033[0;35m%}#%{\033[0m%} " '

效果如下
real

引用:
ANSI-Color WIki
TCSH Manual

相關文章