我正在使用C ++脚本制作自定义bash提示。计划是从.bash_profile的PS1声明中调用脚本,如下所示:
PS1='$(bashprompt.out)'
这部分工作正常。我遇到的问题是将ANSI序列打印到终端上。您可以在下图中看到它,它过早返回到行的开头。
我以如下格式定义颜色:
#define DGRAYORANGE "\033[48;5;202;38;5;243m"
[在bash脚本中,我知道避免此问题的方法是通过使用\[ \]
将颜色表示为非打印颜色(如此处https://www.funtoo.org/Prompt_Magic)所述,但这在C ++中不起作用。
这里是用于生成彩色提示的C ++代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <string>
#include <iostream>
#define ARROW ""
#define DGRAYORANGE "\033[48;5;202;38;5;243m"
#define ORANGEDGRAY "\033[38;5;202;48;5;243m"
#define DGRAYDEFAULT "\033[38;5;243;48;5;0m"
#define WHITEDGRAY "\033[38;5;255;48;5;243m"
#define WHITEORANGE "\033[38;5;255;48;5;202m"
#define RESET "\033[0m"
using namespace std;
void PrintPrompt(string, string, string);
string FormatCWD(char *);
int main() {
int buffersize = 256;
char hostname[buffersize];
char cwd[buffersize];
struct passwd* pw;
gethostname(hostname,sizeof(hostname));
pw = getpwuid(getuid());
getcwd(cwd,sizeof(cwd));
string stringCwd = FormatCWD(cwd);
string stringUsername = pw->pw_name;
string stringHostname = hostname;
PrintPrompt(stringCwd,stringHostname,stringUsername);
}
void PrintPrompt(string cwd, string hostname, string username){
string cwdString = cwd;
cout << WHITEDGRAY << username <<
DGRAYORANGE << ARROW << " " <<
WHITEORANGE << hostname <<
ORANGEDGRAY << ARROW << " " <<
WHITEDGRAY << cwd << DGRAYDEFAULT <<
ARROW << RESET << " " << endl;
}
string FormatCWD(char * cwd) {
string stringCwd = cwd;
int size = stringCwd.length();
int slashCount = 0;
int slashIndex = 0;
for (int i=0; i<size; i++) {
if (stringCwd.at(i) == '/') {
slashIndex = i;
slashCount++;
}
}
string outputCwd = "";
if (slashIndex != 0) {
for (int i=slashIndex+1; i<size; i++) {
outputCwd += stringCwd.at(i);
}
}
return outputCwd;
}
是否可以通过将转义序列标记为非打印(如bash中的格式)来解决此问题?谢谢你们提供的任何帮助。
首先请注意,\[
和\]
由Bash本身而不是由控制台解释,因此必须在PS1
值中明确显示它们,而不是在生成实际提示的程序输出中出现。
现在,您希望对非转义字符进行计数,而不对颜色进行计数。为此,您可以将程序修改为两种模式:
然后您可以使用类似的东西
PS1='$(/tmp/test)\[\r$(/tmp/test c)\]'
这对修改为以下代码的代码有效:
void PrintPrompt(string cwd, string hostname, string username, bool colors){
string cwdString = cwd;
cout << (colors ? WHITEDGRAY : "") << username <<
(colors ? DGRAYORANGE : "") << ARROW << " " <<
(colors ? WHITEORANGE : "") << hostname <<
(colors ? ORANGEDGRAY : "") << ARROW << " " <<
(colors ? WHITEDGRAY : "") << cwd << (colors ? DGRAYDEFAULT : "") <<
ARROW << (colors ? RESET : "") << endl;
}
colors
以例如argc>1
。
[我们使用\r
转义序列转到行的开头,使我们能够用花哨的颜色覆盖已经打印的无色文本,使Bash认为其他文本和转义序列(包括回车)不要占用任何空间。
截屏: