Search This Blog

Tuesday, October 29, 2013

Python cheat sheet

Programming
  • How to pretty print JSON record
echo '{"name":"value"}' | python -mjson.tool


Sunday, October 27, 2013

Home directory and dotfiles management

In Linux you can customize your environment by creating custom aliases and various scripts. After some time this flexibility becomes difficult to managed if you work on many different machines. And if some of the hosts are cloud servers that are being deleted and recreated setting up your home directory can become a boring and annoying task.

Problem

How to customize and maintain your home directory config files on many servers.

Solution
  1. Create a repository for your configs on github. You can track your config files in a repository like mine: dotfiles on github. There are many existing repositories that that you can clone or simply reuse.
  2. On the server/cloud server install https://github.com/andsens/homeshick. This bash script will help us to maintain the config files.
  3. Download your dotfiles (config files) repository on the server
  4. Install it in your home directory
  5. Add the customization to .bashrc file

References

https://github.com/rtomaszewski/dotfiles
https://github.com/andsens/homeshick
http://dotfiles.github.io/

Tuesday, October 8, 2013

How to dynamically inspect and manipulate the content of your loaded page inside chrome browser

The next generation browsers, like Google Chrome, can offer an powerful feature (Chrome DevTools) that allows you to inspect, debug and manipulate any web page element that you browse.

Demonstration and results description
  • Open any web page in your browser
  • Open the Chrome DevTools console tab: Ctrl+Shift+J 
  • In the console type the following code and press Enter
document.body.innerHTML="this is now your new page"
"this is now your new page"
  • Your loaded page within the browser window should be replaced now with our custom string
  • By running different JavaScript code you can change the behavior and content of your page
Now to start exploring this all what you need to know is basic JavaScript knowledge and some understanding of the HTML standard (a good reference can be found here HTML - Living Standard).

References

http://www.whatwg.org/specs/web-apps/current-work/multipage/
https://developers.google.com/chrome-developer-tools/
http://www.w3schools.com/js/js_htmldom_navigation.asp



Thursday, October 3, 2013

Beautiful code formatter for JavaScript in Sublime

I needed recently to work with some JavaScript code to understand how bookmarklets work in Google Chrome. Unfortunately the js code was often not well formatted and difficult to read.

Problem

How to automatically reformat JavaScript code so it looks beautiful and is easy to read.

Solution

I recommend this online site if you don't need to have this functionality in your editor of choice: http://jsbeautifier.org/. To enable beautiful code formatting in Sublime you can:
Testing

To test that the node.js and the necessary plugin is installed correctly you can run the node virtual machine and try to import the module:
 
$ node
> require("js-beautify")
{ [Function]
  js: [Function: js_beautify],
  css: [Function: css_beautify],
  html: [Function],
  js_beautify: [Function: js_beautify],
  css_beautify: [Function: css_beautify],
  html_beautify: [Function] }
>

To test that the plugin is installed successfully in in sublime you can:
  • check sublime tree directory
  •  
     ... Sublime2\Data\Packages\CodeFormatter\
     
To trigger the formatting:
  • Menu tools -> Command Palette (Cmd+Shift+P or Ctrl+Shift+P) and type `Format`.
  • The default keyboard shortcat is  ["ctrl+alt+f"] or 
  • If you want to run the command from the console
  •  
     view.run_command("code_formatter")
     
References

https://sublime.wbond.net/packages/CodeFormatter
http://nodejs.org/
https://npmjs.org/package/js-beautify
http://jsbeautifier.org/

For further info about node.js and the JavaScript virtual machine functionality

http://www.nodebeginner.org/#hello-world
http://stackoverflow.com/questions/2353818/how-do-i-get-started-with-node-js

Tuesday, October 1, 2013

Sublime macro error

Macros are a very power full tools when working with text editors. If you repeatedly typing the same commands/characters to edit your text you can always try to capture them and save as a macro.

Problem

A macro that tries to capture the ALT+F3 keyboard shortcat to highly all the word occurrence in text (more about this here) doesn't work and is erroring out on the console with this message.
 
Unknown macro command find_all_under

Troubleshooting

You can reproduce this by trying these steps:
  • Create a new file with this content.
  •  
    Line : aaa
    comment1
    comment2
    comment3
    
    Line : bbb
    comment4
    comment5
    

  • Select menu tools -> record macro in sublime.
  • Highlight the ":" char and press ALT-F3 to mark two lines.
  • Stop macro and save it.
The file should look similar to this one:
 
[
 {
  "args":
  {
   "by": "characters",
   "extend": true,
   "forward": true
  },
  "command": "move"
 },
 {
  "args": null,
  "command": "find_all_under"
 }
]

When you try to execute this macro it doesn't work and you can see the error message in the console window.

Analysis and solution

The command find_all_under is documented and works fine. You can always try to run it manually from the console yourself.
 
view.window().run_command("find_all_under")

To create the automatic solution and let the editor to do the work for me I decided to write a Plugin using Sublime Python API. The code works perfectly fine and can be found on my github account: https://github.com/rtomaszewski/sublime under the file device_list.py. All what you need to do is to install this plugin and execute it manually or use a keyboard shortcat to trigger it.

Workaround example:
 
view.run_command("device_list")

References

Sublime Python API
http://www.sublimetext.com/docs/2/api_reference.html
http://docs.sublimetext.info/en/latest/reference/plugins.html

Commands
http://docs.sublimetext.info/en/latest/reference/commands.html
http://www.sublimetext.com/docs/commands
http://docs.sublimetext.info/en/latest/extensibility/commands.html

Debugging
http://www.sublimetext.com/forum/viewtopic.php?f=6&t=10106
http://sublimetext.userecho.com/topic/114638-ctrlshiftt-does-not-work/
http://www.sublimetext.com/forum/viewtopic.php?f=6&t=4961