andrewcrook / gnu2bsd

Convert GNU LS_COLORS to BSD LSCOLORS for use with BSD ls

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Description

Reads the LS_COLORS environment variable generated by GNU dircolors, converts it to a format suitable for BSD's LSCOLORS variable, and prints the result to stdout.

Motivation

GNU ls supports colors with the --color option. The colors can be customized by setting the LS_COLORS environment varible, typically set by running GNU dircolors. (See GNU man ls, man dircolors, and man dir_colors.)

However, operating systems like macOS and FreeBSD do not use GNU ls; they use BSD ls. On such systems, ls adds colors with the -G option which is customized by the LSCOLORS variable. (See BSD man ls.) More importantly, the format of BSD LSCOLORS is incompatible with GNU LS_COLORS. So, if you have added GNU LS_COLORS to your BSD-based environment, you currently have two choices to take advantage of it:

  1. Create and maintain the BSD LSCOLORS variable independently
  2. Install GNU coreutils on BSD and alias GNU gls as ls

This program adds a third option:

  1. Run gnu2bsd, using GNU LS_COLORS to generate BSD LSCOLORS

This allows you to keep using BSD ls while still storing your color configuration in one place. Note, however, these two disadvantages:

  1. BSD's ls colors are not as flexible as GNU's; many GNU options will be lost in the translation.
  2. If you set LS_COLORS using GNU dircolors instead of setting it directly in the shell, you will still need to install GNU coreutils.

Installation

Copy this repository to a location of your choice. You can add gnu2bsd.py to your PATH or just run it with python /path/to/gnu2bsd.py. Personally, I keep this repository in ~/.local/lib/ with a symlink at ~/.local/bin/gnu2bsd.

Usage

# Ensure LS_COLOR is set
eval "$(gdircolors)" # or ``eval "$(dircolors)`` or ``LS_COLORS=...``

# Set LSCOLORS
LSCOLORS=$(python /path/to/gnu2bsd.py)

You can now run BSD ls -G to get colors. You can additionally set the CLICOLOR variable to get colors without needing the -G option:

export CLICOLOR=1

Notes

This program assumes that GNU dircolors set the LS_COLORS variable using ANSI escape sequences. Because this is what dircolors does by default, and because most terminals use ANSI, this program should work in most cases.

See Also

  • BSD: man ls
  • GNU: man ls, man dir_colors, man dircolors, ls.c source

About

Convert GNU LS_COLORS to BSD LSCOLORS for use with BSD ls

License:MIT License


Languages

Language:Python 100.0%