Nikola Brežnjak blog - Tackling software development with a dose of humor
  • Home
  • Daily Thoughts
  • Ionic
  • Stack Overflow
  • Books
  • About me
Home
Daily Thoughts
Ionic
Stack Overflow
Books
About me
  • Home
  • Daily Thoughts
  • Ionic
  • Stack Overflow
  • Books
  • About me
Nikola Brežnjak blog - Tackling software development with a dose of humor
Stack Overflow

How to replace text between quotes in vi or vim?

profile for Nikola at Stack Overflow, Q&A for professional and enthusiast programmers
I’m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%). In this category (stackoverflow) of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread here.

So, my question was if for example we have this line of code:

$query = "SELECT * FROM table";

is there a command in vi/vim which can instantly delete everything between quotes and position the cursor between them so I can start typing immediatelly?

The answer, by user eugene y, was:

Use ci", which means: change what’s inside the double quotes.

You can also manipulate other text objects in a similar way, e.g.:

  • ciw – change inside a word
  • ci( – change inside parentheses
  • dit – delete inside an HTML tag, etc.

More about different vim text objects here.

Books

Therapy – Sebastian Fitzek

My favourite quotes from the book Therapy by Sebastian Fitzek:

Stories are worthless if they can’t be narrated to someone.

Show me your friends and I’ll tell you who you are.

Even the most intelligent people act from time to time very funny and unusual. Almost every owner of the TV remote has, for example, irreparable habit of pressing the buttons harder when batteries start to fade.

Stack Overflow

What is disableSelection used for in jQuery sortable?

profile for Nikola at Stack Overflow, Q&A for professional and enthusiast programmers
I’m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%). In this category (stackoverflow) of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread here.

So, my question was whether someone could help me understand why is disableSelection useful and when to use it. The simple usage example of disableSelection is this:

$("#sortable").disableSelection();

The answer, by user kba, was that:

It’s useful if you want to make text unselectable. If, for instance, you want to make drag-and-drop elements with text on, it’d be annoying to the user if the text on the box accidentally would get selected when trying to drag the box.

Books

Altered Carbon – Richard Morgan

My favourite quotes from the book Altered Carbon by Richard Morgan:

Somebody wants to kill me, and that’s not a nice feeling.

People envy me, people hate me. That’s the price of success. That was news to me, people hated me on ten different planets, but I never considered myself a successful man.

I trust those bastards as I would a cellophane condom.

Human eye is a marvelous device. With a little effort it can overlook even the greatest injustice.

Don’t judge, so that you won’t be judged.

They call that a Kemmerich gradient, and yours is so steep that you would need a bolt and ropes to climb it.

NodeJS

MEAN.io VS MEAN.js and deploying the latter on DigitalOcean

What are they?

MEAN.(io + js) are both full-stack JavaScript frameworks, and they are both comprised of these technologies:

  • MongoDB – leading NoSQL database, empowering businesses to be more agile and scalable
  • Express – minimal and flexible Node.js web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications
  • Angular – lets you extend HTML vocabulary for your application. The resulting environment is extraordinarily expressive, readable, and quick to develop
  • Node.js – a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications

Which is which?

There seems to be a lot of confusion between the two, and this StackOverflow answer can help clarify it. Essentially:

Mean.js is a fork of Mean.io and both initiatives were started by the same guy. Mean.io is now under the umbrella of the company Linnovate and looks like the guy (Amos Haviv) stopped his collaboration with this company and started Mean.js. You can read more about his reasons here.

On their GitHub pages the MEAN.io has more stars, forks, and watchers but that may easily be to the fact that the MEAN.js is younger:

mean js github page

MEAN.js GitHub page

mean.io github page

MEAN.io GitHub page

How to get started?

I already wrote about MEAN.io and how to deploy it to Nodejitsu from Windows machine, and in this blog post I’ll show you how easy it is to deploy MEAN.js on DigitalOcean. Surely, you can deploy MEAN.io too on DigitalOcean, but you would have to install all the prerequisites on your own, and as you’ll see the MEAN.js is all set up automagically with few button clicks.

Disclamer: I am affiliated with DigitalOcean. If the post helped you, you can signup via my referral link. They regularly offer free credit (currently they offer 10$) so you can try it out without paying anything (I’m sure you have the Google skills to get the discount code).

So, from my experience with the cloud hostings I was pretty much amazed how fast you can get your root box up and running on DigitalOcean, additionally they have the (so called) droplet for MEAN ready so in basically less than 1 minute you’ll have everything ready to start coding your MEAN project. Let’s now see how to do this step by step.

Create a droplet

Naturally, first you have to signup up with DigitalOcean (remember to look for coupons), and once you do that you’ll be able to login to their web admin dashboard.

DigitalOcean calls its virtual servers, droplets; each droplet that you create is a new virtual server for your personal use. In order to create a new droplet from your web admin, click on the CREATE button, and select a custom hostname and size (as shown on the image below):

create_1

All on the same page (scroll down), select a region which is closest to you (London  1 in my case, as shown on the image below):

create_2

The most important part is selecting the actual distribution/application that will be installed on your droplet by default. Here you have quite a few options and I encourage you to test them as you see fit. In my example, however, we’re gonna use the MEAN on Ubuntu 14.04 (as shown on the image below).

create_4

Finally, you can leave the additional settings as they are and you can confirm the droplet creation by clicking on the Create Droplet button.

create_5

Login to your new droplet

Shortly after (less than 1 minute!) you’ve clicked the Create Droplet button you’ll get the email with the info on how to connect to your droplet. Below is the example of such an email. In order to connect to your droplet you can use PuTTY (a free implementation of Telnet and SSH for Windows and Unix platforms), and all you’ll have to do is enter the IP and click Open (it’s good to name this connection and save it for later use – digocean in the image below).connect_1

If the popup appears as shown on the image below, most likely you’ll want to click Yes.connect_2

When you log in with the username root and password that you got in the mail, you’ll be prompted to repeat the root’s password and to change it to something else, which is a standard security practice.connect_3

Run the application

Your application will be located in /opt/mean  folder, and in order to run it you just have to execute the following command in that folder:

grunt

Output that you’ll most likely see after running the grunt command can be seen on the image below:

run_1

To view your application in the browser, visit http://YOUR_DROPLET_IP:3000/ (in my case that would be http://178.62.47.198:3000) and you should see a screen similar to the one on the image below:run_2Finally, all done. Next logical step would be to check out he MEAN.js docs.

Happy building!

Books

The Science of Getting Rich – Wallace Wattles

My favourite quotes from the book The Science of Getting Rich by Wallace Wattles:

One is all and all is one. One substance manifests itself as the seeming many elements of the material world.

The fact remains that it is not possible to live a really complete or a successful life unless one is rich. No man can rise to his greatest possible height in talent or soul development unless he has plenty of money. For to unfold the soul and to develop talent, he must have many things to use, and he cannot have these things if he hasn’t got the money to buy them with. A man develops mind, soul and body by making use of these things. And the society is so organised that the man must have money in order to become the possessor of things. Therefore the basis of all advancement for men must be science of getting rich.

To be really rich does not mean to be satisfied or contempt with a little. No man ought to be satisfied with little if he is capable of using and enjoying more.

To be content with less is sinful.

We should live equally for the mind, body and soul.

You can render to God and humanity no greater service than to make the most of yourself.

Getting rich is not a result of saving. Nor is getting due to doing things which others failed to do.

You will do best in the business you like.

You do not want to get rich in order to live “sweinlishly”.

Get rid of the idea that God wants you to sacrifice for others, and then you can secure his favor by doing so. What she wants is to make the most of yourself, for yourself and for others, and you can help others more by making the most of yourself.

You can make the best of yourself only by getting rich.

You are to become a creator, not competitor.

You do not have to get something for nothing. You can give a man more use value than the cash value that you take from him.

It is your father’s pleasure to give you the kingdom, said Jesus.

Surround yourself with the best in order to become the best.

Know what you want, be definite and ask for it.

Think that the thing it is already yours. Live in the house mentally until it takes the form around you physically.

Whatsoever things you ask for when you pray, believe that you will receive them and you shall have them, said Jesus.

You cannot impress the mind of God by having a special Sabath day set apart to tell him what you want and then forgetting him during the week. You need to pray without seizing.

If you want to get rich, don’t study poverty, health is not obtained by thinking about disease.

Never take sudden or radical action to as when you are in doubt of wisdom of doing so.

Only those who gain more retain any. From him who had not shall be taken even that which he have.

Don’t please your employer, advance yourself.

Never admit the possibility of failure or speak in the way of failure as a possibility. Never speak of times as being hard or business conditions being doubtful.

There is a thinking stuff from which all things are made and which in its original state permeate penetrate and fills the inter spaces of the universe.

A thought in its substance produces the thing that is imaged by the thought.

Man can form things in his thought and by impressing his thought upon formless substance can cause the thing he thinks about to be created. In order to do this man must pass from competitive to creative mind, otherwise he cannot be in harmony with the formless intelligence which is always creative and never competitive in spirit.

Man may come into full harmony with the formless substance by entertaining a lively and sincere gratitude for the blessings it bestoves upon him. Gratitude unifies the mind of men within intelligence of substance so that mans thoughts are received by the formless.

Man must form a clear and definite image of the things he wishes to have, to do, or to become and he must hold this image in his thoughts while being deeply grateful for the supreme that all his desires are granted to him.

The man who wishes to get rich must spend his leisure hours in contemplating his vision and in earnest thanksgiving that the reality is being given to him. Too much stress must not be laid upon the importance of frequent contemplation of the mental image.

You have to do this day all that can be done this day, with doing each act in a successful manner.

CodeProject, WPF

How to build a font viewer application in WPF without writing any C# code

TL;DR

You can download from Github the demo application (zipped exe) or the whole source code project (Visual Studio project solution), or continue reading to see how it was built step by step.

What is this for an application?

My version of the FontViewer application as initially designed and developed in the book Sams Teach Yourself WPF in 24 Hours, with added IntegerUpDown control from Extended.Wpf.Toolkit NuGet package (for changing the font size).

Disclamer

I’m in no way affiliated with Amazon or this book, I just think it’s great, for beginners especially. This StackOverflow question may be useful if you’re searching for a good book on learning WPF.

Let’s build it step by step

First, create a new WPF Application project in Visual Studio (File -> New Project) as shown on the image below, and name it FontViewer:

FontViewer_1

Full source code listing

Now, copy the following XAML code in your MainWindow.xaml file (explanation comes after):

<Window x:Class="FontViewer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        Title="FontViewer" Height="450" Width="600">
    
    <DockPanel Margin="8">
        <Border DockPanel.Dock="Top"
                CornerRadius="1"
                BorderThickness="2"
                BorderBrush="Black"
                Background="AliceBlue"
                Padding="8"
                Margin="0 0 0 8">
            <TextBlock FontSize="14" TextWrapping="Wrap">
                <Bold><Italic>Instructions:</Italic></Bold>
                <LineBreak />
                
                Select a <Underline>font</Underline> to view from the list <Italic>below</Italic>.
                <Span FontSize="10">
                    You can change the text by typing in the region at the bottom.
                </Span>
            </TextBlock>
        </Border>
        
        <ListBox x:Name="FontList" 
                 DataContext="{x:Static Fonts.SystemFontFamilies}"
                 DockPanel.Dock="Left" 
                 ItemsSource="{Binding}"
                 Width="160">
            <ListBox.ToolTip>
                <ToolTip>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Count, Mode=OneTime}"/>
                        <TextBlock Text=" fonts are installed."/>
                    </StackPanel>
                </ToolTip>
            </ListBox.ToolTip>
        </ListBox>

        <StackPanel Orientation="Horizontal"  
                    HorizontalAlignment="Center" 
                    Margin="5"
                    DockPanel.Dock="Top">
            
            <TextBlock Margin="0 0 5 0">Font size:</TextBlock>
            <xctk:IntegerUpDown x:Name="FontSize"
                                Height="20" 
                                Width="50"
                                Increment="1" 
                                Value="20">
            </xctk:IntegerUpDown>
        </StackPanel>

        <TextBox x:Name="SampleText"
                 MinLines="2"
                 Margin="5"
                 TextWrapping="Wrap"
                 ToolTip="Type here to change the preview text."
                 DockPanel.Dock="Top">
            The quick brown fox jumps over the lazy dog.
        </TextBox>

        <TextBlock Text="{Binding ElementName=SampleText, Path=Text}"
                   FontFamily="{Binding ElementName=FontList,Path=SelectedItem}"
                   FontSize="{Binding ElementName=FontSize,Path=Value}"
                   TextWrapping="Wrap"
                   Margin="5"/>
    </DockPanel>
</Window>

Code decomposition

You can see that our main container is DockPanel, which is useful when you want to “dock” certain elements to the sides (top, bottom, left, right) of the window.

Next comes the Border element which contains instructions (on how to use the application) written inside the TextBlock element. If you’re familiar with CSS, the attributes on the Border element should seem natural to you. However, one novelty is the DockPanel.Dock=”Top” property which basically tells the Border element to position itself at the top of the parent DockPanel (in which it is contained).

To populate the list of available fonts which are installed on the machine, we’re using the ListBox element. It’s docked to the left, its name variable (by which we can reference it later in the code) is set to FontList (x:Name=”FontList”), and most importantly the ItemSource attribute is set to {x:Static Fonts.SystemFontFamilies} which basically tells the ListBox that it should display all the items which are returned from the static property SystemFontFamilies provided by .NET framework.

After the ListBox comes a StackPanel which by default “stacks” elements that it contains one below the other. By using the Orientation property (Orientation=”Horizontal”) we can make the contained elements align one next to the other.

Let’s mix it up a little

Since we want to have a numeric Up/Down control as we do in WinForms we will have to download additional component as it doesn’t exist in the standard WPF library. The discussion about this can be found on Stack Overflow. The solution is to go to Tools -> NuGet Package Manager -> Manage NuGet Packages… as shown on the illustration below:

FontViewer_3

On the form that shows up after this you have to do a search for Extended.Wpf.Toolkit and Install it:

FontViewer_2

As stated in the installation instructions you have to:

  1. Add a using statement (“using Xceed.Wpf.Toolkit;” for most of the controls, “using Xceed.Wpf.DataGrid;” for the datagrid control) to the top of .cs files
  2. Add a new xmlns (xmlns:xctk=”http://schemas.xceed.com/wpf/xaml/toolkit” for most of the controls, xmlns:xcdg=”http://schemas.xceed.com/wpf/xaml/datagrid” for the datagrid control) to the top of XAML files
  3. Remember to use the namespace prefix (in the above example, <xctk: …> or <xcdg: …>) in the body of your XAML

As you can see in the full code listing, I added the needed xmlns in MainWindow.xaml. However, the needed using statement (using using Xceed.Wpf.Toolkit;) has to be added in the MainWindow.cs (right click on the MainWindow.xaml file and select View Code) file.

After this installation and proper including is done, you can use the new IntegerUpDown control in your XAML file like this:

<xctk:IntegerUpDown x:Name="FontSize"
                    Height="20" 
                    Width="50"
                    Increment="1" 
                    Value="20">
</xctk:IntegerUpDown>

Increment attribute tells the control by which amount to increment/decrement the number which is set by default in Value attribute.

Let’s enter some text

In order for the user to be able to enter some text and then see how the text looks like in the particular font, we’re using the TextBox element. Most important attribute is the x:Name (x:Name=”SampleText”) because we’ll use it to reference it in the TextBlock below.

Show me how the font looks like

We’re using the TextBlock element to show the user how the text which he entered in the TextBox looks like in the font which he selected from the ListBox. Here lies the core of our application as here we’re setting the so called Data Binding. We’re binding the Text attribute of the TextBlock to whatever is entered in the TextBox named SampleText (Text=”{Binding ElementName=SampleText, Path=Text}”). Next, we’re binding the FontFamily to the selected item in the ListBox named FontList (FontFamily=”{Binding ElementName=FontList,Path=SelectedItem}”), and finally we’re binding the FontSize to the Value of the IntegerUpDown control named FontSize.

Wrap up

If you run your application now you’ll see it works as expected and just like that you’ve made your very own version of the FontViewer application, you’ve seen how to install additional components from within Visual Studio using NuGet package manager, and you’ve seen how to set the data binding between elements.

Hope this proved to be helpful and got you excited about WPF in that manner that you’ll now go and do further exploring on your own.

Quick tips

Enable continuous scrolling by default in Adobe Reader

Whenever I opened some pdf file and scrolled with the mouse the scrolling was per one page. Sure, you can change this by going to View -> Page Display -> Enable Scrolling:

enableScrolling

but once you reopen Adobe Reader it’s back to as it was.

The solution is to make the default setting for all documents to have this setting and you do this by going to Edit -> Preferences and selecting Accessibility -> Override Page Display and setting it to Single Page Continuous.

adobeReaderDefaultSettings

edit: Thanks to Hugo André’s comment, it seems that in newer versions this is found by going to: “Go to the tab Adobe Reader -> Preferences”.

Quick tips

How to make a print screen on a Mac keyboard on the Windows machine

This has been haunting me for some time now – there’s just no Print Screen key on this keyboard which works on a Windows machine.

If you take a look at this thread you’ll see numerous attempts of which only installing AppleKeys2 worked for me. Now I only press F13 and woila – it works the same as Print Screen on a normal Windows keyboard.

If for some reason the link for AppleKeys2 is not working anymore, you can download it here.

Quick tips, WPF

Maximizing WPF Window to second monitor

So, if you have this in the code behind:

if (System.Windows.Forms.Screen.AllScreens.Length >= Config.ScreenNumber)
{
    System.Drawing.Rectangle screenBounds = System.Windows.Forms.Screen.AllScreens[Config.ScreenNumber - 1].Bounds;
    this.Left = screenBounds.Left;
    this.Top = screenBounds.Top;
}

And this in your XAML file on Window:

<Window WindowState="Maximized" ...

Asuming that Config.ScreenNumber equals 2, it will not position itself on the second screen no matter what you do, it will maximize on your main screen. What helped in the end was to remove the WindowState from the XAML definition and to add Loaded event handler:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    this.WindowState = WindowState.Maximized;
}
Page 47 of 51« First...102030«46474849»50...Last »

Recent posts

  • Discipline is also a talent
  • Play for the fun of it
  • The importance of failing
  • A fresh start
  • Perseverance

Categories

  • Android (3)
  • Books (114)
    • Programming (22)
  • CodeProject (35)
  • Daily Thoughts (77)
  • Go (3)
  • iOS (5)
  • JavaScript (127)
    • Angular (4)
    • Angular 2 (3)
    • Ionic (61)
    • Ionic2 (2)
    • Ionic3 (8)
    • MEAN (3)
    • NodeJS (27)
    • Phaser (1)
    • React (1)
    • Three.js (1)
    • Vue.js (2)
  • Leadership (1)
  • Meetups (8)
  • Miscellaneou$ (77)
    • Breaking News (8)
    • CodeSchool (2)
    • Hacker Games (3)
    • Pluralsight (7)
    • Projects (2)
    • Sublime Text (2)
  • PHP (6)
  • Quick tips (40)
  • Servers (8)
    • Heroku (1)
    • Linux (3)
  • Stack Overflow (81)
  • Unity3D (9)
  • Windows (8)
    • C# (2)
    • WPF (3)
  • Wordpress (2)

"There's no short-term solution for a long-term result." ~ Greg Plitt

"Everything around you that you call life was made up by people that were no smarter than you." ~ S. Jobs

"Hard work beats talent when talent doesn't work hard." ~ Tim Notke

© since 2016 - Nikola Brežnjak