Code Monkey home page Code Monkey logo

cw's Introduction

cw

(c) 2013-2024 Reuben Thomas [email protected]
(c) 2004-2010 v9/fakehalo [email protected]

https://github.com/rrthomas/cw/

cw colors the output of command-line programs. It is designed to be transparent: when you type du, df, ping, etc. in your shell the output is automatically colored according to a definition script, but when the commands are used non-interactively (e.g. in another script) their output is not colored, so as not to confuse programs that process their output. Text patterns are defined as lexical classes, and assigned colors. The arguments to the original program and OS under which it is running can be tested. cw includes over 50 definition scripts, and it’s easy to write more.

The colors to use are defined by definition scripts, which have the same name as the program they color.

cw should not cause any security problems, but since wrapping processes and changing their output can result in subtle bugs, it is not recommended for use by privileged users such as root.

cw is distributed under the GNU General Public License version 3, or, at your option, any later version. This is free software: you are free to change and redistribute it. There is NO warranty, to the extent permitted by law.

Please report bugs in the issue tracker (see above for the address).

Installation

Install cw using LuaRocks from https://www.luarocks.org/ :

luarocks install cw

To use cw, you need to prepend its definition directory to the PATH environment variable. You should do this in your shell’s interactive startup file (you don’t want cw being run by background commands). For example, for bash and other Bourne-compatible shells, add the following line to your ~/.bashrc:

if [ -n "$PS1" ]; then PATH=`cw-definitions-path`:$PATH; export PATH; fi

The test of PS1 ensures that the shell is really interactive.

Use

Start a new shell and try some commands, which should now be colored!

The cw command itself has --version and --help, and explains that it’s not meant to be run directly.

Alternatives to cw

cw doesn’t try to replace some dedicated coloring wrappers and programs, such as:

ccal: http://unicorn.us.com/cal.html
colorgcc: https://schlueters.de/colorgcc.html
colordiff: https://www.colordiff.org/
colormake: https://github.com/pagekite/Colormake
freecolor: https://www.rkeene.org/oss/freecolor/

There are also other programs like cw that you may prefer:

Crayonizer: https://sites.google.com/site/columscode/home/crayonizer
acoc: http://www.caliban.org/ruby/acoc.shtml

Definition scripts

Definition scripts are written in the Lua scripting language but you don’t need to know Lua to make a simple script.

The instructions that follow assume you have checked the cw project out from GitHub (see top for URL).

To make your own, try first modifying an existing script; remember to make your script executable, and please consider submitting it to GitHub (see top) for inclusion in future releases.

Definition scripts, which have the same name as the program they color, are in the ‘script’ subdirectory.

cw provides scripts with a standard Lua environment, plus luaposix (useful for tasks such as checking the system type and setting environment variables) and the following extra features:

  • match(class, pattern) This function adds a rule to color text matching pattern as class class. Matches are applied in order of definition. See “Classes and colors” below for an explanation of classes, and the Lua manual for the syntax of patterns.
  • arg A list of command-line arguments to the program being colored. For convenience, each argument is also a key in the table, so it is easy to test whether a certain argument has been given, in any position, with code such as if arg["--help"] then…

Classes and colors

The default classes are: highlight, lowlight, warning, error, default. The names suggest the sort of text patterns you may want to map to them. The mapping of classes to colors can be changed with the CW_COLORS environment variable (see “Environment variables”, below).

The available colors are: black, red, green, yellow, blue, magenta, cyan, white. A color may be prefixed bright to denote a brighter color, e.g. bright red (note the space).

Environment variables

  • NOCOLOR Disables color wrapping when set. Note that cw disables coloring if standard output is not connected to a terminal, or if the terminal type TERM is dumb.
  • CW_INVERT Inverts the built-in color scheme when set, to make cw more legible on terminals with white backgrounds (cw is set up for a black background by default).
  • CW_COLORS Defines a mapping from classes to colors. The format is CW_COLORS=class=color[:class=color...].

Recommendations for definition scripts

There are two obvious reasons to color commands: first, for aesthetic reasons, and secondly, to make the output more legible. For the first kind, use your imagination! The colorers provided with cw are of the second kind, and the following suggestions aim to help improve presentation:

  • Colors should be used to pick out key information; it is not necessary to color everything.
  • Many commands’ output is already clear, and needs little or no coloring. For example, color just the column headings of tables.
  • When each line is in the same format, but unaligned, color can be helpful to pick out particular fields; for example, the coloring of variable names in the env colorer.
  • Since cw is not recommended for use by privileged users, don’t bother point writing wrappers for commands that typically require administrator privileges to use.
  • Help output (e.g. the output of --help) should not be colored.
  • Use of the default class only makes sense to override the coloring of text that has already been colored by another pattern.

cw's People

Contributors

blueyed avatar rrthomas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

blueyed solbu

cw's Issues

Make automake a dependency

I ran

brew install lua
...
luarocks install cw
...
LUA=/usr/local/bin/lua autoreconf -i && ./configure --prefix=/usr/local/lib/luarocks/rocks-5.2/cw/2.0.4-1 --libdir=/usr/local/lib/luarocks/rocks-5.2/cw/2.0.4-1/lib --datadir=/usr/local/lib/luarocks/rocks-5.2/cw/2.0.4-1/lua && make clean && make
sh: autoreconf: command not found

Error: Build error: Failed building.

so then i ran

brew install automake
...
luarocks install cw

and it worked just dandy

License upgraded to GPLv3+, but the LICENSE file is still GPLv2

In 2013 you upgraded the license from GPLv2+ to GPLv3+.
But you forgot to upgrade the LICENSE file that contains the actuall license text, which is still GPLv2.
You might want to fix that,

I package cw for Mageia, and just now discovered that you have continued the development.
Thank you for that. :-)

Make cw faster

Can we output more than a line at a time to get closer to normal speed?

/usr/bin/lua: /usr/bin/cw:39: module 'std' not found:

Hi, Thomas.

I am the Mageia cw maintainer, and I can't get the new lua based version to work. I stumbled across your fork a few days ago, and I'm attempting to upgrade our old 1.0.16 version.
We have a policy that all software must be compiled from the source code, so No «rock» installation.

Apparently it can't find the std module, but seaches for the module yields absolutely no relevant results of any kind, and there is no list of dependencies in the documentaiton.

When I run «cw --help», I get this output:

$ cw --help
/usr/bin/lua: /usr/bin/cw:39: module 'std' not found:
        no field package.preload['std']
        no file '/usr/share/lua/5.2/std.lua'
        no file '/usr/share/lua/5.2/std/init.lua'
        no file '/usr/lib/lua/5.2/std.lua'
        no file '/usr/lib/lua/5.2/std/init.lua'
        no file '/usr/lib64/lua/5.2/std.lua'
        no file '/usr/lib64/lua/5.2/std/init.lua'
        no file '/usr/lib/lua/5.2/std.so'
        no file '/usr/lib/lua/5.2/loadall.so'
        no file '/usr/lib64/lua/5.2/std.so'
        no file '/usr/lib64/lua/5.2/loadall.so'
        no file './std.so'
stack traceback:
        [C]: in function 'require'
        /usr/bin/cw:39: in main chunk
        [C]: in ?

Fails to install cw-definitions-path

Trying to install on Debian sid, I get the following:

/usr/bin/lua cw-definitions-path --version >/dev/null || ( cat cw-definitions-path && rm cw-definitions-path )
/usr/bin/lua: cw-definitions-path:27: module 'std' not found:
no field package.preload['std']
no file '/usr/local/share/lua/5.2/std.lua'
no file '/usr/local/share/lua/5.2/std/init.lua'
no file '/usr/local/lib/lua/5.2/std.lua'
no file '/usr/local/lib/lua/5.2/std/init.lua'
no file '/usr/share/lua/5.2/std.lua'
no file '/usr/share/lua/5.2/std/init.lua'
no file './std.lua'
no file '/usr/local/lib/lua/5.2/std.so'
no file '/usr/lib/x86_64-linux-gnu/lua/5.2/std.so'
no file '/usr/lib/lua/5.2/std.so'
no file '/usr/local/lib/lua/5.2/loadall.so'
no file './std.so'
stack traceback:
[C]: in function 'require'
cw-definitions-path:27: in main chunk
[C]: in ?

Unable to get it work under ubuntu 15.04

I followed the procedure in the README, but I can't find how to fix it.
I use this in my path : /usr/local/lib/luarocks/rocks/cw/2.0.1-2/libexec/cw

Here is the output of df
/usr/bin/lua5.1: /usr/local/lib/luarocks/rocks/cw/2.0.1-2/bin/cw:141: attempt to call field 'pipeline_iterator' (a nil value)
stack traceback:
/usr/local/lib/luarocks/rocks/cw/2.0.1-2/bin/cw:141: in main chunk
[C]: ?

Thanks

strace-color binary does not color

I saw that a binary strace-color was installed at exact the same time as i installed cw, so i assume it belongs to this package.

I tried to put this in my ~/.zshrc

if [ -n "$PS1" ]; then PATH=$(~/.luarocks/bin/cw-definitions-path):$PATH; export PATH; fi

I also tried setting PATH per process

PATH=$(~/.luarocks/bin/cw-definitions-path):$PATH; ~/.luarocks/bin/strace-color ~/php/php-src_7.4.4/install/bin/php -S localhost:8002  --php-ini php_debug.ini

In both situations i can do echo $PATH and see that the first path points to /home/flip111/.luarocks/lib/luarocks/rocks/cw/2.0.6-1/libexec/cw. In this directory i find the cw definition files.

I don't see any color output of strace, did i do something wrong ?

Problem with installing

ubuntu 19.10

»   luarocks install cw --local
Installing https://luarocks.org/cw-2.0.6-1.src.rock
Missing dependencies for cw 2.0.6-1:
   stdlib ~> 41.2 (not installed)
   luaposix >= 33.3.1, < 33.4 (not installed)
   ansicolors >= 1.0.2 (not installed)
   ldoc (not installed)

cw 2.0.6-1 depends on stdlib ~> 41.2 (not installed)
Installing https://luarocks.org/stdlib-41.2.2-1.src.rock
No existing manifest. Attempting to rebuild...
stdlib 41.2.2-1 is now installed in /home/flip111/.luarocks (license: MIT/X11)

cw 2.0.6-1 depends on luaposix >= 33.3.1, < 33.4 (not installed)
Installing https://luarocks.org/luaposix-33.3.1-1.src.rock
./configure LUA='/usr/bin/lua5.3' LUA_INCLUDE='-I/usr/include/lua5.3' --prefix='/home/flip111/.luarocks/lib/luarocks/rocks/luaposix/33.3.1-1' --libdir='/home/flip111/.luarocks/lib/luarocks/rocks/luaposix/33.3.1-1/lib' --datadir='/home/flip111/.luarocks/lib/luarocks/rocks/luaposix/33.3.1-1/lua' --datarootdir='/home/flip111/.luarocks/lib/luarocks/rocks/luaposix/33.3.1-1' && make clean all
## --------------------------- ##
## Configuring luaposix 33.3.1 ##
## --------------------------- ##

checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether make supports nested variables... (cached) yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for Minix Amsterdam compiler... no
checking for ar... ar
checking for ranlib... ranlib
checking for egrep... (cached) /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking minix/config.h usability... no
checking minix/config.h presence... no
checking for minix/config.h... no
checking whether it is safe to define __EXTENSIONS__... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking the archiver (ar) interface... ar
checking how to print strings... printf
checking for a sed that does not truncate output... (cached) /usr/bin/sed
checking for fgrep... /usr/bin/grep -F
checking for ld used by gcc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 1572864
checking how to convert x86_64-unknown-linux-gnu file names to x86_64-unknown-linux-gnu format... func_convert_file_noop
checking how to convert x86_64-unknown-linux-gnu file names to toolchain format... func_convert_file_noop
checking for /usr/bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for dlltool... no
checking how to associate runtime and link libraries... printf %s\n
checking for archiver @FILE support... @
checking for strip... strip
checking for ranlib... (cached) ranlib
checking command to parse /usr/bin/nm -B output from gcc object... ok
checking for sysroot... no
checking for a working dd... /usr/bin/dd
checking how to truncate binary pipes... /usr/bin/dd bs=4096 count=1
checking for mt... mt
checking if mt is a manifest tool... no
checking for dlfcn.h... yes
checking for objdir... .libs
checking if gcc supports -fno-rtti -fno-exceptions... no
checking for gcc option to produce PIC... -fPIC -DPIC
checking if gcc PIC flag -fPIC -DPIC works... yes
checking if gcc static flag -static works... yes
checking if gcc supports -c -o file.o... yes
checking if gcc supports -c -o file.o... (cached) yes
checking whether the gcc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no
checking for ANSI C header files... (cached) yes
checking crypt.h usability... yes
checking crypt.h presence... yes
checking for crypt.h... yes
checking for strings.h... (cached) yes
checking sys/statvfs.h usability... yes
checking sys/statvfs.h presence... yes
checking for sys/statvfs.h... yes
checking for net/if.h... yes
checking for linux/netlink.h... yes
checking sys/msg.h usability... yes
checking sys/msg.h presence... yes
checking for sys/msg.h... yes
checking for msgrcv... yes
checking for msgsnd... yes
checking whether fdatasync is declared... yes
checking for library containing crypt... -lcrypt
checking for library containing socket... none required
checking for clock_gettime in -lrt... yes
checking for NcursesW wide-character library... yes
checking for working ncursesw/curses.h... yes
checking for working ncursesw.h... no
checking for working ncurses.h... yes
checking for resizeterm... yes
checking for strlcpy... no
checking for sched_getsheduler... no
checking for sched_setscheduler... yes
checking for statvfs... yes
checking for gethostid... yes
checking for posix_fadvise... yes
checking if /usr/bin/lua5.3 is a Lua interpreter... yes
checking whether /usr/bin/lua5.3 version >= 5.1, < 5.4... yes
checking for /usr/bin/lua5.3 version... 5.3
checking for /usr/bin/lua5.3 platform... unknown
checking for /usr/bin/lua5.3 script directory... ${prefix}/share/lua/5.3
checking for /usr/bin/lua5.3 module directory... ${exec_prefix}/lib/lua/5.3
checking if LUA_VERSION is defined... yes
checking lua.h usability... no
checking lua.h presence... no
checking for lua.h... no
checking lualib.h usability... no
checking lualib.h presence... no
checking for lualib.h... no
checking lauxlib.h usability... no
checking lauxlib.h presence... no
checking for lauxlib.h... no
checking luaconf.h usability... no
checking luaconf.h presence... no
checking for luaconf.h... no
configure: error: cannot find headers for specified LUA_INCLUDE

Error: Failed installing dependency: https://luarocks.org/luaposix-33.3.1-1.src.rock - Build error: Failed building.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.