Browse Source

Curses

pull/135/head
Jure Šorn 2 years ago
parent
commit
2eeb96b189
2 changed files with 18 additions and 18 deletions
  1. 16
      README.md
  2. 20
      index.html

16
README.md

@ -2410,12 +2410,12 @@ Curses
------ ------
#### Runs a basic file explorer in the terminal: #### Runs a basic file explorer in the terminal:
```python ```python
from curses import wrapper, ascii, A_REVERSE, KEY_DOWN, KEY_UP, KEY_LEFT, KEY_RIGHT, KEY_ENTER
from os import listdir, path, chdir
import curses, curses.ascii, os
from curses import A_REVERSE, KEY_DOWN, KEY_UP, KEY_LEFT, KEY_RIGHT, KEY_ENTER
def main(screen): def main(screen):
ch, first, selected, paths = 0, 0, 0, listdir()
while ch != ascii.ESC:
ch, first, selected, paths = 0, 0, 0, os.listdir()
while ch != curses.ascii.ESC:
height, _ = screen.getmaxyx() height, _ = screen.getmaxyx()
screen.erase() screen.erase()
for y, filename in enumerate(paths[first : first+height]): for y, filename in enumerate(paths[first : first+height]):
@ -2426,12 +2426,12 @@ def main(screen):
first += (first <= selected - height) - (first > selected) first += (first <= selected - height) - (first > selected)
if ch in [KEY_LEFT, KEY_RIGHT, KEY_ENTER, 10, 13]: if ch in [KEY_LEFT, KEY_RIGHT, KEY_ENTER, 10, 13]:
new_dir = '..' if ch == KEY_LEFT else paths[selected] new_dir = '..' if ch == KEY_LEFT else paths[selected]
if path.isdir(new_dir):
chdir(new_dir)
first, selected, paths = 0, 0, listdir()
if os.path.isdir(new_dir):
os.chdir(new_dir)
first, selected, paths = 0, 0, os.listdir()
if __name__ == '__main__': if __name__ == '__main__':
wrapper(main)
curses.wrapper(main)
``` ```

20
index.html

@ -54,7 +54,7 @@
<body> <body>
<header> <header>
<aside>September 4, 2022</aside>
<aside>September 5, 2022</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a> <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header> </header>
@ -1976,12 +1976,12 @@ print(table)
</code></pre></div></div> </code></pre></div></div>
<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_DOWN, KEY_UP, KEY_LEFT, KEY_RIGHT, KEY_ENTER
<span class="hljs-keyword">from</span> os <span class="hljs-keyword">import</span> listdir, path, chdir
<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">import</span> curses, curses.ascii, os
<span class="hljs-keyword">from</span> curses <span class="hljs-keyword">import</span> A_REVERSE, KEY_DOWN, KEY_UP, KEY_LEFT, KEY_RIGHT, KEY_ENTER
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">(screen)</span>:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">(screen)</span>:</span>
ch, first, selected, paths = <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, listdir()
<span class="hljs-keyword">while</span> ch != ascii.ESC:
ch, first, selected, paths = <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, os.listdir()
<span class="hljs-keyword">while</span> ch != curses.ascii.ESC:
height, _ = screen.getmaxyx() height, _ = screen.getmaxyx()
screen.erase() screen.erase()
<span class="hljs-keyword">for</span> y, filename <span class="hljs-keyword">in</span> enumerate(paths[first : first+height]): <span class="hljs-keyword">for</span> y, filename <span class="hljs-keyword">in</span> enumerate(paths[first : first+height]):
@ -1992,12 +1992,12 @@ print(table)
first += (first &lt;= selected - height) - (first &gt; selected) first += (first &lt;= selected - height) - (first &gt; selected)
<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>]: <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>]:
new_dir = <span class="hljs-string">'..'</span> <span class="hljs-keyword">if</span> ch == KEY_LEFT <span class="hljs-keyword">else</span> paths[selected] new_dir = <span class="hljs-string">'..'</span> <span class="hljs-keyword">if</span> ch == KEY_LEFT <span class="hljs-keyword">else</span> paths[selected]
<span class="hljs-keyword">if</span> path.isdir(new_dir):
chdir(new_dir)
first, selected, paths = <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, listdir()
<span class="hljs-keyword">if</span> os.path.isdir(new_dir):
os.chdir(new_dir)
first, selected, paths = <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, os.listdir()
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>: <span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
wrapper(main)
curses.wrapper(main)
</code></pre></div></div> </code></pre></div></div>
@ -2906,7 +2906,7 @@ $ pyinstaller script.py --add-data '&lt;path&gt;:.' <span class="hljs-comment">
<footer> <footer>
<aside>September 4, 2022</aside>
<aside>September 5, 2022</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a> <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer> </footer>

Loading…
Cancel
Save