efficient vim config
[dotfiles/.git] / .local / lib / python3.9 / site-packages / pywal / sequences.py
1 """
2 Send sequences to all open terminals.
3 """
4 import glob
5 import logging
6 import os
7
8 from .settings import CACHE_DIR, OS
9 from . import util
10
11
12 def set_special(index, color, iterm_name="h", alpha=100):
13     """Convert a hex color to a special sequence."""
14     if OS == "Darwin":
15         return "\033]P%s%s\033\\" % (iterm_name, color.strip("#"))
16
17     if index in [11, 708] and alpha != "100":
18         return "\033]%s;[%s]%s\033\\" % (index, alpha, color)
19
20     return "\033]%s;%s\033\\" % (index, color)
21
22
23 def set_color(index, color):
24     """Convert a hex color to a text color sequence."""
25     if OS == "Darwin" and index < 20:
26         return "\033]P%1x%s\033\\" % (index, color.strip("#"))
27
28     return "\033]4;%s;%s\033\\" % (index, color)
29
30
31 def set_iterm_tab_color(color):
32     """Set iTerm2 tab/window color"""
33     return ("\033]6;1;bg;red;brightness;%s\a"
34             "\033]6;1;bg;green;brightness;%s\a"
35             "\033]6;1;bg;blue;brightness;%s\a") % (*util.hex_to_rgb(color),)
36
37
38 def create_sequences(colors, vte_fix=False):
39     """Create the escape sequences."""
40     alpha = colors["alpha"]
41
42     # Colors 0-15.
43     sequences = [set_color(index, colors["colors"]["color%s" % index])
44                  for index in range(16)]
45
46     # Special colors.
47     # Source: https://goo.gl/KcoQgP
48     # 10 = foreground, 11 = background, 12 = cursor foregound
49     # 13 = mouse foreground, 708 = background border color.
50     sequences.extend([
51         set_special(10, colors["special"]["foreground"], "g"),
52         set_special(11, colors["special"]["background"], "h", alpha),
53         set_special(12, colors["special"]["cursor"], "l"),
54         set_special(13, colors["special"]["foreground"], "l"),
55         set_special(17, colors["special"]["foreground"], "l"),
56         set_special(19, colors["special"]["background"], "l"),
57         set_color(232, colors["special"]["background"]),
58         set_color(256, colors["special"]["foreground"])
59     ])
60
61     if not vte_fix:
62         sequences.extend(
63             set_special(708, colors["special"]["background"], "l", alpha)
64         )
65
66     if OS == "Darwin":
67         sequences += set_iterm_tab_color(colors["special"]["background"])
68
69     return "".join(sequences)
70
71
72 def send(colors, cache_dir=CACHE_DIR, to_send=True, vte_fix=False):
73     """Send colors to all open terminals."""
74     if OS == "Darwin":
75         tty_pattern = "/dev/ttys00[0-9]*"
76
77     else:
78         tty_pattern = "/dev/pts/[0-9]*"
79
80     sequences = create_sequences(colors, vte_fix)
81
82     # Writing to "/dev/pts/[0-9] lets you send data to open terminals.
83     if to_send:
84         for term in glob.glob(tty_pattern):
85             util.save_file(sequences, term)
86
87     util.save_file(sequences, os.path.join(cache_dir, "sequences"))
88     logging.info("Set terminal colors.")