Move splitlines_like_git to a more appropriate home

This commit is contained in:
Kovid Goyal 2023-03-10 17:26:06 +05:30
parent ffa8c1c498
commit 7acc6bdeb8
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 39 additions and 38 deletions

View File

@ -1,7 +1,10 @@
from typing import List, Optional, Tuple
from typing import Callable, List, Optional, Tuple
from .collect import Segment
def splitlines_like_git(raw: bytes, callback: Callable[[memoryview], None]) -> None: ...
def split_with_highlights(
line: str, truncate_points: List[int], fg_highlights: List[Segment],
bg_highlight: Optional[Segment]

View File

@ -8,11 +8,9 @@
import subprocess
from typing import Dict, Iterator, List, Optional, Sequence, Tuple, Union
from kitty.fast_data_types import splitlines_like_git
from . import global_data
from .collect import lines_for_path
from .diff_speedup import changed_center
from .diff_speedup import changed_center, splitlines_like_git
left_lines: Tuple[str, ...] = ()
right_lines: Tuple[str, ...] = ()

View File

@ -175,6 +175,39 @@ split_with_highlights(PyObject *self UNUSED, PyObject *args) {
#undef NEXT_TRUNCATE_POINT
}
static PyObject*
splitlines_like_git(PyObject *self UNUSED, PyObject *args) {
char *raw; Py_ssize_t sz;
PyObject *callback;
if (!PyArg_ParseTuple(args, "y#O", &raw, &sz, &callback)) return NULL;
while (sz > 0 && (raw[sz-1] == '\n' || raw[sz-1] == '\r')) sz--;
PyObject *mv, *ret;
#define CALLBACK \
mv = PyMemoryView_FromMemory(raw + start, i - start, PyBUF_READ); \
if (mv == NULL) return NULL; \
ret = PyObject_CallFunctionObjArgs(callback, mv, NULL); \
Py_DECREF(mv); \
if (ret == NULL) return NULL; \
Py_DECREF(ret); start = i + 1;
Py_ssize_t i = 0, start = 0;
for (; i < sz; i++) {
switch (raw[i]) {
case '\n':
CALLBACK; break;
case '\r':
CALLBACK;
if (i + 1 < sz && raw[i+1] == '\n') { i++; start++; }
break;
}
}
if (start < sz) {
i = sz; CALLBACK;
}
Py_RETURN_NONE;
}
static void
free_resources(void) {
free(line_buffer.buf); line_buffer.buf = NULL; line_buffer.capacity = 0; line_buffer.pos = 0;
@ -183,6 +216,7 @@ free_resources(void) {
static PyMethodDef module_methods[] = {
{"changed_center", (PyCFunction)changed_center, METH_VARARGS, ""},
{"split_with_highlights", (PyCFunction)split_with_highlights, METH_VARARGS, ""},
{"splitlines_like_git", (PyCFunction)splitlines_like_git, METH_VARARGS, ""},
{NULL, NULL, 0, NULL} /* Sentinel */
};

View File

@ -1535,4 +1535,3 @@ def expand_ansi_c_escapes(test: str) -> str: ...
def update_tab_bar_edge_colors(os_window_id: int) -> bool: ...
def mask_kitty_signals_process_wide() -> None: ...
def is_modifier_key(key: int) -> bool: ...
def splitlines_like_git(raw: bytes, callback: Callable[[memoryview], None]) -> None: ...

View File

@ -196,42 +196,9 @@ parse_input_from_terminal(PyObject *self UNUSED, PyObject *args) {
#undef CALL
}
static PyObject*
splitlines_like_git(PyObject *self UNUSED, PyObject *args) {
char *raw; Py_ssize_t sz;
PyObject *callback;
if (!PyArg_ParseTuple(args, "y#O", &raw, &sz, &callback)) return NULL;
while (sz > 0 && (raw[sz-1] == '\n' || raw[sz-1] == '\r')) sz--;
PyObject *mv, *ret;
#define CALLBACK \
mv = PyMemoryView_FromMemory(raw + start, i - start, PyBUF_READ); \
if (mv == NULL) return NULL; \
ret = PyObject_CallFunctionObjArgs(callback, mv, NULL); \
Py_DECREF(mv); \
if (ret == NULL) return NULL; \
Py_DECREF(ret); start = i + 1;
Py_ssize_t i = 0, start = 0;
for (; i < sz; i++) {
switch (raw[i]) {
case '\n':
CALLBACK; break;
case '\r':
CALLBACK;
if (i + 1 < sz && raw[i+1] == '\n') { i++; start++; }
break;
}
}
if (start < sz) {
i = sz; CALLBACK;
}
Py_RETURN_NONE;
}
static PyMethodDef module_methods[] = {
METHODB(parse_input_from_terminal, METH_VARARGS),
METHODB(read_command_response, METH_VARARGS),
METHODB(splitlines_like_git, METH_VARARGS),
{NULL, NULL, 0, NULL} /* Sentinel */
};