Skip to content

Commit

Permalink
Allows to configure bg and fg colors
Browse files Browse the repository at this point in the history
  • Loading branch information
flozz committed Nov 14, 2020
1 parent b6a0b39 commit a888fc1
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
98 changes: 98 additions & 0 deletions nautilus_terminal/color_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"""
This module contains varous helper functions related to color.
"""


import re


NAMED_COLORS = {
"white": (0xFF, 0xFF, 0xFF),
"silver": (0xC0, 0xC0, 0xC0),
"gray": (0x80, 0x80, 0x80),
"black": (0x00, 0x00, 0x00),
"red": (0xFF, 0x00, 0x00),
"maroon": (0x80, 0x00, 0x00),
"yellow": (0xFF, 0xFF, 0x00),
"olive": (0x80, 0x80, 0x00),
"lime": (0x00, 0xFF, 0x00),
"green": (0x00, 0x80, 0x00),
"aqua": (0x00, 0xFF, 0xFF),
"teal": (0x00, 0x80, 0x80),
"blue": (0x00, 0x00, 0xFF),
"navy": (0x00, 0x00, 0x80),
"fuchsia": (0xFF, 0x00, 0xFF),
"purple": (0x80, 0x00, 0x80),
}


def is_color(string):
"""Checks if the given string is a valid color.
:param str string: The string to check.
:rtype: bool
>>> from rivalcfg.color_helpers import is_color
>>> is_color("#FF0000") # hexadecimal colors are supported
True
>>> is_color("#F00") # short format allowed
True
>>> is_color("FF0000") # "#" is optionnal
True
>>> is_color("#ff0000") # case insensitive
True
>>> is_color("#FF00") # not a valid color
False
>>> is_color("red") # named color are supported
True
>>> is_color("RED") # named color are case insensitive
True
>>> is_color("hello") # not a valid color name
False
"""
if string.lower() in NAMED_COLORS:
return True
if re.match(r"^#?[0-9a-f]{3}([0-9a-f]{3})?$", string, re.IGNORECASE):
return True
return False


def parse_color_string(color):
"""Converts a color string into an RGB tuple.
:param str color: The string to convert.
:return: (r, g, b)
>>> from rivalcfg.color_helpers import parse_color_string
>>> parse_color_string("#FF0000") # hexadecimal colors are supported
(255, 0, 0)
>>> parse_color_string("#F00") # short format allowed
(255, 0, 0)
>>> parse_color_string("FF0000") # "#" is optionnal
(255, 0, 0)
>>> parse_color_string("#ff0000") # case insensitive
(255, 0, 0)
>>> parse_color_string("red") # named color are supported
(255, 0, 0)
>>> parse_color_string("RED") # named color are case insensitive
(255, 0, 0)
"""
# Named color
if color.lower() in NAMED_COLORS:
return NAMED_COLORS[color.lower()]

# #f00 or #ff0000 -> f00 or ff0000
if color.startswith("#"):
color = color[1:]

# f00 -> ff0000
if len(color) == 3:
color = color[0] * 2 + color[1] * 2 + color[2] * 2 # noqa

# ff0000 -> (255, 0, 0)
return (
int(color[0:2], 16),
int(color[2:4], 16),
int(color[4:], 16)
)

27 changes: 27 additions & 0 deletions nautilus_terminal/nautilus_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from . import logger
from . import helpers
from . import color_helpers
from . import nautilus_accels_helpers


Expand Down Expand Up @@ -226,6 +227,32 @@ def _build_and_inject_ui(self):
"height-request",
TERMINAL_CHAR_HEIGHT * TERMINAL_MIN_HEIGHT + TERMINAL_BORDER_WIDTH * 2)

# Terminal colors

fg_color = (255, 255, 255)
bg_color = (0, 0, 0)

settings_fg_color = self._settings.get_string("foreground-color")
settings_bg_color = self._settings.get_string("background-color")

if color_helpers.is_color(settings_fg_color):
fg_color = color_helpers.parse_color_string(settings_fg_color)

if color_helpers.is_color(settings_bg_color):
bg_color = color_helpers.parse_color_string(settings_bg_color)

self._ui_terminal.set_color_foreground(Gdk.RGBA(
fg_color[0] / 255.0,
fg_color[1] / 255.0,
fg_color[2] / 255.0,
1))

self._ui_terminal.set_color_background(Gdk.RGBA(
bg_color[0] / 255.0,
bg_color[1] / 255.0,
bg_color[2] / 255.0,
1))

# File drag & drop support

self._ui_terminal.drag_dest_set(
Expand Down
10 changes: 10 additions & 0 deletions nautilus_terminal/schemas/org.flozz.nautilus-terminal.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,15 @@
<summary>Keyboard shortcut to toggle the terminal</summary>
<description>The keyboard shortcut to toggle Nautilus Terminal (e.g. "F4", "&lt;Ctrl&gt;F4")</description>
</key>
<key name="background-color" type="s">
<default>"#000000"</default>
<summary>Terminal background color</summary>
<description>The color of the terminal background (Hexadecimal notation. E.g. "#000000", "black")</description>
</key>
<key name="foreground-color" type="s">
<default>"#ffffff"</default>
<summary>Terminal text color</summary>
<description>The color of the terminal text (Hexadecimal notation. E.g. "#FFFFFF", "white")</description>
</key>
</schema>
</schemalist>

0 comments on commit a888fc1

Please sign in to comment.