Skip to content

Commit c17cf2b

Browse files
author
Tymoteusz Jankowski
committed
dvc/dagascii: Use less instead of AsciiCanvas._do_draw
Uses less CLI tool to paging the output in the interactive mode while doing e.g. `dvc pipeline show ..`. Fixes #2807
1 parent 24d3a87 commit c17cf2b

File tree

3 files changed

+48
-129
lines changed

3 files changed

+48
-129
lines changed

dvc/dagascii.py

Lines changed: 48 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
from __future__ import print_function
33
from __future__ import unicode_literals
44

5+
import logging
56
import math
7+
import os
8+
import pydoc
69
import sys
10+
from distutils.spawn import find_executable
711

812
from grandalf.graphs import Edge
913
from grandalf.graphs import Graph
@@ -12,6 +16,44 @@
1216
from grandalf.routing import EdgeViewer
1317
from grandalf.routing import route_with_lines
1418

19+
from dvc.utils import boxify
20+
21+
22+
logger = logging.getLogger(__name__)
23+
24+
25+
DEFAULT_PAGER = "less"
26+
DEFAULT_PAGER_FORMATTED = "{} --chop-long-lines --clear-screen".format(
27+
DEFAULT_PAGER
28+
)
29+
DVC_PAGER_VAR_NAME = "DVC_PAGER"
30+
DVC_PAGER_INFO = """\
31+
Less command line tool is missing.
32+
Install & add less tool to your PATH env. var. to automatically send output \
33+
to pager.
34+
Also, you can override default pager via {} env. var.
35+
""".format(
36+
DVC_PAGER_VAR_NAME
37+
)
38+
39+
40+
def find_pager(pager_cmd, pager_cmd_with_format, pager_env_name):
41+
if sys.stdout.isatty():
42+
less_found = find_executable(pager_cmd) is not None
43+
if less_found:
44+
pager_cmd = os.getenv(pager_env_name, pager_cmd_with_format)
45+
46+
def less_pager(text):
47+
return pydoc.tempfilepager(pydoc.plain(text), pager_cmd)
48+
49+
pager = less_pager
50+
else:
51+
logger.info(boxify(DVC_PAGER_INFO, border_color="yellow"))
52+
pager = pydoc.plainpager
53+
else:
54+
pager = pydoc.plainpager
55+
return pager
56+
1557

1658
class VertexViewer(object):
1759
"""Class to define vertex box boundaries that will be accounted for during
@@ -60,99 +102,12 @@ def __init__(self, cols, lines):
60102

61103
def draw(self):
62104
"""Draws ASCII canvas on the screen."""
63-
if sys.stdout.isatty(): # pragma: no cover
64-
from asciimatics.screen import Screen
65-
66-
Screen.wrapper(self._do_draw)
67-
else:
68-
for line in self.canvas:
69-
print("".join(line))
70-
71-
def _do_draw(self, screen): # pragma: no cover
72-
# pylint: disable=too-many-locals
73-
# pylint: disable=too-many-branches, too-many-statements
74-
from dvc.system import System
75-
from asciimatics.event import KeyboardEvent
76-
77-
offset_x = 0
78-
offset_y = 0
79-
smaxrow, smaxcol = screen.dimensions
80-
assert smaxrow > 1
81-
assert smaxcol > 1
82-
smaxrow -= 1
83-
smaxcol -= 1
84-
85-
if self.lines + 1 > smaxrow:
86-
max_y = self.lines + 1 - smaxrow
87-
else:
88-
max_y = 0
89-
90-
if self.cols + 1 > smaxcol:
91-
max_x = self.cols + 1 - smaxcol
92-
else:
93-
max_x = 0
94-
95-
while True:
96-
for y in range(smaxrow + 1):
97-
y_index = offset_y + y
98-
line = []
99-
for x in range(smaxcol + 1):
100-
x_index = offset_x + x
101-
if (
102-
len(self.canvas) > y_index
103-
and len(self.canvas[y_index]) > x_index
104-
):
105-
line.append(self.canvas[y_index][x_index])
106-
else:
107-
line.append(" ")
108-
assert len(line) == (smaxcol + 1)
109-
screen.print_at("".join(line), 0, y)
110-
111-
screen.refresh()
112-
113-
# NOTE: get_event() doesn't block by itself,
114-
# so we have to do the blocking ourselves.
115-
#
116-
# NOTE: using this workaround while waiting for PR [1]
117-
# to get merged and released. After that need to adjust
118-
# asciimatics version requirements.
119-
#
120-
# [1] https://github.com/peterbrittain/asciimatics/pull/188
121-
System.wait_for_input(self.TIMEOUT)
122-
123-
event = screen.get_event()
124-
if not isinstance(event, KeyboardEvent):
125-
continue
126-
127-
k = event.key_code
128-
if k == screen.KEY_DOWN or k == ord("s"):
129-
offset_y += 1
130-
elif k == screen.KEY_PAGE_DOWN or k == ord("S"):
131-
offset_y += smaxrow
132-
elif k == screen.KEY_UP or k == ord("w"):
133-
offset_y -= 1
134-
elif k == screen.KEY_PAGE_UP or k == ord("W"):
135-
offset_y -= smaxrow
136-
elif k == screen.KEY_RIGHT or k == ord("d"):
137-
offset_x += 1
138-
elif k == ord("D"):
139-
offset_x += smaxcol
140-
elif k == screen.KEY_LEFT or k == ord("a"):
141-
offset_x -= 1
142-
elif k == ord("A"):
143-
offset_x -= smaxcol
144-
elif k == ord("q") or k == ord("Q"):
145-
break
146-
147-
if offset_y > max_y:
148-
offset_y = max_y
149-
elif offset_y < 0:
150-
offset_y = 0
151-
152-
if offset_x > max_x:
153-
offset_x = max_x
154-
elif offset_x < 0:
155-
offset_x = 0
105+
pager = find_pager(
106+
DEFAULT_PAGER, DEFAULT_PAGER_FORMATTED, DVC_PAGER_VAR_NAME
107+
)
108+
lines = map("".join, self.canvas)
109+
joined_lines = os.linesep.join(lines)
110+
pager(joined_lines)
156111

157112
def point(self, x, y, char):
158113
"""Create a point on ASCII canvas.

dvc/system.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -218,41 +218,6 @@ def inode(path):
218218
assert inode < 2 ** 64
219219
return inode
220220

221-
@staticmethod
222-
def _wait_for_input_windows(timeout):
223-
import sys
224-
import ctypes
225-
import msvcrt
226-
from ctypes.wintypes import DWORD, HANDLE
227-
228-
# https://docs.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-waitforsingleobject
229-
from win32event import WAIT_OBJECT_0, WAIT_TIMEOUT
230-
231-
func = ctypes.windll.kernel32.WaitForSingleObject
232-
func.argtypes = [HANDLE, DWORD]
233-
func.restype = DWORD
234-
235-
rc = func(msvcrt.get_osfhandle(sys.stdin.fileno()), timeout * 1000)
236-
if rc not in [WAIT_OBJECT_0, WAIT_TIMEOUT]:
237-
raise RuntimeError(rc)
238-
239-
@staticmethod
240-
def _wait_for_input_posix(timeout):
241-
import sys
242-
import select
243-
244-
try:
245-
select.select([sys.stdin], [], [], timeout)
246-
except select.error:
247-
pass
248-
249-
@staticmethod
250-
def wait_for_input(timeout):
251-
if System.is_unix():
252-
return System._wait_for_input_posix(timeout)
253-
else:
254-
return System._wait_for_input_windows(timeout)
255-
256221
@staticmethod
257222
def is_symlink(path):
258223
path = fspath(path)

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ def run(self):
6666
"jsonpath-ng>=1.4.3",
6767
"requests>=2.22.0",
6868
"grandalf==0.6",
69-
"asciimatics>=1.10.0",
7069
"distro>=1.3.0",
7170
"appdirs>=1.4.3",
7271
"treelib>=1.5.5",

0 commit comments

Comments
 (0)