def from_screen( cls, stdscr: 'curses._CursesWindow', color_manager: ColorManager, ) -> 'Syntax': grammars = Grammars(prefix_data('grammar_v1'), xdg_data('grammar_v1')) theme = Theme.from_filename(xdg_config('theme.json')) ret = cls(grammars, theme, color_manager) ret._init_screen(stdscr) return ret
def test_theme_default_settings_from_no_scope(): theme = Theme.from_dct({ 'tokenColors': [ { 'settings': { 'foreground': '#cccccc', 'background': '#333333' } }, ], }) assert theme.default.fg == Color.parse('#cccccc') assert theme.default.bg == Color.parse('#333333')
def _highlight_output(theme: Theme, compiler: Compiler, filename: str) -> int: state = compiler.root_state if theme.default.bg is not None: print('\x1b[48;2;{r};{g};{b}m'.format(**theme.default.bg._asdict())) with open(filename, encoding='UTF-8') as f: for line_idx, line in enumerate(f): first_line = line_idx == 0 state, regions = highlight_line(compiler, state, line, first_line) for start, end, scope in regions: print_styled(line[start:end], theme.select(scope)) print('\x1b[m', end='') return 0
def main(argv: Optional[Sequence[str]] = None) -> int: parser = argparse.ArgumentParser() parser.add_argument('--theme', default=xdg_config('theme.json')) parser.add_argument('--grammar-dir', default=prefix_data('grammar_v1')) parser.add_argument('filename') args = parser.parse_args(argv) with open(args.filename, encoding='UTF-8') as f: first_line = next(f, '') theme = Theme.from_filename(args.theme) grammars = Grammars(args.grammar_dir) compiler = grammars.compiler_for_file(args.filename, first_line) return _highlight_output(theme, compiler, args.filename)
def test_theme_scope_comma_at_beginning_and_end(): theme = Theme.from_dct({ 'colors': { 'foreground': '#cccccc', 'background': '#333333' }, 'tokenColors': [ { 'scope': '\n,a,b,\n', 'settings': { 'fontStyle': 'italic' } }, ], }) assert theme.select(('d', )).i is False assert theme.select(('a', )).i is True assert theme.select(('b', )).i is True
def test_theme_scope_split_by_commas(): theme = Theme.from_dct({ 'colors': { 'foreground': '#cccccc', 'background': '#333333' }, 'tokenColors': [ { 'scope': 'a, b, c', 'settings': { 'fontStyle': 'italic' } }, ], }) assert theme.select(('d', )).i is False assert theme.select(('a', )).i is True assert theme.select(('b', )).i is True assert theme.select(('c', )).i is True
def test_theme_scope_as_A_list(): theme = Theme.from_dct({ 'colors': { 'foreground': '#cccccc', 'background': '#333333' }, 'tokenColors': [ { 'scope': ['a', 'b', 'c'], 'settings': { 'fontStyle': 'underline' } }, ], }) assert theme.select(('d', )).u is False assert theme.select(('a', )).u is True assert theme.select(('b', )).u is True assert theme.select(('c', )).u is True
def test_theme_scope_internal_newline_commas(): # this is arguably malformed, but `cobalt2` in the wild has this issue theme = Theme.from_dct({ 'colors': { 'foreground': '#cccccc', 'background': '#333333' }, 'tokenColors': [ { 'scope': '\n,a,\n,b,\n', 'settings': { 'fontStyle': 'italic' } }, ], }) assert theme.select(('d', )).i is False assert theme.select(('a', )).i is True assert theme.select(('b', )).i is True
from babi.color import Color from babi.theme import Theme THEME = Theme.from_dct({ 'colors': { 'foreground': '#100000', 'background': '#aaaaaa' }, 'tokenColors': [ { 'scope': 'foo.bar', 'settings': { 'foreground': '#200000' } }, { 'scope': 'foo', 'settings': { 'foreground': '#300000' } }, { 'scope': 'parent foo.bar', 'settings': { 'foreground': '#400000' } }, ], }) def unhex(color):