1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-11-10 06:46:15 +01:00

Move run_pager() and get_default_pager() from 'core' to 'output' module

This commit is contained in:
Daniel Mach 2024-04-16 16:18:50 +02:00
parent c39b648615
commit 861a362622
3 changed files with 56 additions and 45 deletions

View File

@ -77,6 +77,8 @@ from .obs_scm.store import store_write_initial_packages
from .obs_scm.store import store_write_last_buildroot
from .obs_scm.store import store_write_project
from .obs_scm.store import store_write_string
from .output import get_default_pager
from .output import run_pager
from .output import sanitize_text
from .util import xdg
from .util.helper import decode_list, decode_it, raw_input, _html_escape
@ -1915,16 +1917,6 @@ def get_default_editor():
return 'vi'
def get_default_pager():
system = platform.system()
if system == 'Linux':
dist = _get_linux_distro()
if dist == 'debian':
return 'pager'
return 'less'
return 'more'
def format_diff_line(line):
if line.startswith(b"+++") or line.startswith(b"---") or line.startswith(b"Index:"):
line = b"\x1b[1m" + line + b"\x1b[0m"
@ -1943,41 +1935,6 @@ def highlight_diff(diff):
return diff
def run_pager(message, tmp_suffix=''):
if not message:
return
if not sys.stdout.isatty():
if isinstance(message, str):
print(message)
else:
sys.stdout.buffer.write(message)
else:
tmpfile = tempfile.NamedTemporaryFile(suffix=tmp_suffix)
if isinstance(message, str):
tmpfile.write(bytes(message, 'utf-8'))
else:
tmpfile.write(message)
tmpfile.flush()
env = os.environ.copy()
pager = os.getenv("PAGER", default="").strip()
pager = pager or get_default_pager()
# LESS env is not always set and we need -R to display escape sequences properly
less_opts = os.getenv("LESS", default="")
if "-R" not in less_opts:
less_opts += " -R"
env["LESS"] = less_opts
cmd = shlex.split(pager) + [tmpfile.name]
try:
run_external(*cmd, env=env)
finally:
tmpfile.close()
def run_editor(filename):
cmd = _editor_command()
cmd.append(filename)

View File

@ -1,6 +1,8 @@
from .key_value_table import KeyValueTable
from .input import get_user_input
from .output import get_default_pager
from .output import print_msg
from .output import run_pager
from .output import sanitize_text
from .output import safe_print
from .output import safe_write

View File

@ -1,6 +1,9 @@
import os
import platform
import re
import shlex
import sys
import tempfile
from typing import Dict
from typing import Optional
from typing import TextIO
@ -147,3 +150,52 @@ def safe_write(file: TextIO, text: Union[str, bytes], *, add_newline: bool = Fal
file.write(text)
if add_newline:
file.write(os.linesep)
def get_default_pager():
from ..core import _get_linux_distro
system = platform.system()
if system == 'Linux':
dist = _get_linux_distro()
if dist == 'debian':
return 'pager'
return 'less'
return 'more'
def run_pager(message: Union[bytes, str], tmp_suffix: str = ""):
from ..core import run_external
if not message:
return
if not sys.stdout.isatty():
if isinstance(message, str):
print(message)
else:
sys.stdout.buffer.write(message)
else:
tmpfile = tempfile.NamedTemporaryFile(suffix=tmp_suffix)
if isinstance(message, str):
tmpfile.write(bytes(message, 'utf-8'))
else:
tmpfile.write(message)
tmpfile.flush()
env = os.environ.copy()
pager = os.getenv("PAGER", default="").strip()
pager = pager or get_default_pager()
# LESS env is not always set and we need -R to display escape sequences properly
less_opts = os.getenv("LESS", default="")
if "-R" not in less_opts:
less_opts += " -R"
env["LESS"] = less_opts
cmd = shlex.split(pager) + [tmpfile.name]
try:
run_external(*cmd, env=env)
finally:
tmpfile.close()