|
335 | 335 |
|
336 | 336 |
|
337 | 337 | <div><h2 id="tuple"><a href="#tuple" name="tuple">#</a>Tuple</h2><p><strong>Tuple is an immutable and hashable list.</strong></p><pre><code class="python language-python hljs"><tuple> = ()
|
338 |
| -<tuple> = (<el>,) |
339 |
| -<tuple> = (<el_1>, <el_2> [, ...]) |
| 338 | +<tuple> = (<el>,) <span class="hljs-comment"># Or: <el>,</span> |
| 339 | +<tuple> = (<el_1>, <el_2> [, ...]) <span class="hljs-comment"># Or: <el_1>, <el_2> [, ...]</span> |
340 | 340 | </code></pre></div>
|
341 | 341 |
|
342 | 342 |
|
|
2078 | 2078 | </code></pre></div></div>
|
2079 | 2079 |
|
2080 | 2080 |
|
2081 |
| -<div><h2 id="curses"><a href="#curses" name="curses">#</a>Curses</h2><div><h4 id="clearstheterminalprintsamessageandwaitsfortheesckeypress">Clears the terminal, prints a message and waits for the ESC key press:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> curses <span class="hljs-keyword">import</span> wrapper, curs_set, ascii |
2082 |
| -<span class="hljs-keyword">from</span> curses <span class="hljs-keyword">import</span> KEY_UP, KEY_RIGHT, KEY_DOWN, KEY_LEFT |
| 2081 | +<div><h2 id="curses"><a href="#curses" name="curses">#</a>Curses</h2><div><h4 id="runsabasicfileexplorerintheterminal">Runs a basic file explorer in the terminal:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> curses <span class="hljs-keyword">import</span> wrapper, ascii, A_REVERSE, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_ENTER |
| 2082 | +<span class="hljs-keyword">from</span> os <span class="hljs-keyword">import</span> listdir, chdir, path |
2083 | 2083 |
|
2084 |
| -<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span> |
2085 |
| - wrapper(draw) |
2086 |
| - |
2087 |
| -<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">draw</span><span class="hljs-params">(screen)</span>:</span> |
2088 |
| - curs_set(<span class="hljs-number">0</span>) <span class="hljs-comment"># Makes cursor invisible.</span> |
2089 |
| - screen.nodelay(<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Makes getch() non-blocking.</span> |
2090 |
| - screen.clear() |
2091 |
| - screen.addstr(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-string">'Press ESC to quit.'</span>) <span class="hljs-comment"># Coordinates are y, x.</span> |
2092 |
| - <span class="hljs-keyword">while</span> screen.getch() != ascii.ESC: |
2093 |
| - <span class="hljs-keyword">pass</span> |
2094 |
| - |
2095 |
| -<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_border</span><span class="hljs-params">(screen)</span>:</span> |
2096 |
| - <span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> namedtuple |
2097 |
| - P = namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>) |
2098 |
| - height, width = screen.getmaxyx() |
2099 |
| - <span class="hljs-keyword">return</span> P(width<span class="hljs-number">-1</span>, height<span class="hljs-number">-1</span>) |
| 2084 | +<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">(screen)</span>:</span> |
| 2085 | + ch, first, selected, paths = <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, listdir() |
| 2086 | + <span class="hljs-keyword">while</span> ch != ascii.ESC: |
| 2087 | + height, _ = screen.getmaxyx() |
| 2088 | + screen.clear() |
| 2089 | + <span class="hljs-keyword">for</span> y, path_ <span class="hljs-keyword">in</span> enumerate(paths[first : first+height]): |
| 2090 | + screen.addstr(y, <span class="hljs-number">0</span>, path_, A_REVERSE * (selected == first + y)) |
| 2091 | + ch = screen.getch() |
| 2092 | + selected += (ch == KEY_DOWN) - (ch == KEY_UP) |
| 2093 | + selected = max(<span class="hljs-number">0</span>, min(len(paths)<span class="hljs-number">-1</span>, selected)) |
| 2094 | + first += (first <= selected - height) - (first > selected) |
| 2095 | + <span class="hljs-keyword">if</span> ch <span class="hljs-keyword">in</span> [KEY_LEFT, KEY_RIGHT, KEY_ENTER, <span class="hljs-number">10</span>, <span class="hljs-number">13</span>]: |
| 2096 | + new_dir = <span class="hljs-string">'..'</span> <span class="hljs-keyword">if</span> ch == KEY_LEFT <span class="hljs-keyword">else</span> paths[selected] |
| 2097 | + <span class="hljs-keyword">if</span> path.isdir(new_dir): |
| 2098 | + chdir(new_dir) |
| 2099 | + first, selected, paths = <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, listdir() |
2100 | 2100 |
|
2101 | 2101 | <span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
|
2102 |
| - main() |
| 2102 | + wrapper(main) |
2103 | 2103 | </code></pre></div></div>
|
2104 | 2104 |
|
2105 | 2105 |
|
|
2975 | 2975 | <span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> defaultdict, namedtuple
|
2976 | 2976 | <span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> make_dataclass
|
2977 | 2977 | <span class="hljs-keyword">from</span> enum <span class="hljs-keyword">import</span> Enum
|
2978 |
| -<span class="hljs-keyword">import</span> re, operator <span class="hljs-keyword">as</span> op, itertools <span class="hljs-keyword">as</span> it, functools <span class="hljs-keyword">as</span> ft |
| 2978 | +<span class="hljs-keyword">import</span> functools <span class="hljs-keyword">as</span> ft, itertools <span class="hljs-keyword">as</span> it, operator <span class="hljs-keyword">as</span> op, re |
2979 | 2979 |
|
2980 | 2980 |
|
2981 | 2981 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
|
|
0 commit comments