rename class TUI to App, change StdOutWrapper to a context manager and add idea of a main_loop() for the App class

This commit is contained in:
2020-01-14 08:59:13 +01:00
parent 033cb92dc4
commit d0eabd3252
7 changed files with 186 additions and 145 deletions

View File

@@ -38,96 +38,91 @@
<p>TUI module.</p>
<dl class="field-list simple">
<dt class="field-odd">Date</dt>
<dd class="field-odd"><p>2020-01-10</p>
<dd class="field-odd"><p>2020-01-13</p>
</dd>
</dl>
<span class="target" id="module-tui"></span><dl class="class">
<dt id="pylib.tui.StdOutWrapper">
<em class="property">class </em><code class="sig-name descname">StdOutWrapper</code><a class="reference internal" href="_modules/pylib/tui.html#StdOutWrapper"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.StdOutWrapper" title="Permalink to this definition"></a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
<p>Send print to stdout (print to the standard console).</p>
<p>usage:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># catch all prints into StdOutWrapper</span>
<span class="n">mystdout</span> <span class="o">=</span> <span class="n">StdOutWrapper</span><span class="p">()</span>
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span> <span class="o">=</span> <span class="n">mystdout</span>
<span class="n">sys</span><span class="o">.</span><span class="n">stderr</span> <span class="o">=</span> <span class="n">mystdout</span>
<span class="c1"># begin curses (curses.initscr())</span>
<span class="c1"># do your stuff here</span>
<span class="c1"># print(&quot;foo&quot;)</span>
<span class="c1"># you can also output mystdout.get_text() in a ncurses widget in</span>
<span class="c1"># runtime</span>
<span class="c1"># end curses (curses.endwin())</span>
<span class="c1"># go back to normal state and print all catched prints to stdout</span>
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">__stdout__</span>
<span class="n">sys</span><span class="o">.</span><span class="n">stderr</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">__stderr__</span>
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">mystdout</span><span class="o">.</span><span class="n">get_text</span><span class="p">())</span>
</pre></div>
</div>
<dl class="simple">
<dt>source:</dt><dd><p><a class="reference external" href="https://stackoverflow.com/a/14010948">https://stackoverflow.com/a/14010948</a></p>
</dd>
</dl>
<dl class="method">
<dt id="pylib.tui.StdOutWrapper.get_text">
<code class="sig-name descname">get_text</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#StdOutWrapper.get_text"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.StdOutWrapper.get_text" title="Permalink to this definition"></a></dt>
<dd></dd></dl>
<dl class="attribute">
<dt id="pylib.tui.StdOutWrapper.text">
<code class="sig-name descname">text</code><em class="property"> = ''</em><a class="headerlink" href="#pylib.tui.StdOutWrapper.text" title="Permalink to this definition"></a></dt>
<dd></dd></dl>
<dl class="method">
<dt id="pylib.tui.StdOutWrapper.write">
<code class="sig-name descname">write</code><span class="sig-paren">(</span><em class="sig-param">txt</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#StdOutWrapper.write"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.StdOutWrapper.write" title="Permalink to this definition"></a></dt>
<dd></dd></dl>
</dd></dl>
<dl class="class">
<dt id="pylib.tui.TUI">
<em class="property">class </em><code class="sig-name descname">TUI</code><span class="sig-paren">(</span><em class="sig-param">delay=5</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#TUI"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.TUI" title="Permalink to this definition"></a></dt>
<dt id="pylib.tui.App">
<em class="property">class </em><code class="sig-name descname">App</code><span class="sig-paren">(</span><em class="sig-param">delay=5</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#App"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.App" title="Permalink to this definition"></a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
<p>TUI text-based user interface</p>
<p>initscr is the encapsulation window object of the stdscr
stdsc is the curses.initscr</p>
<dl class="method">
<dt id="pylib.tui.TUI.clear">
<code class="sig-name descname">clear</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#TUI.clear"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.TUI.clear" title="Permalink to this definition"></a></dt>
<dt id="pylib.tui.App.clear">
<code class="sig-name descname">clear</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#App.clear"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.App.clear" title="Permalink to this definition"></a></dt>
<dd></dd></dl>
<dl class="method">
<dt id="pylib.tui.TUI.color_def">
<code class="sig-name descname">color_def</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#TUI.color_def"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.TUI.color_def" title="Permalink to this definition"></a></dt>
<dt id="pylib.tui.App.color_def">
<code class="sig-name descname">color_def</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#App.color_def"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.App.color_def" title="Permalink to this definition"></a></dt>
<dd></dd></dl>
<dl class="method">
<dt id="pylib.tui.TUI.color_table">
<code class="sig-name descname">color_table</code><span class="sig-paren">(</span><em class="sig-param">window=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#TUI.color_table"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.TUI.color_table" title="Permalink to this definition"></a></dt>
<dt id="pylib.tui.App.color_table">
<code class="sig-name descname">color_table</code><span class="sig-paren">(</span><em class="sig-param">window=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#App.color_table"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.App.color_table" title="Permalink to this definition"></a></dt>
<dd><p>Print all available colors with default background.
Check if curses.has_colors() is True.</p>
</dd></dl>
<dl class="method">
<dt id="pylib.tui.TUI.end">
<code class="sig-name descname">end</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#TUI.end"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.TUI.end" title="Permalink to this definition"></a></dt>
<dt id="pylib.tui.App.end">
<code class="sig-name descname">end</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#App.end"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.App.end" title="Permalink to this definition"></a></dt>
<dd><p>clean up</p>
</dd></dl>
<dl class="method">
<dt id="pylib.tui.TUI.getch">
<code class="sig-name descname">getch</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#TUI.getch"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.TUI.getch" title="Permalink to this definition"></a></dt>
<dt id="pylib.tui.App.getch">
<code class="sig-name descname">getch</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#App.getch"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.App.getch" title="Permalink to this definition"></a></dt>
<dd></dd></dl>
<dl class="method">
<dt id="pylib.tui.TUI.refresh">
<code class="sig-name descname">refresh</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#TUI.refresh"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.TUI.refresh" title="Permalink to this definition"></a></dt>
<dt id="pylib.tui.App.main_loop">
<code class="sig-name descname">main_loop</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#App.main_loop"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.App.main_loop" title="Permalink to this definition"></a></dt>
<dd></dd></dl>
<dl class="method">
<dt id="pylib.tui.App.refresh">
<code class="sig-name descname">refresh</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#App.refresh"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.App.refresh" title="Permalink to this definition"></a></dt>
<dd></dd></dl>
</dd></dl>
<dl class="class">
<dt id="pylib.tui.StdOutWrapper">
<em class="property">class </em><code class="sig-name descname">StdOutWrapper</code><a class="reference internal" href="_modules/pylib/tui.html#StdOutWrapper"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.StdOutWrapper" title="Permalink to this definition"></a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
<p>Send print to stdout (print to the standard console).</p>
<dl class="field-list simple">
<dt class="field-odd">Example</dt>
<dd class="field-odd"><p></p></dd>
</dl>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># catch all prints into StdOutWrapper</span>
<span class="k">with</span> <span class="n">StdOutWrapper</span><span class="p">()</span> <span class="k">as</span> <span class="n">mystdout</span><span class="p">:</span>
<span class="c1"># begin curses (curses.initscr(), ...)</span>
<span class="c1"># do your stuff here</span>
<span class="c1"># print(&quot;foo&quot;)</span>
<span class="c1"># you can also output mystdout.get_text() in a ncurses widget in</span>
<span class="c1"># runtime</span>
<span class="c1"># end curses (curses.endwin())</span>
<span class="c1"># go back to normal state and print all catched prints to stdout</span>
</pre></div>
</div>
<dl class="method">
<dt id="pylib.tui.StdOutWrapper.get_text">
<code class="sig-name descname">get_text</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#StdOutWrapper.get_text"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.StdOutWrapper.get_text" title="Permalink to this definition"></a></dt>
<dd></dd></dl>
<dl class="method">
<dt id="pylib.tui.StdOutWrapper.write">
<code class="sig-name descname">write</code><span class="sig-paren">(</span><em class="sig-param">txt</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/pylib/tui.html#StdOutWrapper.write"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pylib.tui.StdOutWrapper.write" title="Permalink to this definition"></a></dt>
<dd><p>print uses write()</p>
</dd></dl>
</dd></dl>
<dl class="class">