megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

How to update values of a column from one table to another?

Sometimes, I come across situations where I would need to update a newly added in a pivot table and this query does come in handy:

UPDATE table_a INNER JOIN table_b ON table_b.col_x = table_a.col_x SET table_a.col_y = table_b.col_y;

Hope you found this useful too!

How to switch between integrated terminal and editor in Visual Studio Code?

While, I'm trying to adjust myself to using Visual Studio Code, I found it quite annoying that there isn't a shortcut to switch focus between the editor and the integrated terminal. For a guy, like me, who makes use of the keyboard all the time, that's pretty important.

I did some research and found a way on how to do it. Just type Ctrl+Shift+P and type Open Keyboard Shortcuts and add these lines:

[
    {"key": "ctrl+`", "command":"workbench.action.terminal.focus"},
    {"key": "ctrl+`", "command":"workbench.action.focusActiveEditorGroup", "when": "terminalFocus"},
]

Save the file and now, you'll be able to switch between the two by pressing Ctrl+` keys.

Hope you found this tip useful!

Redirect from HTTP to HTTPS in Apache VirtualHosts

Here's a simple technique on how I learned to redirect a site from HTTP to HTTPS automatically using Apache's VirtualHost configuration.

Go to your configuration file or 000-default.conf and modify your configuration to something like this:

<VirtualHost *:80>
    ServerName your.domain.com
    Redirect permanent / https://your.domain.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName your.domain.com
    SSLEngine On
    # insert code here...
</VirtualHost>

Save the file and check if the configuration is correct before your restart the server:

sudo apachectl configtest

If you get the message, Syntax OK, then go ahead and restart the server:

sudo systemctl restart apache2

Now, your visitors will be redirected from HTTP to HTTPS automatically!

Hope you found this tip useful!

Quick Search for file in your workspace in Visual Studio Code

Recently, started using Visual Studio Code as I'm yet figure out a way to install Sublime Text on Void Linux (using it as my current daily driver).

I'm always used to looking for my files using keyboard shortcuts in Sublime Text and I was kind of surprised this feature isn't enabled by default in Visual Studio Code.

Anyway, after a little digging, I found out that adding this line to your settings.json file would allow you to look for files in your workspace:

"search.useIgnoreFiles": false

And, now try Ctrl+P and you'll be able to search for your file(s) easily.

Hope you found this tip useful!

How to install Composer manually?

You may wonder "why not install Composer via apt or yum package manager?" and yes, it can be installed that way too.

But what if you are using old composer packages or you wanted to skip the checks done during the interactive installation routine, this could be the way for you.

I wouldn't really call it an advanced technique or something but if you know what you're doing, then you should be in the right direction.

The below snippet will show you how to install it manually:

wget "https://getcomposer.org/download/VERSION_X.X.X/composer.phar"
sudo mv composer.phar /usr/local/bin/composer
sudo chmod a+x /usr/local/bin/composer

Yes, that's it!

By placing Composer in the /usr/local/bin directory, it will be accessible from any directory within the system and you can run it globally.

To check if you've installed it correctly, just do the following:

composer -V

Hope you found this tip useful!

Prevent VIM from creating swapfiles

I like VIM and use it regularly to write and edit code on a daily basis but I always find the creation of .swp files really annoying.

If you find them annoying too, you disable them temporarily in the editor, like this:

:set noswapfile

Or if you want to disable it permanently, just add this line in your .vimrc file:

set noswapfile

Don't get me wrong, I'm not saying that you should dislike .swp files because if the editor crashes or your computer/server crashes in midway, those files can save your progress.

Hope you found this tip useful!

How to setup rsync with passwordless SSH on UNIX/Linux?

Tired of ensuring if whether each file in every server is synced? Planning on doing automated backups? If so, then this technique should come in handy for you.

Interested? Then follow the steps below:

Check if rsync over SSH works

Before you start, please ensure that you can rsync to your intended server over ssh using a password. With the following example, you can just send a simple file over to the intended server and see if it works or not:

rsync -avz -e ssh test.txt username@REMOTE_SERVER_IP_OR_DOMAIN:/path/to/folder/

Once you execute this command on the terminal, it'll prompt you for a password on the remote server, if it does, then it works.

Generate SSH Keys

If you want to do a passwordless SSH, you need to generate public and private SSH keys on the local server by typing the following command on the terminal:

ssh-keygen
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

If you're prompted to enter a passphrase, just hit Enter and proceed until the key is generated. Once the keys are generated, they'll be in the ~/.ssh directory on your local server.

Copy public key to remote server

Using ssh-copy-id, you can copy the public key to the remote server:

ssh-copy-id -i ~/.ssh/id_rsa.pub REMOTE_SERVER_IP_OR_DOMAIN

Once executed, you'll be prompted to enter the password of the account on the remote server and if successful, the public key will be copied to the remote server and will be stored in it's appropriate location.

Perform Rsync over passwordless SSH

If you've come this far, then you should be able to SSH to the remote server without entering the password:

ssh REMOTE_SERVER_IP_OR_DOMAIN

If it works, then perform the rsync operation again (above) and this time, it shouldn't prompt you to enter any password.

Hope you liked reading this short article!

Render a simple RGB colored image in C++

Recently, I started reading a book called The Graphics Codex, it's an amazing book about Computer Graphics covering a lot of content and I find it quite resourceful. I wanted to build my own toy raytracer for the sake of exploring, so that's why I started to refresh my graphics coding knowledge a bit.

Just like any graphics renderer, in order to view your image. you must be able to write your image to a file, right?

Below, I wrote a really simple code to generate the entire RGB colored spectrum from top to bottom:

#include <iostream>
using namespace std;

int main() {
    const int width = 800;
    const int height = 800;

    std::cout << "P3\n" << width << " " << height << "\n255\n";

    for(int j=height-1; j>=0; j--) {
        for(int i=0; i<width; i++) {
            auto r = double(i) / (width-1);
            auto g = double(j) / (height-1);
            auto b = 0.25;

            int ir = static_cast<int>(255.999 * r);
            int ig = static_cast<int>(255.999 * g);
            int ib = static_cast<int>(255.999 * b);

            std::cout << ir << " " << ig << " " << ib << "\n";
        }
    }
}

You can generate it by simply creating an executable like this:

g++ -o pixels pixels.cpp

Now, when you execute it by typing ./pixels, you'll get a list of numbers that's pretty much a combination and permutation of RGB color values.

Lastly, to generate a colored image, like the one above, just redirect the output to an image format, in this example, I used PPM image format:

./pixels > image.ppm && xdg-open image.ppm

And that's it, you have generated your own colored image! 😁

Hope you found this useful!