comparison sat_frontends/jp/base.py @ 3709:09f5ac48ffe3

merge bookmark @
author Goffi <goffi@goffi.org>
date Fri, 12 Nov 2021 17:21:24 +0100
parents 51983c55c5b6 5131ed9163c0
children ea204216a505
comparison
equal deleted inserted replaced
3684:8353cc3b8db9 3709:09f5ac48ffe3
119 def get_config(self, name, section=C.CONFIG_SECTION, default=None): 119 def get_config(self, name, section=C.CONFIG_SECTION, default=None):
120 """Retrieve a setting value from sat.conf""" 120 """Retrieve a setting value from sat.conf"""
121 return config.getConfig(self.sat_conf, section, name, default=default) 121 return config.getConfig(self.sat_conf, section, name, default=default)
122 122
123 def guess_background(self): 123 def guess_background(self):
124 if not sys.stdin.isatty() or not sys.stdout.isatty(): 124 # cf. https://unix.stackexchange.com/a/245568 (thanks!)
125 return 'dark' 125 try:
126 stdin_fd = sys.stdin.fileno() 126 # for VTE based terminals
127 old_settings = termios.tcgetattr(stdin_fd) 127 vte_version = int(os.getenv("VTE_VERSION", 0))
128 try: 128 except ValueError:
129 tty.setraw(sys.stdin.fileno()) 129 vte_version = 0
130 # we request background color 130
131 sys.stdout.write("\033]11;?\a") 131 color_fg_bg = os.getenv("COLORFGBG")
132 sys.stdout.flush() 132
133 expected = "\033]11;rgb:" 133 if ((sys.stdin.isatty() and sys.stdout.isatty()
134 for c in expected: 134 and (
135 ch = sys.stdin.read(1) 135 # XTerm
136 if ch != c: 136 os.getenv("XTERM_VERSION")
137 # background id is not supported, we default to "dark" 137 # Konsole
138 # TODO: log something? 138 or os.getenv("KONSOLE_VERSION")
139 return 'dark' 139 # All VTE based terminals
140 red, green, blue = [int(c, 16)/65535 for c in sys.stdin.read(14).split('/')] 140 or vte_version >= 3502
141 # '\a' is the last character 141 ))):
142 sys.stdin.read(1) 142 # ANSI escape sequence
143 finally: 143 stdin_fd = sys.stdin.fileno()
144 termios.tcsetattr(stdin_fd, termios.TCSADRAIN, old_settings) 144 old_settings = termios.tcgetattr(stdin_fd)
145 145 try:
146 lum = utils.per_luminance(red, green, blue) 146 tty.setraw(sys.stdin.fileno())
147 if lum <= 0.5: 147 # we request background color
148 return 'dark' 148 sys.stdout.write("\033]11;?\a")
149 else: 149 sys.stdout.flush()
150 return 'light' 150 expected = "\033]11;rgb:"
151 for c in expected:
152 ch = sys.stdin.read(1)
153 if ch != c:
154 # background id is not supported, we default to "dark"
155 # TODO: log something?
156 return 'dark'
157 red, green, blue = [
158 int(c, 16)/65535 for c in sys.stdin.read(14).split('/')
159 ]
160 # '\a' is the last character
161 sys.stdin.read(1)
162 finally:
163 termios.tcsetattr(stdin_fd, termios.TCSADRAIN, old_settings)
164
165 lum = utils.per_luminance(red, green, blue)
166 if lum <= 0.5:
167 return 'dark'
168 else:
169 return 'light'
170 elif color_fg_bg:
171 # no luck with ANSI escape sequence, we try COLORFGBG environment variable
172 try:
173 bg = int(color_fg_bg.split(";")[-1])
174 except ValueError:
175 return "dark"
176 if bg in list(range(7)) + [8]:
177 return "dark"
178 else:
179 return "light"
180 else:
181 # no autodetection method found
182 return "dark"
151 183
152 def set_color_theme(self): 184 def set_color_theme(self):
153 background = self.get_config('background', default='auto') 185 background = self.get_config('background', default='auto')
154 if background == 'auto': 186 if background == 'auto':
155 background = self.guess_background() 187 background = self.guess_background()
157 raise exceptions.ConfigError(_( 189 raise exceptions.ConfigError(_(
158 'Invalid value set for "background" ({background}), please check ' 190 'Invalid value set for "background" ({background}), please check '
159 'your settings in libervia.conf').format( 191 'your settings in libervia.conf').format(
160 background=repr(background) 192 background=repr(background)
161 )) 193 ))
194 self.background = background
162 if background == 'light': 195 if background == 'light':
163 C.A_HEADER = A.FG_MAGENTA 196 C.A_HEADER = A.FG_MAGENTA
164 C.A_SUBHEADER = A.BOLD + A.FG_RED 197 C.A_SUBHEADER = A.BOLD + A.FG_RED
165 C.A_LEVEL_COLORS = (C.A_HEADER, A.BOLD + A.FG_BLUE, A.FG_MAGENTA, A.FG_CYAN) 198 C.A_LEVEL_COLORS = (C.A_HEADER, A.BOLD + A.FG_BLUE, A.FG_MAGENTA, A.FG_CYAN)
166 C.A_SUCCESS = A.FG_GREEN 199 C.A_SUCCESS = A.FG_GREEN