summaryrefslogtreecommitdiff
path: root/pywal/sequences.py
blob: 1b2fce5c3a5dbc0674c606f2c0f311859b2f6ade (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Send sequences to all open terminals.
"""
import glob
import os

from .settings import CACHE_DIR, OS
from . import util


def set_special(index, color, iterm_name="h"):
    """Convert a hex color to a special sequence."""
    alpha = util.Color.alpha_num

    if OS == "Darwin":
        return "\033]P%s%s\033\\" % (iterm_name, color.strip("#"))

    if index in [11, 708] and alpha != "100":
        return "\033]%s;[%s]%s\033\\" % (index, alpha, color)

    return "\033]%s;%s\033\\" % (index, color)


def set_color(index, color):
    """Convert a hex color to a text color sequence."""
    if OS == "Darwin":
        return "\033]P%x%s\033\\" % (index, color.strip("#"))

    return "\033]4;%s;%s\033\\" % (index, color)


def set_iterm_tab_color(color):
    """Set iTerm2 tab/window color"""
    return """
    \033]6;1;bg;red;brightness;%s\a
    \033]6;1;bg;green;brightness;%s\a
    \033]6;1;bg;blue;brightness;%s\a
    """ % (*util.hex_to_rgb(color),)


def create_sequences(colors):
    """Create the escape sequences."""
    # Colors 0-15.
    sequences = [set_color(index, colors["colors"]["color%s" % index])
                 for index in range(16)]

    # Special colors.
    # Source: https://goo.gl/KcoQgP
    # 10 = foreground, 11 = background, 12 = cursor foregound
    # 13 = mouse foreground
    sequences.extend([set_special(10, colors["special"]["foreground"], "g"),
                      set_special(11, colors["special"]["background"], "h"),
                      set_special(12, colors["colors"]["color1"], "l"),
                      set_special(13, colors["special"]["foreground"], "l"),
                      set_special(17, colors["special"]["foreground"], "l"),
                      set_special(19, colors["special"]["background"], "l")])

    # This escape sequence doesn't work in VTE terminals and their parsing of
    # unknown sequences is garbage so we need to use some escape sequence
    # M A G I C to hide the output.
    # \033[s                 # Save cursor position.
    # \033[1000H             # Move the cursor off screen.
    # \033[8m                # Conceal text.
    # \033]708;#000000\033\\ # Garbage sequence.
    # \033[u                 # Restore cursor position.
    sequences.extend(["\033[s\033[1000H\033[8m%s\033[u" %
                      set_special(708, colors['special']['background']),
                      set_special(13, colors["special"]["cursor"], "l")])

    if OS == "Darwin":
        sequences += set_iterm_tab_color(colors["special"]["background"])

    return "".join(sequences)


def send(colors, cache_dir=CACHE_DIR):
    """Send colors to all open terminals."""
    if OS == "Darwin":
        tty_pattern = "/dev/ttys00[0-9]*"

    else:
        tty_pattern = "/dev/pts/[0-9]*"

    sequences = create_sequences(colors)

    # Writing to "/dev/pts/[0-9] lets you send data to open terminals.
    for term in glob.glob(tty_pattern):
        util.save_file(sequences, term)

    util.save_file(sequences, os.path.join(cache_dir, "sequences"))
    print("colors: Set terminal colors.")