<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Posts &middot; dpc&#39;s blog</title>

  <meta name="description" content="Random posts about stuff.">

  <meta name="generator" content="Hugo 0.16" />
  <meta name="twitter:card" content="summary">
  
  <meta name="twitter:title" content="Posts &middot; dpc&#39;s blog">
  <meta name="twitter:description" content="Random posts about stuff.">

  <meta property="og:title" content="Posts &middot; dpc&#39;s blog">
  <meta property="og:type" content="blog">
  <meta property="og:description" content="Random posts about stuff.">
  

  <link href='//fonts.googleapis.com/css?family=Source+Sans+Pro:400,700|Oxygen:400,700' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pure/0.6.0/pure-min.css">
  <!--[if lte IE 8]>
      <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pure/0.6.0/grids-responsive-old-ie-min.css">
  <![endif]-->
  <!--[if gt IE 8]><!-->
      <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pure/0.6.0/grids-responsive-min.css">
  <!--<![endif]-->

  <link rel="stylesheet" href="http://dpc.pw//css/all.min.css">
  <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet">

  <link rel="alternate" type="application/rss+xml" title="dpc&#39;s blog" href="http://dpc.pw//index.xml" />
</head>
<body>


<div id="layout" class="pure-g">
  <div class="sidebar pure-u-1 pure-u-md-1-4">
  <div class="header">
    <hgroup>
      <h1 class="brand-title"><a href="http://dpc.pw/">dpc&#39;s blog</a></h1>
      <h2 class="brand-tagline">Random posts about stuff.</h2>
    </hgroup>

    <nav class="nav">
      <ul class="nav-list">
        
        
        <li class="nav-item">
          <a class="pure-button" href="http://dpc.pw//index.xml"><i class="fa fa-rss"></i> rss</a>
        </li>
      </ul>
    </nav>
  </div>
  

</div>


  <div class="content pure-u-1 pure-u-md-3-4">
    <div>
      
      <div class="posts">
        
          <h1 class="content-subhead">
  2016-10-10
  <p class="post-meta">
    
    
    
      <a class="post-tags post-tags-programming" href="http://dpc.pw//tags/programming">programming</a>
    
    
  </p>
</h1>

          <section class="post">
            <header class="post-header">

              <a href="http://dpc.pw/on-state-pollution/" class="post-title">On state pollution</a>
            </header>

            <div class="post-description">
              

<h2 id="introduction">Introduction</h2>

<p>I&rsquo;d like to share an method I&rsquo;ve developed on how to judge a code. This method
uses a metric that I named <em>state pollution</em>.</p>

<p>Note: I am not well versed in academic Computer Science nomenclature and just
trying to be precise in my explanation. Be aware that I might be wrong
somewhere and please feel free to correct me if I&rsquo;m naming something wrong or
misusing concepts. Maybe someone already described something like that (or
better). Please refer me right places if it the case.</p>

<p>First, I use <em>state</em> as a synonym of <em>entropy</em> or <em>amount of information</em>.</p>

<p><em>State pollution</em> of a fragment of a code is an amount of <em>aggregate visible state</em>
that fragment adds to the whole code.</p>

<p><em>Aggregate visible state</em> is sum of <em>visible state</em> of every executable line of
the code.</p>

<p><em>Visible state</em> of a piece/line of code is the amount of <em>state</em> this code can
access (<em>readable visible state</em>) or modify (<em>writeable visible state</em>).</p>

<p>The state is measured in bits. Eg. boolean variable can be only in two states:
<code>true</code> or <code>false</code>. This is 1 bit of information. A C-language <code>char</code> has 256
possible values, which is 8 bits.</p>

<p>The following Rust Hello World example has 0-bits of <em>aggregate visible state</em>.</p>

<pre><code class="language-rust">fn main() {
    println!(&quot;Hello world!&quot;);
}
</code></pre>

<p>The above code has only one effective line of code: <code>println!(...)</code> and that line has no variables to access or modify.</p>

<p>Now let&rsquo;s say we&rsquo;re going to add a pointless mutable <code>8-bit</code> variable:</p>

<pre><code class="language-rust">fn main() {
    let mut x = 0u8;
    println!(&quot;Hello world!&quot;);
}
</code></pre>

<p>This new line makes it potentially possible for <code>println!(...)</code> to print a text
dependent on it&rsquo;s value. So the <em>aggregate visible state</em> of the code is now <code>8-bits</code>, and
the <em>state pollution</em> introduced by new line is <code>8-bits</code>.</p>

<p>Not that if <code>mut</code> keyword was removed, <code>println!(...)</code> could potentially print
<code>x</code>, but not modify it (as variables are immutable by default in Rust). That
would cause <em>aggregate writeable visible state</em> to stay equal to 0, increasing
only <em>readable visible state</em>.</p>

<p>Note: this whole <em>entropy</em> calculation is not meant to be precise. It is to be thought of
as <a href="https://en.wikipedia.org/wiki/Big_O_notation">Big O Notation</a>. It&rsquo;s the magnitude that is important, not the exact values! To mimic <code>O(...)</code>
I&rsquo;m going to use similar <code>P(...)</code> where <em>P</em> stands for <em>pollution</em>.</p>

<p>I hope the concept is clear now. Now let&rsquo;s get to while it&rsquo;s useful. I find
<em>state pollution</em> metric to be generalization of many other more-specific ways
to judge the code, and corresponding to &ldquo;good practices&rdquo; and judgments about the
code that people develop with experience.</p>

<h2 id="why-is-using-enumerator-better-than-an-integer">Why is using enumerator better than an integer</h2>

<p>Let&rsquo;s compare:</p>

<pre><code class="language-rust">enum State {
    Open,
    Connected,
    Errored
}

state : State;
</code></pre>

<p>vs</p>

<pre><code class="language-rust">state : uint;
</code></pre>

<p><code>state</code> that is an enumerator have only 3 possible values (entropy = 1.5 bits). <code>state</code> that is just an integer,
has a lot more possible values, thus introducing more <em>state pollution</em>.</p>

<h2 id="why-are-local-values-preferable-over-globals">Why are local values preferable over globals</h2>

<p>Most programmers would agree that avoiding global values in any code is a good
practice. Using <em>state pollution</em> metric one could say that, a global variable
has a <code>P(n*e)</code> where <code>n</code> is number of lines of the whole code and <code>e</code> is a entropy of the
variable itself (1-bit for boolean, 8-bit for C-language char, etc.). In contrast
local variable has a <code>P(m*e)</code> where <code>m</code> is total number of lines of one
function.  As <code>n &gt;= m</code> using local variables instead of global is minimizing
<em>aggregate visible state pollution</em></p>

<p>Natural language explanation simple: every global variable in your code makes
every line of your code potentially do something different depending on the value
of that variable.</p>

<h2 id="why-is-static-typing-preferable-over-dynamic-typing">Why is static typing preferable over dynamic typing?</h2>

<p>In statically-typed languages <code>bool</code> can have only two values. That&rsquo;s enforced.
In dynamically-typed, every variable can potentially have any value of any
type. Variables in dynamically-typed languages introduce more <em>state
pollution</em>. Obviously code written in dynamically-typed language variables
doesn&rsquo;t always abuse that, but as a possibility - it&rsquo;s always there, in every
variable. Maybe by mistake, maybe on purpose, and change to the code could
introduce a new state. So <em>state pollution</em> is there, maybe just discounted a
little.</p>

<h2 id="why-are-immutable-variables-better-than-mutable">Why are immutable variables better than mutable</h2>

<p>The mutable state increases both <em>writeable</em> and <em>readable visible pollution</em>,
immutable only the later. With immutable state any code can potentially
experience different behavior depending on the state, but at least can&rsquo;t change
the behavior of other places that are polluted with the same state.</p>

<h2 id="why-is-encapsulation-and-abstraction-good">Why is encapsulation and abstraction good</h2>

<p>Limiting visibility of the state using eg. Go <code>interfaces</code> or Rust <code>traits</code>, or
just field and method visibility settings does not completely remove the state,
but wraps it in a surface that limits it&rsquo;s effective pollution: the code exposed to
encapsulated or abstracted state can&rsquo;t directly interfere with it.</p>

<h2 id="why-is-functional-code-preferable-over-imperative">Why is functional code preferable over imperative</h2>

<p>By nature functional code limits the state pollution. Eg. pure virtual function
can only use it&rsquo;s arguments, so that arguments are the only state that pollutes
the function code.</p>

            </div>
          </section>
        
          <h1 class="content-subhead">
  2016-09-27
  <p class="post-meta">
    
    
    
      <a class="post-tags post-tags-go" href="http://dpc.pw//tags/go">go</a><a class="post-tags post-tags-opinion" href="http://dpc.pw//tags/opinion">opinion</a><a class="post-tags post-tags-rant" href="http://dpc.pw//tags/rant">rant</a>
    
    
  </p>
</h1>

          <section class="post">
            <header class="post-header">

              <a href="http://dpc.pw/my-opinion-on-go/" class="post-title">My opinion on Go</a>
            </header>

            <div class="post-description">
              

<h2 id="tl-dr">TL;DR</h2>

<p>The biggest strength of Go, IMO, was the FAD created by the fact that it is
&ldquo;backed by Google&rdquo;. That gave Go immediate traction, and bootstrapped a
decently sized ecosystem. Everybody know about it, and have somewhat positive
attitude thinking &ldquo;it&rsquo;s simple, fast, and easy to learn&rdquo;.</p>

<p>I enjoy (crude but still) static typing, compiling to native
code, and most of all: native-green thread, making Go quite productive for
server side code. I just had to get used to many workarounds for lack of
generics, remember about avoid all the <a href="https://gist.github.com/lavalamp/4bd23295a9f32706a48f">Go landmines</a> and ignore
poor expressiveness.</p>

<p>My favourite thing about Go, is that it produces static, native binaries.
Unlike software written in Python, getting software written in Go to actually
run is always painless.</p>

<p>However overall, Go is poorly designed language full of painful archaisms. It
ignores multiple great ideas from programming languages research and other PL
experiences.</p>

<p><a href="https://news.ycombinator.com/item?id=12525949">&ldquo;Go&rsquo;s simplicity is syntactic. The complexity is in semantics and runtime
behavior.&rdquo;</a></p>

<p>Every time I write code in Go, I get the job done, but I feel deeply disappointed.</p>

<h2 id="reading-material">Reading material</h2>

<p>I think the best description of Go&rsquo;s problems was <a href="http://yager.io/programming/go.html">Why Go Is Not Good</a>.</p>

<p>Also interesting:</p>

<ul>
<li><a href="http://nomad.so/2015/03/why-gos-design-is-a-disservice-to-intelligent-programmers/">Why Go’s design is a disservice to intelligent
programmers</a>

<ul>
<li>&ldquo;In my opinion Go has been designed by people who have been using C all
their lives and don’t want to try anything new. The language could be
described as C with training wheels.&rdquo;</li>
</ul></li>
<li><a href="http://dtrace.org/blogs/ahl/2016/08/02/i-love-go-i-hate-go/">I Love Go; I Hate Go</a></li>
<li><a href="https://github.com/ksimka/go-is-not-good">Many, many articles explaining why Go is just bad</a></li>
</ul>

<h2 id="more-on-my-experiences-with-go">More on my experiences with Go</h2>

<p>While I was following Go programming language since it&rsquo;s announcement and even
did some learning and toying with it, only recently I had opportunity to try Go
in real projects. For the last couple of months I&rsquo;ve been working a lot with
Go, and below is why I think Go is simply a bad programming language.</p>

<h3 id="nil">Nil</h3>

<p><code>nil</code>/<code>null</code> should not exists in any recent programming language.</p>

<p><code>nil</code> keeps being the biggest source of frustration when working with code
written in Go. <code>nil</code> handling is inconsistent too with sometimes <code>nil</code> being OK
to use, and sometimes not so much.</p>

<h3 id="no-sane-error-handling">No sane error handling</h3>

<pre><code class="language-go">o1, err := DoSomething1()
if err != nil {
    return
}
defer Cleanup(o1)

o2, err := o2.DoSomething2()
if err != nil {
    return
}
defer Cleanup2(o2)

…

oN, err := oN-1.DoSomethingN()
if err != nil {
    return
}
defer CleanupN(oN)
</code></pre>

<p>compare the above with corresponding Rust code:</p>

<pre><code class="language-rust">let o1 = try!(DoSomething1());
let o2 = try!(o1.DoSomething2());
…
let oN = try!(oN-1.DoSomethingN());
</code></pre>

<p>or</p>

<pre><code class="language-rust">DoSomething1()
   .and_then(|o1| o1.DoSomething2())
   .and_then(|o2| o2.DoSomething2())
   …
   .and_then(|oN| oN.DoSomethingN())
</code></pre>

<p>You might ask, where are the cleanup calls. Well, because Rust has guaranteed,
predictable destructors, you don&rsquo;t need cleanup calls 99% of the time! Files
and sockets will be closed, http connections ended (or returned to the pool) etc.</p>

<p><a href="https://blog.golang.org/error-handling-and-go">Mildly-satisfying workarounds are given</a></p>

<h3 id="lack-of-generics">Lack of generics</h3>

<p>It seems to me lack of generic support is the root cause of all other problems.</p>

<p>Go has <code>nil</code> because without some form of generics, having a <code>Option</code> is not
possible.</p>

<p>Go has poor error handling, as something like Rust&rsquo;s <code>Result</code> can not be used
without generics.</p>

<p>Go has no proper collections, and you weird <code>make</code> invocations are required
because it lacks generics.</p>

<h3 id="annoyingness">Annoyingness</h3>

<p>I fiddle with the code to try something out, I get an error that something is
not used anymore. I have to remove the <code>import</code> to make the compiler happy.
Then add it again.</p>

<p>Why it can&rsquo;t be just a warning? Why is it <em>that</em> important for all the
<code>imports</code> to really be used?</p>

<p>Or compiler errors because <code>{</code> is on a newline. Madness.</p>

<p>Go compiler is happy when I don&rsquo;t check errors returned by a function (maybe by
mistake), and I have to use <code>go vet</code> and <code>golint</code> to ensure there are no real
obvious issues with my code, but it just won&rsquo;t let me be when it comes to
irrelevant details.</p>

<h3 id="small-things">Small things</h3>

<p>How do you decrement an atomic in Go?</p>

<pre><code class="language-go">AddUint32(&amp;x, ^uint32(c-1))
</code></pre>

<p>That&rsquo;s how - described right in <a href="https://golang.org/pkg/sync/atomic/#AddUint32">atomic package comment for
AddUint32</a>. Easy to read, and
self-explaining.  I mean&hellip; come on. It took similar time to write the comment
explaining how to decrement, as it would take to write a much needed function
that would just do it!</p>

<h3 id="community-doesn-t-like-me">Community doesn&rsquo;t like me</h3>

<p>Couple of times I ventured into Go community gathering places, looking for
advise on how to deal with some of the problems I was having. I don&rsquo;t know if
it&rsquo;s me, or the fact that I was referring to mechanisms from other languages,
that Go simply lacks, but every time I got hostile responses in tones of &ldquo;one
true way of Go&rdquo; and me being stupid for looking for alternatives to
established Go conventions.</p>

            </div>
          </section>
        
          <h1 class="content-subhead">
  2016-09-15
  <p class="post-meta">
    
    
    
      <a class="post-tags post-tags-shell" href="http://dpc.pw//tags/shell">shell</a>
    
    
  </p>
</h1>

          <section class="post">
            <header class="post-header">

              <a href="http://dpc.pw/precache-all-the-things/" class="post-title">Precache all the things!</a>
            </header>

            <div class="post-description">
              <p><img src="http://cdn.memegenerator.net/instances/400x/33671988.jpg" alt="funny meme" /></p>

<p>Having a lot of RAM nowadays is relatively cheap and Linux can make a good use
of it. With tools like <a href="http://en.wikipedia.org/wiki/Preload_(software)">preload</a> most of Linux distributions are
trying to proactively read things that you might want to use soon.</p>

<p>However if your desktop have a ridiculous amount of memory (mine has 32GB) it
may take ages for these tools to make use of all that memory. And why would you pay
for it and then let it just sit idle instead of working for you?</p>

<p>The thing is: you can do much better, because you know what you are going to
use in the future.</p>

<p>So, as always, let&rsquo;s write a tiny script under the name <code>precache</code>.</p>

<pre><code class="language-bash">#!/bin/sh

exec nice -n 20 ionice -c 3 find &quot;${1:-.}&quot; -xdev -type f \
	-exec nice -n 20 ionice -c 3 cat '{}' \; &gt; /dev/null
</code></pre>

<p>Personally I keep it as <code>$HOME/bin/precache</code>.</p>

<p>The basic idea is to traverse through all the subdirectories of an argument
using <code>find</code> and read all the files from the disk, discarding their output. If no
argument is given <code>precache</code> will traverse the current directory.</p>

<p>The <code>nice</code> and <code>ionice</code> commands are used to force all of this to be done only
when the system is really idle, so it&rsquo;s not slowing down anything else.</p>

<p>Keep in mind that the script will not switch to different filesystems (<code>-xdev</code>
option).</p>

<p>All of this is done to make Linux fill the free memory with cached
data so it&rsquo;s already waiting for you when you need it. You can check the
memory usage using <code>top</code> command. The <code>cached</code> position is the one that we want
to increase to the point where no memory is sitting idle.</p>

<p>How do I use this script? There are multiple ways.</p>

<p>First, you can preload your whole home directory on boot.</p>

<pre><code class="language-bash">#!/bin/sh

if [ ! -f &quot;/tmp/$USER-home-precache&quot; ]; then
	touch -f &quot;/tmp/$USER-home-precache&quot;
	precache &quot;$HOME&quot; &amp;
fi
</code></pre>

<p><a href="http://en.gentoo-wiki.com/wiki/Autostart_Programs">Add this command to your autostart on login.</a> or alternatively
just put:</p>

<pre><code>precache /home/&lt;yourusername&gt; &amp;
</code></pre>

<p>in your system&rsquo;s <code>/etc/rc.local</code>.</p>

<p>Also, when you&rsquo;re about to work on something in few minutes (like launching a
game or starting a big compilation), you can precache relevant directories to
make the game load and work faster.</p>

            </div>
          </section>
        
          <h1 class="content-subhead">
  2016-09-15
  <p class="post-meta">
    
    
    
      <a class="post-tags post-tags-shell" href="http://dpc.pw//tags/shell">shell</a><a class="post-tags post-tags-tmux" href="http://dpc.pw//tags/tmux">tmux</a>
    
    
  </p>
</h1>

          <section class="post">
            <header class="post-header">

              <a href="http://dpc.pw/make-current-dir-a-tmux-session-placeholder./" class="post-title">Make current dir a tmux session placeholder.</a>
            </header>

            <div class="post-description">
              <p><code>tmux-session.sh</code>:</p>

<pre><code class="language-bash">#!/bin/bash
# Reattach to (or spawn new if not existing) tmux session
# tmux session &lt;session_name&gt; [ &lt;session_directory&gt; ]

export STY=&quot;tmux-$1&quot;
RC=&quot;.tmux&quot;
if [ ! -z &quot;$2&quot; ]; then
	RC=&quot;$2/$RC&quot;
fi

RC=&quot;$(readlink -f &quot;$RC&quot;)&quot;

if ! tmux has-session -t &quot;$1&quot; 2&gt;/dev/null ; then
	if [ ! -z &quot;$RC&quot; -a -f &quot;$RC&quot; ] ; then
		tmux new-session -d -s &quot;$1&quot; &quot;tmux move-window -t 9; exec tmux source-file \&quot;$RC\&quot;&quot;
	else
		tmux new-session -d -s &quot;$1&quot;
	fi
fi

exec tmux attach-session -t &quot;$1&quot;
</code></pre>

<p><code>tmux-here.sh</code>:</p>

<pre><code class="language-bash">#!/bin/bash
# Spawn tmux session in current directory
# use path's sha256 hash as session name

exec &quot;$HOME/bin/tmux-session&quot; &quot;$(echo &quot;$PWD&quot; | sha256sum | awk '{ print $1 }')&quot; &quot;$PWD&quot;
</code></pre>

            </div>
          </section>
        
          <h1 class="content-subhead">
  2016-09-15
  <p class="post-meta">
    
    
    
      <a class="post-tags post-tags-shell" href="http://dpc.pw//tags/shell">shell</a>
    
    
  </p>
</h1>

          <section class="post">
            <header class="post-header">

              <a href="http://dpc.pw/shell-tip-retry.sh/" class="post-title">Shell tip: `retry.sh`</a>
            </header>

            <div class="post-description">
              <p>It&rsquo;s often the case when I have a command you want to retry until it&rsquo;s successful.</p>

<p>It&rsquo;s useful to have <code>retry.sh</code> script like this:</p>

<pre><code class="language-bash">#!/bin/sh

while ! &quot;$@&quot;; do sleep 1; done
</code></pre>

            </div>
          </section>
        
          <h1 class="content-subhead">
  2016-01-15
  <p class="post-meta">
    
    
    
      <a class="post-tags post-tags-shell" href="http://dpc.pw//tags/shell">shell</a>
    
    
  </p>
</h1>

          <section class="post">
            <header class="post-header">

              <a href="http://dpc.pw/prepend-or-append-to-path-like-environment-variable./" class="post-title">Prepend or append to PATH like environment variable.</a>
            </header>

            <div class="post-description">
              <p>In Unix there are quite a lot variables representing path lists of different kind similar to <code>PATH</code> like <code>LD_LIBRARY_PATH</code>, <code>PKG_CONFIG_PATH</code>.</p>

<p>Usual idiom to modify these variables is:</p>

<pre><code>$PATH=&quot;$PATH:/new/path/to/something&quot;
</code></pre>

<p>I found it quite a lot of typing in a daily work, so I&rsquo;m using functions shortening the above to just:</p>

<pre><code>append_env PATH /new/path/to/something
</code></pre>

<p>The version for Bash is:</p>

<pre><code>function append_env {
    if [ -z &quot;${!1}&quot; ]; then
        export &quot;$1&quot;=&quot;$2&quot;
    else
        export &quot;$1&quot;=&quot;${!1}:$2&quot;
    fi
}
function prepend_env {
    if [ -z &quot;${!1}&quot; ]; then
        export &quot;$1&quot;=&quot;$2&quot;
    else
        export &quot;$1&quot;=&quot;$2:${!1}&quot;
    fi
}
</code></pre>

<p>And for Zsh:</p>

<pre><code>function append_env {
    eval &quot;local v=\$$1&quot;
    if [ -z &quot;$v&quot; ]; then
        export &quot;$1&quot;=&quot;$2&quot;
    else
        export &quot;$1&quot;=&quot;$v:$2&quot;
    fi
}
function prepend_env {
    eval &quot;local v=\$$1&quot;
    if [ -z &quot;$v&quot; ]; then
        export &quot;$1&quot;=&quot;$2&quot;
    else
        export &quot;$1&quot;=&quot;$2:$v&quot;
    fi
}
</code></pre>

            </div>
          </section>
        
          <h1 class="content-subhead">
  2014-09-15
  <p class="post-meta">
    
    
    
      <a class="post-tags post-tags-shell" href="http://dpc.pw//tags/shell">shell</a><a class="post-tags post-tags-c" href="http://dpc.pw//tags/c">c</a>
    
    
  </p>
</h1>

          <section class="post">
            <header class="post-header">

              <a href="http://dpc.pw/asynchronous-gnu-readline-printing/" class="post-title">Asynchronous GNU Readline printing</a>
            </header>

            <div class="post-description">
              <p>Some while ago I&rsquo;ve spend my time developing a <a href="http://github.com/dpc/xmppconsole">XMPP command line client</a> which is using <a href="http://code.stanziq.com/strophe/">strophe XMPP library</a>
to handle XMPP and <a href="http://tiswww.case.edu/php/chet/readline/rltop.html">GNU Readline</a> for I/O.</p>

<p>The idea was to have a readline prompt at the bottom and yet be able to asynchronously print incoming messages above it - in the &ldquo;log window&rdquo;.</p>

<p>It seems that many people were looking for solution already:</p>

<ul>
<li><a href="http://stackoverflow.com/questions/1512028/gnu-readline-how-do-clear-the-input-line">GNU Readline: how do clear the input line?</a></li>
<li><a href="http://stackoverflow.com/questions/691652/using-gnu-readline-how-can-i-add-ncurses-in-the-same-program">Using GNU Readline; how can I add ncurses in the same program?</a></li>
</ul>

<p>I haven&rsquo;t found any satisfying answer on the web so I&rsquo;d like to present my own solution.</p>

<p>Basic idea is to use alternate (asynchronous) GNU Readline interface and on each new asynchronous print:</p>

<ul>
<li>save a copy of current line state</li>
<li>clear both prompt and current line (content + position)</li>
<li>force screen update</li>
<li>print asynchronous event (followed by a newline)</li>
<li>restore prompt and current line state</li>
<li>force screen update</li>
</ul>

<p>Simple it is, indeed and you can see <a href="http://github.com/dpc/xmppconsole/blob/master/src/io.c">a working code</a> if you don&rsquo;t believe.</p>

<p>The only thing that I was unable to get is preventing putting the original confirmed readline buffer in &ldquo;log window&rdquo;. As this is not a big deal for my requirements the complete and universal solution would be able to change what the user typed in the readline buffer just before it&rsquo;s getting scrolled up and becoming part of the &ldquo;log window&rdquo;.</p>

<p>I hope someone will fine it useful, like I do.</p>

<p>Update:</p>

<p>Below is a patch by Denis Linvinus:</p>

<p><a href="http://pastebin.com/SA87Lxqq">http://pastebin.com/SA87Lxqq</a></p>

<p>that he uses to get more &ldquo;unrestricted log window with a prompt&rdquo; for his project and wanted me to share with you too. Generally, I think it&rsquo;s a bit unclean because of using vt100 escape sequences so I&rsquo;m not going to merge it, but if anyone finds it useful, it&rsquo;s good for everyone.</p>

            </div>
          </section>
        
      </div>
      

      <div class="footer">
  <div class="pure-menu pure-menu-horizontal pure-menu-open">
    <ul>
      <li>Powered by <a class="hugo" href="http://hugo.spf13.com/" target="_blank">hugo</a></li>
    </ul>
  </div>
</div>
<script src="http://dpc.pw//js/all.min.js"></script>

    </div>
  </div>
</div>


</body>
</html>
