如何通过语法着色共享从命令提示符获取的 bash/shell 代码片段

发布于 2024-09-16 11:59:38 字数 595 浏览 2 评论 0原文

我正在寻找某种方法来共享直接从命令提示符获取的代码片段。 例如:

plinjasa@pllzpc029 ~
$ ls

plinjasa@pllzpc029 ~
$ mkdir maindir

plinjasa@pllzpc029 ~
$ ls -l
total 0
drwxr-xr-x+ 1 plinjasa Domain Users 0 2010-08-26 15:22 maindir

plinjasa@pllzpc029 ~
$ cd maindir

plinjasa@pllzpc029 ~/maindir
$ touch somefile

plinjasa@pllzpc029 ~/maindir
$ ls -l
total 0
-rw-r--r-- 1 plinjasa Domain Users 0 2010-08-26 15:22 somefile

plinjasa@pllzpc029 ~/maindir
$ 

有一些解决方案可以通过 bash 语法突出显示来显示这一点(如您所见),但我没有看到任何方法来保留提示着色或任何有助于阅读此类片段的方法。

有什么想法吗?

[编辑] 修复了示例,因为它也导致了有关 FTP 着色的一些混乱。这不重要

I'm looking for some way to share code snippets taken directly from command prompt.
For example:

plinjasa@pllzpc029 ~
$ ls

plinjasa@pllzpc029 ~
$ mkdir maindir

plinjasa@pllzpc029 ~
$ ls -l
total 0
drwxr-xr-x+ 1 plinjasa Domain Users 0 2010-08-26 15:22 maindir

plinjasa@pllzpc029 ~
$ cd maindir

plinjasa@pllzpc029 ~/maindir
$ touch somefile

plinjasa@pllzpc029 ~/maindir
$ ls -l
total 0
-rw-r--r-- 1 plinjasa Domain Users 0 2010-08-26 15:22 somefile

plinjasa@pllzpc029 ~/maindir
$ 

There are some solutions to show this with bash syntax highlighting (as you can see) but I don't see any way to preserve prompt coloring or anything which would help to read such snippet.

Any ideas?

[EDIT] Fixed example as it caused some confusion about having FTP coloring too. It's not important

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

铁轨上的流浪者 2024-09-23 11:59:39

您可以使用 脚本 捕获控制台会话。当您退出会话时,将会有一个名为 typescript 的文件,其中包含您在控制台上输入/看到的内容。然后,您可以使用 ANSIFilter。它的输出尚不完美,但可能适合您的需求。

如果您不需要它是可移植的,或者收件人也使用 UNIX/Linux 终端,您也可以只发送 typescript 文件,可以使用 catless 查看该文件-R 在终端中。

You can capture console sessions using script. When you exit the session, there will be a file called typescript containing exactly what you've typed/seen on the console. You can then convert it (including console color codes) to HTML using ANSIFilter. Its output is not yet perfect, but might fit your needs.

If you don't need it to be portable or if the recipient is also using a UNIX/Linux terminal, you can also just send the typescript file, which can be viewn using cat or less -R in the terminal.

羁〃客ぐ 2024-09-23 11:59:39

您可以使用实用程序将 ansi 彩色终端输出转换为 html。其中之一在这里: ansi2html.sh 我发现它非常有用。 jkramer提供了从终端获取包含完整日志的文本文件的解决方案,该文件可以与ansi2html.sh一起使用,因此这里不再重复。

You can use a utility to convert ansi colored terminal output to html. One of them is here: ansi2html.sh and I found it very useful. jkramer provided solution to obtain a text file with the full log from the terminal, which can be used with ansi2html.sh, so I won't repeat it here.

离旧人 2024-09-23 11:59:39

一点也不容易,因为提示符、分隔符等不同。例如,我会在逻辑上分离与不同程序(lftp 和 shell 本身)的交互,并将提示符减少到最小表达式。就像这样:

您打开 lftp 程序并执行任何操作:

$ lftp
lftp> open -u ...
Password...
...
lftp> XXX
lftp> exit

然后您可以测试 shell 提示符:

$ ls -l
total 0
drwx....

这样您就可以将不同的操作分开到不同的程序,并将提示符减少为一个小单词有助于识别正在使用的程序。

Not easy at all, because of the different prompts, separators, etc. I would, for instance, separate logically the interactions with different programs (lftp and the shell itself), and reduce the prompt to the minimum expression. That is something like:

You open the lftp program and do whatever:

$ lftp
lftp> open -u ...
Password...
...
lftp> XXX
lftp> exit

then you can test into your shell prompt:

$ ls -l
total 0
drwx....

This way you separate different actuations to different programs, and reducing the prompt to a small word helps identifying the program being used.

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