def trans(stack: Stack, pum_width: int, context: Context, metrics: Sequence[Metric]) -> Iterator[Tuple[Metric, VimCompletion]]: s = state() scr_width, _ = s.screen display = stack.settings.display is_lower = lower(context.words_before) == context.words_before kind_dead_width = sum( display_width(s, tabsize=context.tabstop) for s in display.pum.kind_context) ellipsis_width = display_width(display.pum.ellipsis, tabsize=context.tabstop) truncate = clamp(pum_width, scr_width - context.scr_col, display.pum.x_max_len) w_adjust = _cum(stack.settings.weights, metrics=metrics) sortby = _sort_by(is_lower, adjustment=w_adjust) ranked = sorted(metrics, key=sortby) pruned = tuple(_prune(stack, context=context, ranked=ranked)) max_width = _max_width(pruned) for metric in pruned: yield metric, _cmp_to_vcmp( display.pum, label_width=metric.label_width, ellipsis_width=ellipsis_width, kind_dead_width=kind_dead_width, truncate=truncate, max_width=max_width, metric=metric, )
def _table(headers: Sequence[str], rows: Mapping[str, Mapping[str, str]]) -> str: s_rows = sorted(rows.keys(), key=strxfrm) c0_just = max( chain((0, ), (display_width(key, tabsize=_TAB_SIZE) for key in s_rows))) c_justs = { header: max( chain( (display_width(header, tabsize=_TAB_SIZE), ), (display_width(vs.get(header, ""), tabsize=_TAB_SIZE) for vs in rows.values()), )) for header in headers } def cont() -> Iterator[str]: yield _H_SEP.join( chain((" " * c0_just, ), (header.ljust(c_justs[header]) for header in headers))) for key in s_rows: yield _H_SEP.join( chain( (key.ljust(c0_just), ), (rows[key].get(header, "").ljust(c_justs[header]) for header in headers), )) h, *t = cont() rep = display_width(h, tabsize=_TAB_SIZE) sep = f"{linesep}{_V_SEP * rep}{linesep}" return sep.join(chain((h, ), t))
def _join( ctx: _ReviewCtx, instance: UUID, completion: Completion, match_metrics: MatchMetrics, ) -> Metric: weight = Weights( prefix_matches=match_metrics.prefix_matches, edit_distance=match_metrics.edit_distance, recency=ctx.inserted.get(completion.sort_by, 0), proximity=ctx.proximity.get(completion.sort_by, 0), ) label_width = display_width(completion.label, tabsize=ctx.context.tabstop) # !! WARN # Use UTF8 len for icon support # !! WARN kind_width = len(completion.kind) metric = Metric( instance=instance, comp=completion, weight_adjust=sigmoid(completion.weight_adjust), weight=weight, label_width=label_width, kind_width=kind_width, ) return metric
def _repr_str(dumper: SafeDumper, data: str) -> ScalarNode: if len(data.splitlines()) > 1: style = "|" elif display_width(data, tabsize=_TAB) > _WIDTH: style = ">" else: style = "" node = dumper.represent_scalar("tag:yaml.org,2002:str", data, style=style) return node
def _positions( display: PreviewDisplay, event: _Event, lines: Sequence[str], state: State, ) -> Iterator[Tuple[int, int, _Pos]]: scr_width, scr_height = state.screen top, btm, left, right = ( event.row, event.row + event.height + 1, event.col, event.col + event.width + event.scrollbar, ) dls = tuple( display_width(line, tabsize=state.context.tabstop) for line in lines) limit_w = _clamp(min(display.x_max_len, max(chain((0, ), dls)))) limit_h = _clamp(sum(ceil((dl or 1) / display.x_max_len) for dl in dls)) ns_width = limit_w(scr_width - left) n_height = limit_h(top - 1) b_width, b_height = border_w_h(display.border) ns_col = left - 1 n = _Pos( row=top - 1 - n_height - b_height, col=ns_col, height=n_height, width=ns_width, ) if n.row > 1 and display.positions.north is not None: yield 1, display.positions.north, n s = _Pos( row=btm, col=ns_col, height=limit_h(scr_height - btm), width=ns_width, ) if s.row + s.height < scr_height - 1 and display.positions.south is not None: yield 2, display.positions.south, s we_height = limit_h(scr_height - top - 2) w_width = limit_w(left - 2) w = _Pos( row=top, col=left - 2 - w_width - b_width, height=we_height, width=w_width, ) if display.positions.west is not None: yield 3, display.positions.west, w e = _Pos( row=top, col=right + 1, height=we_height, width=limit_w(scr_width - right - 2), ) if display.positions.east is not None: yield 4, display.positions.east, e