Giter VIP home page Giter VIP logo

shell-term-background's Introduction

Pypi Installs Latest Version Supported Python Versions

The Python module contained is part of a broader set of scripts

For the shell scripts, if you source this from a shell session it will set and export COLORFGBG to 0;15 for dark backgrounds and 15;0 for light backgrounds which is a convention used by some programs. Since I find this a little arcane, the program also sets and exports LC_DARK_BG to 1 for dark backgrounds and 0 for light. The LC_ (locale) assists in some ssh configurations which often will let environment variables with that prefix get passed along and set to a remote ssh session.

From Python you can call term_background.is_dark_background() which returns a True if we think the background is dark.

The heuristics used is to try to query the background color using an xterm control sequence.

Many, but not all, terminals support this query. So as a fallback we query environment variable COLORFGBG and failing this we use some defaults for some known terminals set from the TERM environment variable. OSX has its own way of querying characteristics so we use that too if you are running on that OS.

When we can get pixel intensities of red, blue, and green values of the background, we can use that determine light and dark based the combined sum: zero values indicate an absense of a particular color and compare that with the values of the foreground.

You can set any of these environment variables to influence the output decision.

Many thanks to Thomas Dickey, Egmont Koblinger, and Gilles, for explanations (and code!) via unix.stackexchange. John Green had the idea to compare the foreground and background colors instead comparing the background against the midway gray color and implemented that change here.

Of course bugs and lacuna in this code are mine.

shell-term-background's People

Contributors

rocky 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

Watchers

 avatar  avatar  avatar  avatar  avatar

shell-term-background's Issues

is_dark_rgb comparison value seems wrong

(Unfortunately I've so far only been able to test this on one terminal)

It looks to me that the comparison value used in the is_dark_rgb function is wrong.

The RGB values returned from my terminal (iTerm2) are all between 0x00 and 0xff and therefore R+B+G is always smaller then (.6 * (0xffff * 3)) leading to the script always reporting dark mode.

In tmux?

This is really useful, any chance you could get it to work in tmux too?

Export LC_DARKMODE

You can automatically pass environment variables from an SSH client to an SSH server, e.g. the DARK_BG variable, to allow a vim on the server to set the correct background. (See https://serverfault.com/questions/482907/setting-a-variable-for-a-given-ssh-host)

However, variables must be explicitly whitelisted in the servers /etc/ssh/sshd_config. By default, some Linux' allow only LC_* (others are even more restrictive).

I suggest to additionally export LC_DARKMODE=[1|0] so this variable can be sent to remote servers without addional configuration of the remote host.

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.