구문 강조 / 컬러링 고양이
의 출력 색상 화하는 방법이있다 cat
방법은, grep
수행은.
의 경우 grep
대부분의 콘솔에서 검색된 키워드를 강조 표시하는 컬러 출력을 표시합니다. 그렇지 않으면, 당신은 grep --color
당신의 개인적인 선택에 따라 프로그램의 출력을 색칠하는 일반적인 방법이 있습니까?
내가 이해 한 바에 따르면, 프로그램 자체는 색상에 대한 책임이 없습니다. 껍질입니다.
나는 FreeBSD 5.2.1에서 기본 쉘을 사용하고 있는데, 그것은 시대 이후로 색을 본 적이없는 것처럼 보입니다.
cat
구문 강조 표시는 단순히 범위를 벗어납니다. cat
그런 의미가 아닙니다. 일부 파일의 전체 내용을 어떤 방식으로 (전체 파일에 대해 동일한 색상으로) 채색하려면 터미널 이스케이프 시퀀스를 사용하여 색상을 제어 할 수 있습니다.
파일 유형에 따라 색상을 선택하는 샘플 스크립트는 다음과 같습니다 ( cat
직접 호출하는 대신 이와 같은 것을 사용할 수 있음).
#!/bin/bash
fileType="$(file "$1" | grep -o 'text')"
if [ "$fileType" == 'text' ]; then
echo -en "\033[1m"
else
echo -en "\033[31m"
fi
cat $1
echo -en "\033[0m"
위의 (이스케이프 시퀀스를 지원하는 터미널에서) 텍스트 파일을 'bold'로 인쇄하고 이진 파일을 빨간색으로 인쇄합니다. 이진 파일을 인쇄 하는 strings
대신 사용할 수 있으며 cat
필요에 맞게 논리를 향상시킬 수 있습니다.
pygmentize
파이썬 패키지에서 추천 합니다 python-pygments
. ccat
ccrypt 패키지에서 사용하지 않는 한 다음과 같은 편리한 별칭을 정의 할 수 있습니다 .
alias ccat='pygmentize -g'
그리고 줄 번호를 원한다면 :
alias ccat='pygmentize -g -O style=colorful,linenos=1'
옵션 :
피그 니 제이션 이 좋습니다. 별칭이 있습니다.
alias c='pygmentize -g'
그러나 하이라이트 는 널리 사용 가능한 또 다른 대안입니다.
alias cats='highlight -O ansi --force'
설치:
다음을 사용 pygments
하여 설치해야 할 수도 있습니다 .
sudo pip install pygments
sudo easy_install Pygments #for Mac user
과에 대한 highlight
패키지를 배포하는 모든 경우에 쉽게 사용할 수있는
sudo apt-get install highlight
sudo yum install highlight
- 비트 버킷 리포 : https://bitbucket.org/birkenfeld/pygments-main
- GitHub 미러 : https://github.com/sglyon/pygments
실제로 :
하이라이트를 잘 비교하기 위해 아래에 샷을 첨부하고 있습니다.
다음은 pygmentize
실제로 적용되는 것입니다.
그리고 이것은 highlight
:
2018 년 4 월 말부터 :
Bat- 구문 강조 표시 및 Git 통합이 포함 된 cat (1) 클론.
이 프로젝트는 Rust로 작성된 색상 및 사용자 정의를 지원하는 고양이 복제품입니다. 여러 테마를 사용한 구문 강조뿐만 아니라 Git 통합도 제공합니다. 설명서에 설명 된대로 :
박쥐는 다음 목표를 달성하려고합니다 .
- Provide beautiful, advanced syntax highlighting
- Integrate with Git to show file modifications
- Be a drop-in replacement for (POSIX) cat
- Offer a user-friendly command-line interface
It is, needless to say, much faster than pygmentize and does not choke when confronted with large files.
Source code and binary releases + installation instructions can be found in the Github repository, as well as a comparison to alternative programs.
The tool you're looking for is probably supercat (here's a quick introduction published by Linux Journal).
I realize that this answer is late, and that it doesn't fully meet the OP requirements. So I'm adding it just for reference (it could be useful for other people looking for how to colorize text file output).
vimcat
is single-file (shell script) and works good:
http://www.vim.org/scripts/script.php?script_id=4325
Last update is from December 2013. Hint: you can force file type recognition by vimcat -c "set ft=<type>"
.
There is a colorized version of cat - ccat. Get it from https://github.com/jingweno/ccat/.
Installation on Linux/Windows/macOS
It is a single standalone executable so to install it you can unpack the binary version for your OS from https://github.com/jingweno/ccat/releases and copy the ccat binary for example to /usr/local/bin
.
If you want to avoid binaries, or if there is no binary for your platform (e.g. raspberry pi, etc), then you can alternately compile from source given that you have a working go
development environment (apt install golang
on debian-based linuxes or brew install golang
on mac):
go get -u github.com/jingweno/ccat
The
ccat
binary will be created under your$GOPATH/bin
.
Installing on Mac via homebrew
brew install ccat
Alias to cat
To replace regular cat
with ccat
add in ~/.bashrc
:
alias cat="ccat $*"
alias cat0="/bin/cat $*" # for cases when you need plain `cat`
ccat
is implemented in Go, so it is a native binary which runs much faster than Python-based solutions, such as pygments, the module behind pygmentize
; I didn't see any noticeable speed difference between cat
and ccat
.
source-highlight
Maybe it's possible to find interesting source-highlight
released under GNU: a package different from highlight
.
Excerpt from apt-cache show source-highlight
:
Description-en: convert source code to syntax highlighted document.
This program, given a source file, produces a document with syntax highlighting.
It supports syntax highlighting for over 100 file formats ...
For output, the following formats are supported: HTML, XHTML, LaTeX, Texinfo, ANSI color escape sequences, and DocBook
I did some alias (Cat and PCat, see below) and this is their output
You can install on Debian based with
sudo apt-get install source-highlight
and add it as alias e.g. in your .bash_aliases
with something like the line below.
alias Cat='source-highlight --out-format=esc -o STDOUT -i'
Cat myfile.c # or myfile.xml ...
Or you can do a similar alias (without the -i
at the end to have the possibility to pipe in)
alias PCat='source-highlight --out-format=esc -o STDOUT '
tail myfile.sh | PCat # Note the absence of the `-i`
Among the options that it's possible to read from man source-highlight
the -s
underlines that is possible to select, or force, the highlighting by command line or to leave to the program this duty:
-s, --src-lang=STRING source language (use --lang-list to get the complete list). If not specified, the source language will be guessed from the file extension.
--lang-list list all the supported language and associated language definition file
The best way and the easiest way to do it if you have vim in your machine is to use vimcat
which comes with vimpager
program.
- Install vimpage with
git clone git://github.com/rkitover/vimpager cd vimpager sudo make install
Run vimcat:
vimcat index.html
From what I understand, the binary itself is not responsible for the colors. It is the shell.
That't not correct. Terminal just interprets the color codes that is output to the terminal. Depending on its capability it can ignore certain formatting/coloring codes.
From man page it does not seem cat supports coloring its output. Even if it were to support coloring like grep what should it color in the text file? Syntax highlighting required knowledge of underlying language which is not in the scope of simple utility like cat.
You can try more powerful editors like vim,emacs, gedit etc on unix platform if seeing the code highlighted is your goal.
On OSX simply do brew install ccat
.
https://github.com/jingweno/ccat. Like cat but displays content with syntax highlighting. Built in Go.
bat precisely does that and can be aliased to cat alias cat='bat'
Old question, just answering for the record to provide the solution I ended up using. Perhaps this is a bit hacky (probably not the original intent of the parameter), but:
alias cgrep='grep -C 9000'
cat whatever | cgrep 'snozzberries'
..grep -C N
will provide N lines of context above and below the found item. If it's larger than the input, the whole input is included. In this case, we just make sure it's larger than any typical terminal output we'll want to look at manually, which is generally what you're looking to do if highlighting.
EDIT : However, this solution suggested below by Beni Cherniavsky-Paskin is superior -- it matches (and highlights) either the word you're looking for or the beginning of the line (not highlightable). The net result is exactly what you want.
cat whatever | egrep 'snozzberries|$'
That's the new best solution I've seen for that problem, thanks Beni.
In this question https://superuser.com/questions/602294/is-there-colorizer-utility-that-can-take-command-output-and-colorize-it-accordin grcat
/grc
tool was recommended as alternative to supercat
.
Man of grc and of grcat
; it is part of grc package (sources):
grc - frontend for generic colouriser grcat(1)
grcat - read from standard input, colourise it and write to standard output
나는를 사용하여 채색을 수행하는 작은 스크립트를 작성했습니다 pygmentize
.
colorize_via_pygmentize() {
if [ ! -x "$(which pygmentize)" ]; then
echo "package \'Pygments\' is not installed!"
return -1
fi
if [ $# -eq 0 ]; then
pygmentize -g $@
fi
for FNAME in $@
do
filename=$(basename "$FNAME")
lexer=`pygmentize -N \"$filename\"`
if [ "Z$lexer" != "Ztext" ]; then
pygmentize -l $lexer "$FNAME"
else
pygmentize -g "$FNAME"
fi
done
}
그런 다음 스크립트의 별칭을 만드십시오. alias cat=colorize_via_pygmentize
. 또한 이것을 ~ / .bashrc 에 저장하는 것을 잊지 마십시오 .
하나의 라이너로 cat
출력을 주어진 색상 으로 설정 하려면 추가 할 수 있습니다
alias cat="echo -en 'code' | cat - "
너의 ~에게 ~/.$(basename $SHELL)rc
다음은 색상 코드가 포함 된 요점입니다. https://gist.github.com/chrisopedia/8754917
나는 '\e[1;93m'
노란색으로 강한을 좋아한다 . 다음과 같이 보입니다 :
vim 과이 vimrc 파일을 사용 하십시오 .
짧막 한 농담:
vim -c '1' -c 'set cmdheight=1' -c 'set readonly' -c 'set nomodifiable' -c 'syntax enable' -c 'set guioptions=aiMr' -c 'nmap q :q!<CR>' -c 'nmap <Up> <C-Y>' -c 'nmap <Down> <C-E>' -c 'nmap ^V <C-F><C-G>' "$@"
nano -v
대안이 될 수도 있습니다.
참고 URL : https://stackoverflow.com/questions/7851134/syntax-highlighting-colorizing-cat
'IT story' 카테고리의 다른 글
Vim에서 파이썬 코드 블록을 주석 처리하는 방법 (0) | 2020.05.18 |
---|---|
AngularJS에서 모델 변경 사항을 볼 때 초기로드를 어떻게 무시합니까? (0) | 2020.05.18 |
태블릿 장치에서 탭이 전체 너비를 차지하지 않음 [android.support.design.widget.TabLayout 사용] (0) | 2020.05.18 |
파이썬에서 변수 지우기 (0) | 2020.05.18 |
ComboBox : 항목에 텍스트 및 값 추가 (바인딩 소스 없음) (0) | 2020.05.18 |