comparison sat_frontends/jp/base.py @ 3046:d9f328374473

jp: dark and light theme: - `background` setting can now be used in sat.conf to indicate if the terminal use a `dark` or a `light` colour - if `background` is not set or set to `auto`, the background colour is autodetected - Jp.get_config (or CommandBase.get_config) method can now be used to retrieve a setting
author Goffi <goffi@goffi.org>
date Tue, 01 Oct 2019 22:49:10 +0200
parents 3df611adb598
children 9839ce068140
comparison
equal deleted inserted replaced
3045:6af44ca3beac 3046:d9f328374473
28 import sys 28 import sys
29 import os 29 import os
30 import os.path 30 import os.path
31 import argparse 31 import argparse
32 import inspect 32 import inspect
33 import tty
34 import termios
33 from pathlib import Path 35 from pathlib import Path
34 from glob import iglob 36 from glob import iglob
35 from importlib import import_module 37 from importlib import import_module
36 from sat_frontends.tools.jid import JID 38 from sat_frontends.tools.jid import JID
37 from sat.tools import config 39 from sat.tools import config
38 from sat.tools.common import dynamic_import 40 from sat.tools.common import dynamic_import
39 from sat.tools.common import uri 41 from sat.tools.common import uri
40 from sat.tools.common import date_utils 42 from sat.tools.common import date_utils
43 from sat.tools.common import utils
44 from sat.tools.common.ansi import ANSI as A
41 from sat.core import exceptions 45 from sat.core import exceptions
42 import sat_frontends.jp 46 import sat_frontends.jp
43 from sat_frontends.jp.loops import QuitException, getJPLoop 47 from sat_frontends.jp.loops import QuitException, getJPLoop
44 from sat_frontends.jp.constants import Const as C 48 from sat_frontends.jp.constants import Const as C
45 from sat_frontends.tools import misc 49 from sat_frontends.tools import misc
98 @attribute progress_success(callable): method to call when progress is 102 @attribute progress_success(callable): method to call when progress is
99 successfully finished by default display a message 103 successfully finished by default display a message
100 @attribute progress_failure(callable): method to call when progress failed 104 @attribute progress_failure(callable): method to call when progress failed
101 by default display a message 105 by default display a message
102 """ 106 """
107 self.sat_conf = main_config
108 self.set_color_theme()
103 bridge_module = dynamic_import.bridge(bridge_name, 'sat_frontends.bridge') 109 bridge_module = dynamic_import.bridge(bridge_name, 'sat_frontends.bridge')
104 if bridge_module is None: 110 if bridge_module is None:
105 log.error("Can't import {} bridge".format(bridge_name)) 111 log.error("Can't import {} bridge".format(bridge_name))
106 sys.exit(1) 112 sys.exit(1)
107 113
108 self.bridge = bridge_module.AIOBridge() 114 self.bridge = bridge_module.AIOBridge()
109 self._onQuitCallbacks = [] 115 self._onQuitCallbacks = []
116
117 def get_config(self, name, section='jp', default=None):
118 """Retrieve a setting value from sat.conf"""
119 return config.getConfig(self.sat_conf, section, name, default=default)
120
121 def guess_background(self):
122 stdin_fd = sys.stdin.fileno()
123 old_settings = termios.tcgetattr(stdin_fd)
124 try:
125 tty.setraw(sys.stdin.fileno())
126 # we request background color
127 sys.stdout.write("\033]11;?\a")
128 sys.stdout.flush()
129 expected = "\033]11;rgb:"
130 for c in expected:
131 ch = sys.stdin.read(1)
132 if ch != c:
133 # background id is not supported
134 # TODO: log something?
135 return
136 red, green, blue = [int(c, 16)/65535 for c in sys.stdin.read(14).split('/')]
137 # '\a' is the last character
138 sys.stdin.read(1)
139 finally:
140 termios.tcsetattr(stdin_fd, termios.TCSADRAIN, old_settings)
141
142 lum = utils.per_luminance(red, green, blue)
143 if lum <= 0.5:
144 return 'dark'
145 else:
146 return 'light'
147
148 def set_color_theme(self):
149 background = self.get_config('background', default='auto')
150 if background == 'auto':
151 background = self.guess_background()
152 if background not in ('dark', 'light'):
153 raise exceptions.ConfigError(_(
154 f'Invalid value set for "background" ({background!r}), please check '
155 f'your settings in sat.conf'))
156 if background == 'light':
157 C.A_HEADER = A.FG_MAGENTA
158 C.A_SUBHEADER = A.BOLD + A.FG_RED
159 C.A_LEVEL_COLORS = (C.A_HEADER, A.BOLD + A.FG_BLUE, A.FG_MAGENTA, A.FG_CYAN)
160 C.A_SUCCESS = A.FG_GREEN
161 C.A_FAILURE = A.BOLD + A.FG_RED
162 C.A_WARNING = A.FG_RED
163 C.A_PROMPT_PATH = A.FG_BLUE
164 C.A_PROMPT_SUF = A.BOLD
165 C.A_DIRECTORY = A.BOLD + A.FG_MAGENTA
166 C.A_FILE = A.FG_BLACK
110 167
111 def _bridgeConnected(self): 168 def _bridgeConnected(self):
112 self.parser = argparse.ArgumentParser( 169 self.parser = argparse.ArgumentParser(
113 formatter_class=argparse.RawDescriptionHelpFormatter, description=DESCRIPTION) 170 formatter_class=argparse.RawDescriptionHelpFormatter, description=DESCRIPTION)
114 self._make_parents() 171 self._make_parents()
896 else: 953 else:
897 self.parser.set_defaults(_cmd=self) 954 self.parser.set_defaults(_cmd=self)
898 self.add_parser_options() 955 self.add_parser_options()
899 956
900 @property 957 @property
958 def sat_conf(self):
959 return self.host.sat_conf
960
961 @property
901 def args(self): 962 def args(self):
902 return self.host.args 963 return self.host.args
903 964
904 @property 965 @property
905 def profile(self): 966 def profile(self):