Exemplo n.º 1
0
def main(args: List[str]) -> None:
    warnings.showwarning = showwarning
    cli_opts, items = parse_args(args[1:], OPTIONS, usage, help_text, 'kitty +kitten diff', result_class=DiffCLIOptions)
    if len(items) != 2:
        raise SystemExit('You must specify exactly two files/directories to compare')
    left, right = items
    global_data.title = _('{} vs. {}').format(left, right)
    if os.path.isdir(left) != os.path.isdir(right):
        raise SystemExit('The items to be diffed should both be either directories or files. Comparing a directory to a file is not valid.')
    opts = init_config(cli_opts)
    set_diff_command(opts.diff_cmd)
    lines_for_path.replace_tab_by = opts.replace_tab_by
    left, right = map(get_remote_file, (left, right))
    for f in left, right:
        if not os.path.exists(f):
            raise SystemExit('{} does not exist'.format(f))

    loop = Loop()
    handler = DiffHandler(cli_opts, opts, left, right)
    loop.loop(handler)
    for message in showwarning.warnings:
        from kitty.utils import safe_print
        safe_print(message, file=sys.stderr)
    highlight_processes = getattr(highlight_collection, 'processes', ())
    terminate_processes(tuple(highlight_processes))
    terminate_processes(tuple(worker_processes))
    if loop.return_code != 0:
        if handler.report_traceback_on_exit:
            print(handler.report_traceback_on_exit, file=sys.stderr)
            input('Press Enter to quit.')
        raise SystemExit(loop.return_code)
Exemplo n.º 2
0
def find_best_match(family, bold=False, italic=False):
    q = re.sub(r'\s+', ' ', family.lower())
    font_map = all_fonts_map()

    def score(candidate):
        style_match = 1 if candidate['bold'] == bold and candidate[
            'italic'] == italic else 0
        monospace_match = 1 if candidate['monospace'] else 0
        return style_match, monospace_match

    # First look for an exact match
    for selector in ('ps_map', 'full_map'):
        candidates = font_map[selector].get(q)
        if candidates:
            candidates.sort(key=score)
            return candidates[-1]

    # Let CoreText choose the font if the family exists, otherwise
    # fallback to Menlo
    if q not in font_map['family_map']:
        safe_print(
            'The font {} was not found, falling back to Menlo'.format(family),
            file=sys.stderr)
        family = 'Menlo'
    return {
        'monospace': True,
        'bold': bold,
        'italic': italic,
        'family': family
    }
Exemplo n.º 3
0
def main(args):
    warnings.showwarning = showwarning
    args, items = parse_args(args[1:], OPTIONS, usage, help_text,
                             'kitty +kitten diff')
    if len(items) != 2:
        raise SystemExit(
            'You must specify exactly two files/directories to compare')
    left, right = items
    main.title = _('{} vs. {}').format(left, right)
    if os.path.isdir(left) != os.path.isdir(right):
        raise SystemExit(
            'The items to be diffed should both be either directories or files. Comparing a directory to a file is not valid.'
        )
    opts = init_config(args)
    set_diff_command(opts.diff_cmd)
    lines_for_path.replace_tab_by = opts.replace_tab_by

    loop = Loop()
    handler = DiffHandler(args, opts, left, right)
    loop.loop(handler)
    for message in showwarning.warnings:
        from kitty.utils import safe_print
        safe_print(message, file=sys.stderr)
    if loop.return_code != 0:
        if handler.report_traceback_on_exit:
            print(handler.report_traceback_on_exit, file=sys.stderr)
            input('Press Enter to quit.')
        raise SystemExit(loop.return_code)
Exemplo n.º 4
0
def main(args):
    warnings.showwarning = showwarning
    msg = 'Show a side-by-side diff of the specified files/directories'
    args, items = parse_args(args[1:], OPTIONS,
                             'file_or_directory file_or_directory', msg,
                             'kitty +kitten diff')
    if len(items) != 2:
        raise SystemExit(
            'You must specify exactly two files/directories to compare')
    left, right = items
    if os.path.isdir(left) != os.path.isdir(right):
        raise SystemExit(
            'The items to be diffed should both be either directories or files. Comparing a directory to a file is not valid.'
        )
    opts = init_config(args)
    set_diff_command(opts.diff_cmd)

    loop = Loop()
    handler = DiffHandler(args, opts, left, right)
    loop.loop(handler)
    for message in showwarning.warnings:
        from kitty.utils import safe_print
        safe_print(message, file=sys.stderr)
    if loop.return_code != 0:
        if handler.report_traceback_on_exit:
            print(handler.report_traceback_on_exit, file=sys.stderr)
        raise SystemExit(loop.return_code)
Exemplo n.º 5
0
 def safe_freetype(func):
     try:
         return func(text, bold, italic, num_cells)
     except FontNotFound as err:
         safe_print('ERROR:', err, file=sys.stderr)
     except FreeTypeError as err:
         safe_print('Failed to render text:',
                    repr(text),
                    'with error:',
                    err,
                    file=sys.stderr)
Exemplo n.º 6
0
def render_cell(text=' ', bold=False, italic=False):
    # TODO: Handle non-normalizable combining chars. Probably need to use
    # harfbuzz for that
    text = unicodedata.normalize('NFC', text)[0]
    width = wcwidth(text)
    try:
        bitmap_char = render_char(text, bold, italic, width)
    except FontNotFound as err:
        safe_print('ERROR:', err, file=sys.stderr)
        return missing_glyph(width)
    second = None
    if width == 2:
        if bitmap_char.columns > cell_width:
            bitmap_char, second = split_char_bitmap(bitmap_char)
            second = place_char_in_cell(second)
        else:
            second = render_cell()[0]

    first = place_char_in_cell(bitmap_char)

    return first, second