Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add color to ls output on OpenBSD when colorls is installed #8035

Merged
merged 6 commits into from Jun 1, 2021
Merged

Add color to ls output on OpenBSD when colorls is installed #8035

merged 6 commits into from Jun 1, 2021

Conversation

bonds
Copy link
Contributor

@bonds bonds commented May 30, 2021

Description

Adds color to ls output on OpenBSD when colorls is installed.

Fixes issue #

TODOs:

  • Changes to fish usage are reflected in user documentation/manpages.
  • Tests have been added for regressions fixed
  • User-visible changes noted in CHANGELOG.rst

Copy link
Member

@faho faho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never use which. which is bad.

share/functions/ls.fish Outdated Show resolved Hide resolved
@faho faho added this to the fish 3.3.0 milestone May 30, 2021
@bonds
Copy link
Contributor Author

bonds commented May 30, 2021

Alright, updated to use command -sq instead of which, and determine whether to use colorls every time ls is run instead of caching the answer.

@IlanCosman
Copy link
Contributor

Is "colorls" here referring to https://github.com/athityakumar/colorls? I believe that is essentially obsolete when things like exa or lsd exist as much faster alternatives. Or does openbsd have some other program called colorls?

@bonds
Copy link
Contributor Author

bonds commented May 31, 2021

colorls is, I think, an OpenBSD specific package. Here's the package description:

Information for inst:colorls-6.5p0

Comment:
ls(1) that can use color to display file attributes

Description:
This is a simple hack, taken from FreeBSD, to OpenBSD's ls(1) to
use ANSI sequences to display file attributes in color.  There is
a -G flag (somewhat similar to the -F flag).  Take a look at the
man page for details.  The program is called "colorls", so you may
want to use an alias such as ls=/usr/local/bin/colorls.

Note that you need a color-capable terminal to enable colorls.  This
means you should set your TERM to "wsvt25" on the wscons(4) console
and to "sun-color" when using the Sun console, not "vt220" and
"sun", respectively, which are not color-capable in termcap(5).

Maintainer: Christian Weisgerber <naddy@openbsd.org>

And this is the man page:

COLORLS(1)		    General Commands Manual		    COLORLS(1)

NAME
     colorls  list directory contents

SYNOPSIS
     colorls [-1AaCcdFfGgHhikLlmnopqRrSsTtux] [file ...]

DESCRIPTION
     For each operand that names a file of a type other than directory,
     colorls displays its name as well as any requested, associated
     information.  For each named directory, colorls displays the names of
     files contained within that directory, as well as any requested,
     associated information.

     If no operands are given, the contents of the current directory are
     displayed.	 If more than one operand is given, non-directory operands are
     displayed first; directory and non-directory operands are sorted
     separately and in lexicographical order.  By default, colorls lists one
     entry per line to standard output; the exceptions are to terminals or
     when the -C, -m, or -x options are specified.

     The options are as follows:

     -1	     (The numeric digit one.) Force output to be one entry per line.
	     This is the default when output is not to a terminal.

     -A	     List all entries except for . and ...  Always set for the
	     superuser.

     -a	     Include directory entries whose names begin with a dot (.).

     -C	     Force multi-column output; this is the default when output is to
	     a terminal.

     -c	     Use time file's status was last changed instead of last
	     modification time for sorting (-t) or printing (-g, -l, or -n).

     -d	     Directories are listed as plain files (not searched recursively)
	     and symbolic links in the argument list are not indirected
	     through.

     -F	     Display a slash (/) immediately after each pathname that is a
	     directory, an asterisk (*) after each that is executable, an at
	     sign (@) after each symbolic link, an equal sign (=) after
	     each socket, and a vertical bar (|) after each that is a FIFO.

     -f	     Output is not sorted.  This option implies -a.

     -G	     Enable colorized output.  This option is equivalent to defining
	     CLICOLOR in the environment.  (See below.)

     -g	     List in long format as in -l, except that the owner is not
	     printed.

     -H	     Follow symbolic links specified on the command line.  This is the
	     default behaviour when none of the -d, -F, or -l options are
	     specified.

     -h	     When used with a long format option, use unit suffixes: Byte,
	     Kilobyte, Megabyte, Gigabyte, Terabyte, Petabyte, and Exabyte in
	     order to reduce the number of digits to four or fewer using
	     powers of 2 for sizes (K=1024, M=1048576, etc.).

     -i	     For each file, print its inode number.

     -k	     Modifies the -s option, causing the sizes to be reported in
	     kilobytes.	 Overrides any value specified by the BLOCKSIZE
	     environment variable.

     -L	     If argument is a symbolic link, evaluate the file information and
	     file type to be those of the file referenced by the link, and not
	     the link itself; however, colorls writes the name of the link
	     itself and not the file referenced by the link.

     -l	     (The lowercase letter ell.) List in long format (see below).  A
	     total sum of all file sizes is output on a line before the long
	     listing.  Output is one entry per line.

     -m	     Stream output format; list files across the page, separated by
	     commas.

     -n	     List in long format as in -l, but retain user and group IDs in a
	     numeric format.  The output of -gn and -ng is identical: a long
	     listing with numerical group ID, and no numerical user ID.	 The
	     output of -ln and -nl is identical: a long listing with numerical
	     group and user ID.

     -o	     Include the file flags in a long format (-g, -l, or -n) output.

     -p	     Display a slash (/) immediately after each pathname that is a
	     directory.

     -q	     Force printing of non-graphic characters in file names as the
	     character ?; this is the default when output is to a terminal.

     -R	     Recursively list subdirectories encountered.

     -r	     Reverse the order of the sort to get reverse lexicographical
	     order or the smallest or oldest entries first.

     -S	     Sort by size, largest file first.

     -s	     Display the number of file system blocks actually used by each
	     file, where partial units are rounded up to the next integer
	     value.  Blocks are 512 bytes unless overridden by the -k flag or
	     BLOCKSIZE environment variable.

     -T	     Display complete time information for the file, including month,
	     day, hour, minute, second, and year.  This option has no effect
	     unless one of the long format (-g, -l, or -n) options is also
	     specified.

     -t	     Sort by time modified (most recently modified first) before
	     sorting the operands in lexicographical order.

     -u	     Use file's last access time instead of last modification time for
	     sorting (-t) or printing (-g, -l, or -n).

     -x	     Multi-column output sorted across the page rather than down the
	     page.

     It is not an error to specify more than one of the following mutually
     exclusive options: -1, -C, -g, -l, -m, -n, and -x; and -c, -f, -S, -t,
     and -u.  Where more than one option is specified from the same mutually
     exclusive group, the last option given overrides the others, except that
     -l always overrides -g; and -f always overrides -c, -S, -t, and -u.

   The Long Format
     If the -g, -l, or -n options are given, the following information is
     displayed for each file: mode, number of links, owner (though not for
     -g), group, size in bytes, time of last modification (mmm dd HH:MM),
     and the pathname.	In addition, for each directory whose contents are
     displayed, the first line displayed is the total number of blocks used by
     the files in the directory.  Blocks are 512 bytes unless overridden by
     the -k option or BLOCKSIZE environment variable.

     If the owner or group name is not a known user or group name,
     respectively, or the -n option is given, the numeric ID is displayed.

     If the file is a character special or block special file, the major and
     minor device numbers for the file are displayed in the size field.

     If the -T option is given, the time of last modification is displayed
     using the format mmm dd HH:MM:SS ccyy.

     If the file is a symbolic link, the pathname of the linked-to file is
     preceded by ->.

     The file mode printed under the -g, -l, or -n options consists of the
     entry type, owner permissions, group permissions, and other permissions.
     The entry type character describes the type of file, as follows:

	   -	 regular file
	   b	 block special file
	   c	 character special file
	   d	 directory
	   l	 symbolic link
	   p	 FIFO
	   s	 socket link

     The next three fields are three characters each: owner permissions, group
     permissions, and other permissions.  Each field has three character
     positions:

	   1.	If r, the file is readable; if -, it is not readable.
	   2.	If w, the file is writable; if -, it is not writable.
	   3.	The first of the following that applies:

		      S	    If in the owner permissions, the file is not
			    executable and set-user-ID mode is set.  If in the
			    group permissions, the file is not executable and
			    set-group-ID mode is set.

		      s	    If in the owner permissions, the file is
			    executable and set-user-ID mode is set.  If in the
			    group permissions, the file is executable and set-
			    group-ID mode is set.

		      x	    The file is executable or the directory is
			    searchable.

		      -	    The file is neither readable, writable,
			    executable, nor set-user-ID, nor set-group-ID, nor
			    sticky (see below).

		These next two apply only to the third character in the last
		group (other permissions):

		      T	    The sticky bit is set (mode 1000), but neither
			    executable nor searchable (see chmod(1) or
			    sticky(8)).

		      t	    The sticky bit is set (mode 1000), and is
			    searchable or executable (see chmod(1) or
			    sticky(8)).

     In addition, if the -o option is specified, the file flags (see
     chflags(1)) are displayed as comma-separated strings in front of the file
     size, abbreviated as follows:

	   -	     no flags
	   arch	     archived
	   nodump    do not dump
	   sappnd    system append-only
	   schg	     system immutable
	   uappnd    user append-only
	   uchg	     user immutable

ENVIRONMENT
     BLOCKSIZE	     If the environment variable BLOCKSIZE is set, and the -k
		     option is not specified, the block counts (see -s) will
		     be displayed in units of that size block.

     CLICOLOR	     Use ANSI color sequences to distinguish file types.  See
		     LSCOLORS below.  In addition to the file types mentioned
		     in the -F option some extra attributes (setuid bit set,
		     etc.) are also displayed.	The colorization is dependent
		     on a terminal type with the proper termcap(5)
		     capabilities.  To display the colors on the wscons(4)
		     console, for example, the TERM variable must be set to
		     wsvt25.  Other terminal types may require similar
		     adjustments.  Colorization is silently disabled if the
		     output isn't directed to a terminal unless the
		     CLICOLOR_FORCE variable is defined.

     CLICOLOR_FORCE  Color sequences are normally disabled if the output isn't
		     directed to a terminal.  This can be overridden by
		     setting this flag.	 The TERM variable still needs to
		     reference a color capable terminal however otherwise it
		     is not possible to determine which color sequences to
		     use.

     COLUMNS	     If set to a positive integer, output is formatted to the
		     given width in columns.  Otherwise, colorls defaults to
		     the terminal width, or 80 columns if the output is not a
		     terminal.

     LC_CTYPE	     The character encoding locale(1).	It decides which byte
		     sequences form characters and what their display width
		     is.  If unset or set to "C", "POSIX", or an unsupported
		     value, non-ASCII bytes are replaced by question marks.

     LSCOLORS	     The value of this variable describes what color to use
		     for which attribute when colors are enabled with
		     CLICOLOR.	This string is a concatenation of pairs of the
		     format fb, where f is the foreground color and b is the
		     background color.

		     The color designators are as follows:

			   a	 black
			   b	 red
			   c	 green
			   d	 brown
			   e	 blue
			   f	 magenta
			   g	 cyan
			   h	 light grey
			   A	 bold black, usually shows up as dark grey
			   B	 bold red
			   C	 bold green
			   D	 bold brown, usually shows up as yellow
			   E	 bold blue
			   F	 bold magenta
			   G	 bold cyan
			   H	 bold light grey; looks like bright white
			   x	 default foreground or background

		     Note that the above are standard ANSI colors.  The actual
		     display may differ depending on the color capabilities of
		     the terminal in use.

		     The order of the attributes are as follows:

			   1.	directory
			   2.	symbolic link
			   3.	socket
			   4.	pipe
			   5.	executable
			   6.	block special
			   7.	character special
			   8.	executable with setuid bit set
			   9.	executable with setgid bit set
			   10.	directory writable to others, with sticky bit
			   11.	directory writable to others, without sticky
				bit

		     The default is "exfxcxdxbxegedabagacad", i.e., blue
		     foreground and default background for regular
		     directories, black foreground and red background for
		     setuid executables, etc.

     TERM	     The CLICOLOR functionality depends on a terminal type
		     with color capabilities.

     TZ		     The time zone to use when displaying dates.  See
		     environ(7) for more information.

EXIT STATUS
     The colorls utility exits0 on success, and>0 if an error occurs.

EXAMPLES
     List the contents of the current working directory in long format:

	   $ colorls -l

     In addition to listing the contents of the current working directory in
     long format, show inode numbers, file flags (see chflags(1)), and suffix
     each filename with a symbol representing its file type:

	   $ colorls -lioF

     List the files in /var/log, sorting the output such that the most
     recently modified entries are printed first:

	   $ colorls -lt /var/log

SEE ALSO
     chflags(1), chmod(1), termcap(5), symlink(7), sticky(8)

STANDARDS
     The ls(1) utility is compliant with the IEEE Std 1003.1-2008 (POSIX.1)
     specification, except behaviour for the -o flag differs.

     The flags [-GhT], as well as the BLOCKSIZE environment variable and
     colorls itself, are extensions to that specification.

     The flags [-go] are marked by IEEE Std 1003.1-2008 (POSIX.1) as being
     an X/Open System Interfaces option.

     Historically, the -g flag was used to specify that the group field be
     included in long listings.	 The group field is now automatically included
     in the long listing for files and the meaning of the -g flag has been
     changed in order to be compatible with the IEEE Std 1003.1-2008
     (POSIX.1) specification.

HISTORY
     An ls utility appeared in Version1 AT&T UNIX.

OpenBSD 6.9		       October 24, 2016			   OpenBSD 6.9

share/functions/ls.fish Outdated Show resolved Hide resolved
@faho faho merged commit 3ddb5a2 into fish-shell:master Jun 1, 2021
@faho
Copy link
Member

faho commented Jun 1, 2021

Merged, thanks!

@bonds
Copy link
Contributor Author

bonds commented Jun 1, 2021

You beat me to one more fix I was working on: https://github.com/bonds/fish-shell/commit/fd228351a9254ff9d97232dca42d7bcfb870330a

Should I open a new PR for that?

@faho
Copy link
Member

faho commented Jun 1, 2021

Should I open a new PR for that?

Nah, cherry-picked as ad38730.

@kidonng
Copy link
Contributor

kidonng commented Jun 2, 2021

@IlanCosman I'm thinking about the same thing. What if people has https://github.com/athityakumar/colorls in path? I tried installing colorls and applying this patch, then I got:

$ ls
colorls: invalid option: -F
See 'colorls --help'.

@bonds
Copy link
Contributor Author

bonds commented Jun 3, 2021

Hmm. Well, in a previous version of this PR I tested the chosen ls command with different parameters to check that they were at least supported...that would avoid this problem. I didn't keep that version because @faho didn't like the global function I wrote to reuse my check for colorls's existence, but perhaps a version that does the same thing without a global function would be good.

@faho
Copy link
Member

faho commented Jun 3, 2021

Fixed in a633889, we now ignore a "colorls" if it doesn't understand "-F". Which ignores the ruby colorls, but that seems to be closer in spirit to exa than normal ls.

@bonds
Copy link
Contributor Author

bonds commented Jun 3, 2021

Thanks @faho !

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants