开发者

How to share bash/shell code snippets taken from command prompt with syntax coloring

I'm looking for some way to share code snippets taken di开发者_高级运维rectly 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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜