ansi转tex。 ansi2color.pl

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

ansi转tex。 ansi2color.pl

#1

帖子 eexpress » 2011-12-26 23:59

把终端的各种颜色输出,转tex。
比如

代码: 全选

● l ~/bin|head|ansi2color.pl >xxx.tex
然后

代码: 全选

\input{xxx.tex}
附件
效果图
效果图
ansi2color.pl.tar.gz
(538 Bytes) 已下载 47 次
● 鸣学
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: ansi转tex。 ansi2color.pl

#2

帖子 eexpress » 2011-12-27 11:29

改成支持 --html --tex 和 --none
其中 --none 是人可读的颜色名
--html --tex 都是管道过滤到文件。

原始图
term
term

代码: 全选

● l|ansi2color.pl -h>xxx.html
html
html

代码: 全选

● l|ansi2color.pl -t>xxx.tex
tex
tex
screenshot19.png (10.07 KiB) 查看 1154 次
附件
ansi2color.tar
(10 KiB) 已下载 52 次
● 鸣学
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: ansi转tex。 ansi2color.pl

#3

帖子 eexpress » 2011-12-27 11:34

Many programs that generate colored output detect if they're writing to a TTY, and switch off colors if they aren't. This is because color codes are annoying when you only want to capture the text, so they try to "do the right thing" automatically.

The simplest way to capture color output from a program like that is to tell it to write color even though it's not connected to a TTY. You'll have to read the program's documentation to find out if it has that option. (e.g., grep has the --color=always option.)

You could also use the expect script unbuffer to create a pseudo-tty like this:
echo barney | unbuffer grep barney | sed -n 1,$\ p
● 鸣学
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: ansi转tex。 ansi2color.pl

#4

帖子 eexpress » 2011-12-27 12:15

● git df
diff --git a/ansi2color.pl b/ansi2color.pl
index e3cb441..60d184a 100755
--- a/ansi2color.pl
+++ b/ansi2color.pl
@@ -8,12 +8,15 @@ my @c=qw /black red green yellow blue magenta cyan white/;
my @l;

while(<STDIN>){
+s/\e\[K//g; s/\e\[m/\e\[0m/g;
if($html){
s/\ /&nbsp;/g;
s/\e\[(.*?)m/get_html_color($1)/eg;
s/$/<br>/g;
}elsif($tex){
+ s/\\/\\textbackslash /g;
s/[\#\$\%\&\~\_\{\}]/\\$&/g;
+ s/\^/\\^{}/g;
s/\ /\\hspace{6pt}/g;
s/\e\[(.*?)m/get_tex_color($1)/eg;
s/$/\n/g;

grep的color真捣蛋。
加了3行。
● 鸣学
头像
ChenFengyuan
帖子: 770
注册时间: 2008-03-23 0:39

Re: ansi转tex。 ansi2color.pl

#5

帖子 ChenFengyuan » 2011-12-27 12:32

mark
:em05 :em05 :em05 :em05
头像
roylez
帖子: 1928
注册时间: 2005-10-04 10:59
来自: 上海

Re: ansi转tex。 ansi2color.pl

#6

帖子 roylez » 2011-12-27 12:32

神又拿天书卖萌
弄个dropbox空间来备份文件或者做私人代码服务器
配置:[url]git://github.com/roylez/dotfiles.git[/url]
主页:http://roylez.heroku.com
各种稀奇玩意儿:http://dooloo.info
头像
ChenFengyuan
帖子: 770
注册时间: 2008-03-23 0:39

Re: ansi转tex。 ansi2color.pl

#7

帖子 ChenFengyuan » 2012-01-02 11:31

roylez 写了:神又拿天书卖萌
拜见主席 :em02
回复