Fortran 的彩色终端输出
我的程序将计算状态输出到终端,并包含相当多的信息。如果可能的话,我想对文本的各个部分进行颜色编码。
我已经通过参考此站点上的线程了解了如何在 Bash 和 C++ 中完成此操作。但是,我无法使用其中任何一个在 Fortran(现代)中获得相同的结果。例如,我尝试了这个示例代码,我认为它应该有效:
PROGRAM test
PRINT*, 'A great color is \033[95m pink \033[0m.'
END PROGRAM test
我期望输出为“粉红色是一种很棒的颜色”,其中粉红色是粉红色。相反,我得到“一个很棒的颜色是 \033[95m 粉红色 \033[0m。”我不明白我错过了什么。
如果我将代码中的打印行替换为: CALL EXECUTE_COMMAND_LINE("echo 'A Great color is \033[95m Pink \033[0m.'") 然后我会得到所需的输出。但是我不想继续从我的代码中调用 echo 。有什么办法可以获得彩色输出吗?
谢谢!
My program outputs state of computations to the terminal and includes quite a bit of information. I would like to, if possible, color code parts of the text.
I have seen how it can be done in Bash and C++ by referring to threads on this site. However, I have not been able to use any of that to achieve the same result in Fortran (modern). For example, I tried this sample code, which I thought should work:
PROGRAM test
PRINT*, 'A great color is \033[95m pink \033[0m.'
END PROGRAM test
I would have expected the output to be "A great color is pink" where pink is colored pink. Instead I get "A great color is \033[95m pink \033[0m." I don't understand what I am missing.
If I replace the print line in the code with: CALL EXECUTE_COMMAND_LINE("echo 'A great color is \033[95m pink \033[0m.'") then I get the output as desired. However I wouldn't want to keep calling on echo from my code. Is there any way I can get colored output?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
转义字符表示形式“\033”似乎不适合您。我没有方便检查的 fortran,但您可以尝试通过调用
char
转换函数显式使用该字符而不是 c 样式转义,即通过调用生成实际字符char(27)
并将其构建到输出字符串中的正确位置。The escape character representation as '\033' doesn't appear to be working for you. I don't have fortran handy to check, but you might try explicitly using the character instead of the c-style escaping by calling the
char
conversion function, i.e., make the actual character by callingchar(27)
and build it into your output string in the correct places.这是一个老问题,但我想我应该给出我的答案,以防后来有人(就像我一样)寻找这个问题的答案。
我遇到了和你类似的问题,试图让转义序列起作用。我最终访问了 gfortran 手册页。搜索“escape”引导我找到编译器选项“-fbackslash”。从手册页:
因此,为了让转义序列在 Fortran 中工作,我们需要做的就是使用此选项进行编译。不同的一件事是我们必须使用十六进制数和 x 而不是八进制数。在本例中,我们使用
\x1B
而不是\033
。例如,PRINT *,“\x1B[31mThis text is red.\x1B[0m
”将以红色打印一些文本。我认为这种方法绝对比每次连接一堆单独定义的字符更好我们想使用颜色。
This is kind of an old question, but I figured I'd throw in my answer in case anyone comes along later (as I did) looking for an answer to this.
I had a similar problem as you did, trying to get escape sequences to work. I ended up going to the man page for gfortran. searching for 'escape' lead me to the compiler option '-fbackslash'. From the man page:
So, to get escape sequences to work in Fortran, all we need to do is compile with this option. The one thing that's different is that we have to use hexadecimal numbers along with x instead of octal numbers. In this specific case, instead of
\033
we use\x1B
. For example,PRINT *, "\x1B[31mThis text is red.\x1B[0m
will print some text in red.I think this method is definitely preferable to concatenating a bunch of individually defined characters each time we want to use color.
如果您使用 ifort 进行编译,则需要使用“-assume bscc”进行编译,然后您可以使用的
代码是:
If you are compiling with ifort you need to compile using "-assume bscc", only then you can use
codes are:
我刚刚偶然发现了 foul 模块/库,它似乎完全符合您的要求。我还没有使用过它,但很快就会尝试一下,因为从我的 Fortran 程序中格式化终端输出会非常有用。
I have just stumbled onto the foul module/library which seems to do exactly what you want. I haven't used it yet but will be giving it a go soon as having formatted terminal output from my Fortran programs would be very useful.