|
2 | 2 | from __future__ import print_function |
3 | 3 | from __future__ import unicode_literals |
4 | 4 |
|
| 5 | +import logging |
5 | 6 | import math |
| 7 | +import os |
| 8 | +import pydoc |
6 | 9 | import sys |
7 | 10 |
|
8 | 11 | from grandalf.graphs import Edge |
|
12 | 15 | from grandalf.routing import EdgeViewer |
13 | 16 | from grandalf.routing import route_with_lines |
14 | 17 |
|
| 18 | +from dvc.env import DVC_PAGER |
| 19 | + |
| 20 | + |
| 21 | +logger = logging.getLogger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +DEFAULT_PAGER = "less" |
| 25 | +DEFAULT_PAGER_FORMATTED = "{} --chop-long-lines --clear-screen".format( |
| 26 | + DEFAULT_PAGER |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +def make_pager(cmd): |
| 31 | + def pager(text): |
| 32 | + return pydoc.tempfilepager(pydoc.plain(text), cmd) |
| 33 | + |
| 34 | + return pager |
| 35 | + |
| 36 | + |
| 37 | +def find_pager(): |
| 38 | + if not sys.stdout.isatty(): |
| 39 | + return pydoc.plainpager |
| 40 | + |
| 41 | + env_pager = os.getenv(DVC_PAGER) |
| 42 | + if env_pager: |
| 43 | + return make_pager(env_pager) |
| 44 | + |
| 45 | + if os.system("({}) 2>{}".format(DEFAULT_PAGER, os.devnull)) == 0: |
| 46 | + return make_pager(DEFAULT_PAGER_FORMATTED) |
| 47 | + |
| 48 | + logger.warning( |
| 49 | + "Unable to find `less` in the PATH. Check out " |
| 50 | + "man.dvc.org/doc/command-reference/pipeline/show for more info." |
| 51 | + ) |
| 52 | + return pydoc.plainpager |
| 53 | + |
15 | 54 |
|
16 | 55 | class VertexViewer(object): |
17 | 56 | """Class to define vertex box boundaries that will be accounted for during |
@@ -60,99 +99,10 @@ def __init__(self, cols, lines): |
60 | 99 |
|
61 | 100 | def draw(self): |
62 | 101 | """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 |
| 102 | + pager = find_pager() |
| 103 | + lines = map("".join, self.canvas) |
| 104 | + joined_lines = os.linesep.join(lines) |
| 105 | + pager(joined_lines) |
156 | 106 |
|
157 | 107 | def point(self, x, y, char): |
158 | 108 | """Create a point on ASCII canvas. |
|
0 commit comments