Μηνιαία αρχεία: Απρίλιος 2016


Git: Delete all local branches

The following command will:

  • print all branches that were merged to master
  • then filter out the branch named master and the branch you are currently switched to
  • and finally, it will delete the rest (one branch at a time).
git branch --merged master | grep -v -e "\*" -e "master" | xargs git branch -D

Tip:

To cleanup any remote-tracking references that no longer exist on the remote use the following:

git fetch --prune

Bash/FFMPEG: Batch resize .mp4 videos to fixed resolution 2

We needed to shrink a bunch of mp4 videos so that they would have the same size as the screen of an android device.
We did that both to save space on the internal memory of the device and to make the device perform as efficient as possible as it would not have to shrink the video on the fly.

The command we used was the following:

find . -type f -name "*.mp4" -exec bash -c 'FILE="$1"; ffmpeg -i "${FILE}" -s 1280x720 -acodec copy -y "${FILE%.mp4}.shrink.mp4";' _ '{}' \;

What this command does is the following:

  • Find all files in current folder (and sub-folders) that have the extension .mp4
  • For each file, create a new bash instance in which it will call ffmpeg taking as first parameter the filename that matched
  • -i "${FILE}"ffmpeg will take as input the filename we matched
  • -s 1280x720 – Then change the video size to 1280x720
  • -acodec copy – It will keep the audio as is
  • -y "${FILE%.mp4}.shrink.mp4 – Finally, create a new file (or overwrite existing) that has the extension .shrink.mp4 in the same folder

PHP: Convert JavaScript-escaped Unicode characters to HTML hex references

There are cases where one might receive in PHP, escaped Unicode characters from the client side JavaScript. According to the RFC it is normal for JavaScript to convert characters to that format and in effect that we receive any character in the escaped format of \uXXXX in PHP.

Any character may be escaped.
If the character is in the Basic Multilingual Plane (U+0000 through U+FFFF),
then it may be represented as a six-character sequence:
a reverse solidus, followed by the lowercase letter u, followed by four hexadecimal digits that encode the character's code point.
The hexadecimal letters A though F can be upper or lowercase.

A sample input you might receive could look like this George\u2019s treasure box instead of George’s treasure box.

This kind of input should not be stored as is as it does not make sense to the HTML language, instead we should fix it up using preg_replace.

$decoded = preg_replace('/\\\\u([a-fA-F0-9]{4})/', '&#x\\1;', $input);

The above command will look for all instances of \uXXXX in the $input and it will replace each one with the appropriate character using the XXXX value that it will match.

What this part '/\\\\u([a-fA-F0-9]{4})/' of the code do is the following:

  • \\\\ – Find the character \ in the string, the reason we have four \ instead of one, is because it has special meaning in the regular expression and we have to escape it. For that reason we need to use two of them and get \\. After that, we need to escape each of them again due to the special meaning they have in PHP and we end up with four of them.
  • u – The previous step must be followed by a u character.
  • ([a-fA-F0-9]{4}) – After the previous step has matched, we need to match 4 characters. Each of them must be either a character from A-Z or a-z or 0-9.

This part '&#x\\1;' will:

  • &#x – Is a constant string that will print the characters &#x. These characters will instruct HTML to print the character that will occur using hexadecimal entity reference that will follow.
  • \\1 – Contains the reference of the 1st parenthesized pattern. In this case we only have a parenthesis around the XXXX part of the \uXXXX so \\1 will be replaced with the XXXX value.

HOWTO: Make Terminator Terminal Act Like Guake Terminal in Ubuntu 16.04 LTS (The easy ways) 3

First way to make terminator toggle its visibility using the F12 key (like guake)

  • Start terminator
  • Right click anywhere in the terminal area and click on the Preferences option

terminator-01

  • In the new window, click on the Keybindings tab and scroll down until you find the line that has the following information:
    Name : hide_window
    Action : Toggle window visibility

terminator-02

  • Click on the Keybinding column (3rd column), the value will change to New accelerator..., hit the key combination you want to be used to toggle the visibility of terminator. If you want the same behavior as guake, hit F12. You will see that the value in the Keybinding column will change to F12.
  • Hit the close button to close the settings window.
  • In the terminal try the key you just set (e.g F12) to see if it works. If it doesn’t work and in the case of F12 writes on the terminal a ~, close terminator and re-open it for the changes to get applied.

Second way to make terminator toggle its visibility using the F12 key (like guake)

  • Create the folder tree ~/.config/terminator (maybe it exists already). Please note that the . in front of config is purposely there, it is the way to hide a folder.
  • In the folder create a file named config (the full path would be ~/.config/terminator/config) and put the following as content:
[global_config]
[keybindings]
  hide_window = F12
[layouts]
  [[default]]
    [[[child1]]]
      parent = window0
      type = Terminal
    [[[window0]]]
      parent = ""
      type = Window
[plugins]
[profiles]
  [[default]]
  • Save the file and start terminator, pressing the F12 key should hide the terminal, pressing it once more should make it reappear.