Fortran 的彩色终端输出

发布于 2024-11-15 22:12:04 字数 523 浏览 2 评论 0原文

我的程序将计算状态输出到终端,并包含相当多的信息。如果可能的话,我想对文本的各个部分进行颜色编码。

我已经通过参考此站点上的线程了解了如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丘比特射中我 2024-11-22 22:12:04

转义字符表示形式“\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 calling char(27) and build it into your output string in the correct places.

本王不退位尔等都是臣 2024-11-22 22:12:04

这是一个老问题,但我想我应该给出我的答案,以防后来有人(就像我一样)寻找这个问题的答案。

我遇到了和你类似的问题,试图让转义序列起作用。我最终访问了 gfortran 手册页。搜索“escape”引导我找到编译器选项“-fbackslash”。从手册页:

更改字符串文字中反斜杠的解释
将单个反斜杠字符转换为“C 样式”转义字符。这
以下组合被扩展“\a”、“\b”、“\f”、“\n”、“\r”、
“\t”、“\v”、“\”和“\0”表示 ASCII 字符警报、退格键、
换页、换行、回车、水平制表符、垂直制表符、
分别是反斜杠和 NUL 。此外,“\x”nn、“\u”nnnn 和
“\U”nnnnnnnn(其中每个 n 是一个十六进制数字)被翻译为
与指定代码点对应的 Unicode 字符。全部
\ 前面的字符的其他组合未展开。

因此,为了让转义序列在 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:

Change the interpretation of backslashes in string literals from a
single backslash character to "C-style" escape characters. The
following combinations are expanded "\a", "\b", "\f", "\n", "\r",
"\t", "\v", "\", and "\0" to the ASCII characters alert, backspace,
form feed, newline, carriage return, horizontal tab, vertical tab,
backslash, and NUL , respectively. Additionally, "\x"nn, "\u"nnnn and
"\U"nnnnnnnn (where each n is a hexadecimal digit) are translated into
the Unicode characters corresponding to the specified code points. All
other combinations of a character preceded by \ are unexpanded.

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.

挽心 2024-11-22 22:12:04

如果您使用 ifort 进行编译,则需要使用“-assume bscc”进行编译,然后您可以使用的

PRINT*, 'A great color is \033[95m pink \033[0m.'

代码是:

[90m=dark grey           [30m=black
[91m=peach               [31m=red
[92m=light green         [32m=green
[93m=light yellow        [33m=yellow
[94m=light blue          [34m=blue
[95m=pink                [35m=purple
[96m=light aqua          [36m=aqua
[97m=pearl white

If you are compiling with ifort you need to compile using "-assume bscc", only then you can use

PRINT*, 'A great color is \033[95m pink \033[0m.'

codes are:

[90m=dark grey           [30m=black
[91m=peach               [31m=red
[92m=light green         [32m=green
[93m=light yellow        [33m=yellow
[94m=light blue          [34m=blue
[95m=pink                [35m=purple
[96m=light aqua          [36m=aqua
[97m=pearl white
夏了南城 2024-11-22 22:12:04

我刚刚偶然发现了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文