May 17, 2016

Console progress bar

It's very simple:

import sys
def update_progress(iteration, total):
    percent = int(iteration * 100.0 / total)
    sys.stdout.write(
        "\r[ {0:<51} ] {1}% ~{2}".format(
            '#' * (percent / 2), percent, iteration
        )
   )

The main trick is character "\r", which clear previous line.

May 5, 2016

Linux installation

2 years using OSX gave me more insight on how Unix system work more than before. This time for new home's desktop, I decided to use a Linux.

I chosen Debian testing with XFCE

Why testing? It has latest piece of software. Seems to unstable but it works. There is a good alternative are Mint, elementaryOS, good-looking but buggy, and security faults are largely ignored www.techrepublic.com/article/why-the-linux-mint-hack-is-an-indicator-of-a-larger-problem/

XFCE gives many customization chance and very, very fast! Resources usage is also optimized.



Default fonts is unpleasant, especially for Japanese text. To have above result, there is a fix for it by setting fallback chain in /etc/fonts/local.conf

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<alias>
<family>serif</family>
<prefer>
<family>Tahoma</family>
<family>TakaoPGothic</family>
</prefer>
</alias>
<alias>
<family>sans-serif</family>
<prefer>
<family>Tahoma</family>
<family>TakaoPGothic</family>
</prefer>
</alias>
<alias>
<family>monospace</family>
<prefer>
<family>Monaco</family>
<family>TakaoPGothic</family>
</prefer>
</alias>
<match target="font">
<edit mode="assign" name="hinting">
<bool>true</bool>
</edit>
</match>
<match target="font">
<edit mode="assign" name="autohint">
<bool>true</bool>
</edit>
</match>
<match target="font">
<edit mode="assign" name="hintstyle">
<const>hintslight</const>
</edit>
</match>
<match target="font">
<edit mode="assign" name="rgba">
<const>rgb</const>
</edit>
</match>
<match target="font">
<edit mode="assign" name="antialias">
<bool>true</bool>
</edit>
</match>
<match target="font">
<edit mode="assign" name="lcdfilter">
<const>lcddefault</const>
</edit>
</match>
</fontconfig>
Here I used Tahoma from Windows and Monaco from OSX, but Open Sans and Ubuntu Mono are good alternatives. Takao font is an IPA font derivatives, installed by apt-get install fonts-takao*

Font Osaka causing ugly rendering on many latin characters, better to remove it
sudo rm /usr/share/fonts/truetype/Osaka* -f

It's easy to set system wide shortcuts. There are 2 places:
* Window manager: pre-defined functions
* Keyboard setting: a function is a command





About input methods, there is "uim", better than D-bus and Scim etc I have used.

For Japanese I use Mozc. It's important to set "Default input mode" to Hiragana to make it work as expected.

For Vietnamese I use m17nlib. Add "m17n-vi-telex" to Enabled input methods. It just works.

apt-get install  uim-mozc uim-m17nlib uim-gtk2.0 uim-gtk3




Hope that using it daily can boost Linux skill :D

Additional programs:

apt-get install abiword gnumeric gnome-dictionary gnote systemd-ui keepass2 gufw caffeine ocrfeeder 

* Terminal with copy on selection
* http://www.linuxnov.com/the-complete-guide-to-configure-terminator-terminal-emulator-layouts/
* Valentina Studio - all in one MySQL, Postgres, MS SQL Server GUI
* komodo-edit - Generic editor
* pycharm - Python IDE
* meld - Diff / merge
* systemd-ui
* Pinta - Image editor
* Synapse - launcher
* keepassx
* gufw - Firewall GUI
* plank - a dock
* caffeine
* ocrfeeder - OCR
* gnome-dictionary
* Abiword
* Gnumeric
* gnote

Apr 2, 2016

RiotJS reading

http://www.ersinakinci.com/riot-js-gotchas/
http://happy-css.com/lessons/riotjs/
http://riotgear.js.org/
http://ricostacruz.com/cheatsheets/riot.html

Jan 5, 2016

PEP8 Fix on save

komodo.assertMacroVersion(3);
if (komodo.view.scintilla) {
    komodo.view.scintilla.focus();
} // bug 67103
var koDoc = (komodo.koDoc === undefined ? komodo.document : komodo.koDoc);
var formatter = null;
var onlyWholeFile = false;

switch (koDoc.language) {
    case 'Python':
    case 'Python3':
        formatter = 'autopep8 --aggressive -';
        break;
}

// Save cursor Position
try {
    if (formatter) {
        // Group operations in a single undo
        // Select Buffer, pipe it into formatter.
        var wholeFile = onlyWholeFile || komodo.editor.selText == '';
        var currentPos = komodo.editor.currentPos;
        komodo.editor.beginUndoAction();
        if (wholeFile) {
            komodo.doCommand('cmd_selectAll');
        }
        ko.run.runEncodedCommand(window, formatter + " {'insertOutput': True, 'operateOnSelection': True}");
        komodo.editor.gotoPos(currentPos);
    }
} catch (e) {
    alert(e);
}
finally {
    komodo.editor.endUndoAction();
}

Nov 30, 2015

Auto format code in Komodo

This is a macro in Javascript to align, fixing and make code auto-beautified. Tested with Komodo Edit 8/9.
/*
Install dependencies:

  pip install --upgrade autopep8
  pip3 install --upgrade autopep8
  brew install astyle
  brew install tidy-html5
  brew install homebrew/php/php-cs-fixer
  gem install ruby-beautify

*/
komodo.assertMacroVersion(3);
if (komodo.view.scintilla) {
    komodo.view.scintilla.focus();
} // bug 67103
var koDoc = (komodo.koDoc === undefined ? komodo.document : komodo.koDoc);
var formatter;
var language = koDoc.language;
var cannot_tidy_selection = false;

switch (language) {
case 'C#':
    cannot_tidy_selection = true;
    formatter = 'astyle --style=ansi --mode=cs --convert-tabs --indent=spaces=4 %F > /dev/null 2>&1; cat %F';
    break;
case 'C++':
    cannot_tidy_selection = true;
    formatter = 'astyle --style=linux --mode=c --convert-tabs --indent=spaces=4 %F > /dev/null 2>&1; cat %F';
    break;
case 'CSS':
    formatter = 'csstidy - --preserve_css=true --lowercase_s=true --case_properties=true --sort_properties=true --remove_bslash=false --silent=true --template=medium';
    break;
case 'HTML5':
case 'HTML':
    cannot_tidy_selection = true;
    formatter = 'tidy -q -i -b -c -w 120 --show-warnings no --show-errors 0 --tidy-mark no --css-prefix block --drop-proprietary-attributes yes --anchor-as-name no --enclose-text yes';
    break;
case 'Java':
    cannot_tidy_selection = true;
    formatter = 'astyle --style=java --mode=java --convert-tabs --indent=spaces=4 %F > /dev/null 2>&1; cat %F';
    break;
case 'Perl':
    formatter = 'perltidy';
    break;
case 'PHP':
    cannot_tidy_selection = true;
    formatter = 'php php-cs-fixer.phar fix %F';
    break;
case 'Python':
case 'Python3':
    cannot_tidy_selection = true;
    formatter = 'autopep8 --in-place --aggressive %F';
    break;
case 'Ruby':
    formatter = 'rbeautify.rb -';
    break;
case 'XSLT':
    cannot_tidy_selection = true;
    formatter = 'tidy -q -xml -i -w 120 --show-warnings no --show-errors 0 --tidy-mark no';
    break;
case 'XML':
    cannot_tidy_selection = true;
    formatter = 'xmllint --format --recover -';
    break;
default:
    alert("Syntax Undefined, Add Case to Macro " + language);
    return null;
}

// Save cursor Position
var currentPos = komodo.editor.currentPos;
try {
    // Save the file, Check Changes with "File -> Show Unsaved Changes"
    komodo.doCommand('cmd_save');
    // Group operations in a single undo
    // Select Buffer, pipe it into formatter.
    var text_not_selected = cannot_tidy_selection || komodo.editor.selText == "";
    if (text_not_selected) {
        komodo.doCommand('cmd_selectAll');
        ko.run.runEncodedCommand(window, formatter + " {'insertOutput': True}");
    } else {
        ko.run.runEncodedCommand(window, formatter + " {'insertOutput': True, 'operateOnSelection': True}");
    }
    komodo.doCommand('cmd_cleanLineEndings');
    if (text_not_selected) {
        // Reload file
        var view = ko.views.manager.currentView;
        if (view) {
            // Ref: http://docs.activestate.com/komodo/9.0/macroapi.html#macroapi_file
            var currentURI = koDoc.file.URI,
                currentLine = view.scimoz.lineFromPosition(komodo.editor.currentPos);
            // now close and re-open
            view.closeUnconditionally(); // or close()
            ko.open.URI(currentURI + "#" + currentLine);
        }
    }
    StatusBar_AddMessage("Formatted");
}
catch (e) {
    alert(e);
}
finally {
    // End Undo Action to Avoid Edit Buffer Corruption
    // komodo.editor.endUndoAction();
    return true;
}

Sep 6, 2015

Go language in weekend

I'm curious about Go language. I found it more like Python than C, such as import, len, multiple assignment...

Very good resources on it:
http://openmymind.net/The-Little-Go-Book/

https://www.gitbook.com/book/codegangsta/building-web-apps-with-go/details