2 Send sequences to all open terminals.
8 from .settings import CACHE_DIR, OS
12 def set_special(index, color, iterm_name="h", alpha=100):
13 """Convert a hex color to a special sequence."""
15 return "\033]P%s%s\033\\" % (iterm_name, color.strip("#"))
17 if index in [11, 708] and alpha != "100":
18 return "\033]%s;[%s]%s\033\\" % (index, alpha, color)
20 return "\033]%s;%s\033\\" % (index, color)
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("#"))
28 return "\033]4;%s;%s\033\\" % (index, color)
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),)
38 def create_sequences(colors, vte_fix=False):
39 """Create the escape sequences."""
40 alpha = colors["alpha"]
43 sequences = [set_color(index, colors["colors"]["color%s" % index])
44 for index in range(16)]
47 # Source: https://goo.gl/KcoQgP
48 # 10 = foreground, 11 = background, 12 = cursor foregound
49 # 13 = mouse foreground, 708 = background border color.
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"])
63 set_special(708, colors["special"]["background"], "l", alpha)
67 sequences += set_iterm_tab_color(colors["special"]["background"])
69 return "".join(sequences)
72 def send(colors, cache_dir=CACHE_DIR, to_send=True, vte_fix=False):
73 """Send colors to all open terminals."""
75 tty_pattern = "/dev/ttys00[0-9]*"
78 tty_pattern = "/dev/pts/[0-9]*"
80 sequences = create_sequences(colors, vte_fix)
82 # Writing to "/dev/pts/[0-9] lets you send data to open terminals.
84 for term in glob.glob(tty_pattern):
85 util.save_file(sequences, term)
87 util.save_file(sequences, os.path.join(cache_dir, "sequences"))
88 logging.info("Set terminal colors.")