|
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
|
| 10 | +from distutils.spawn import find_executable |
7 | 11 |
|
8 | 12 | from grandalf.graphs import Edge
|
9 | 13 | from grandalf.graphs import Graph
|
|
12 | 16 | from grandalf.routing import EdgeViewer
|
13 | 17 | from grandalf.routing import route_with_lines
|
14 | 18 |
|
| 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 | + |
15 | 57 |
|
16 | 58 | class VertexViewer(object):
|
17 | 59 | """Class to define vertex box boundaries that will be accounted for during
|
@@ -60,99 +102,12 @@ def __init__(self, cols, lines):
|
60 | 102 |
|
61 | 103 | def draw(self):
|
62 | 104 | """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) |
156 | 111 |
|
157 | 112 | def point(self, x, y, char):
|
158 | 113 | """Create a point on ASCII canvas.
|
|
0 commit comments