summaryrefslogtreecommitdiff
path: root/pywal/sequences.py
blob: 4f14234c653900a9dce369ca8396232d2de4c8a6 (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
"""
Send sequences to all open terminals.
"""
import os

from .settings import __cache_dir__
from . import util


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

    if index in [11, 708] and alpha != 100:
        return f"\033]{index};[{alpha}]{color}\007"

    return f"\033]{index};{color}\007"


def set_color(index, color):
    """Convert a hex color to a text color sequence."""
    return f"\033]4;{index};{color}\007"


def send(colors, vte, cache_dir=__cache_dir__):
    """Send colors to all open terminals."""
    # Colors 0-15.
    sequences = [set_color(num, color)
                 for num, color in enumerate(colors["colors"].values())]

    # Special colors.
    # Source: https://goo.gl/KcoQgP
    # 10 = foreground, 11 = background, 12 = cursor foregound
    # 13 = mouse foreground
    sequences.append(set_special(10, colors["special"]["foreground"]))
    sequences.append(set_special(11, colors["special"]["background"]))
    sequences.append(set_special(12, colors["special"]["cursor"]))
    sequences.append(set_special(13, colors["special"]["cursor"]))

    # Set a blank color that isn't affected by bold highlighting.
    # Used in wal.vim's airline theme.
    sequences.append(set_color(66, colors["special"]["background"]))

    # This escape sequence doesn"t work in VTE terminals.
    if not vte:
        sequences.append(set_special(708, colors["special"]["background"]))

    terminals = [f"/dev/pts/{term}" for term in os.listdir("/dev/pts/")
                 if len(term) < 4]
    terminals.append(cache_dir / "sequences")

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

    print("colors: Set terminal colors")