Tuesday, February 6, 2018

ES6, Jest, and Webstorm: Oh My! Or, fixing 'unexpected token import'

What happens when society tries to rely on arguably one of the world's worst technologies? Fragmentation.

Javascript is beyond fragmented. In any given project, you may be piecing together chunks of react, babel, gulp, webstorm, jest, jsx, webpack, less... The list goes on. All of these parts rarely play together nicely, too.

That led to a giant headache for me and thousands of other Jest users. We wrote up a file in ES6 syntax (to un-worsify the world's worst technology), and wrote an index.test.js file only to find that, oh no:

UNEXPECTED TOKEN 'import'

What's Going on?
What exactly a module is... is not exactly known. Babel isn't putting it into a format that jest is able to properly test. To fix this issue with a very barebones project structure, you need to:

yarn add jest-cli babel babel-core babel-plugin-transform-es2015-modules-commonjs 

This will install everything you need to run Jest, including the babel plugin that turns these modules into a readable format.

package.json
"jest": {
  "testEnvironment": "jsdom"},
"babel": {
  "presets": [
    "es2017"  ]
},
"scripts": {
  "test": "jest --no-cache --verbose"}

.babelrc
{
  "presets": ["es2017"],  "plugins": ["transform-es2015-modules-commonjs"]
}
Good! You now have everything that you need to do your yarn test or npm test with ES6 modules!

Take it a step further: Debug in Webstorm
Wouldn't it be absolutely awful to run a test, and have it say "cannot call function .getName of undefined"? Well, thankfully with this configuration we can properly debug.

Webstorm comes with a debug configuration called Jest. These are the settings you want:



You're all set!
Just set a breakpoint in your es6 code, and select the bug icon on the main webstorm toolbar.

babel will turn the es6 into an understandable Javascript (es3-5), the transform-es2015-modules-commonjs will turn those modules into modules that jest and browsers can understand, and the jest run configuration can talk to node over the V8 debugging protocol to let you set breakpoints.

It's kind of amazing what this hodgepodge of technology can accomplish!

Sunday, December 10, 2017

Diesel: How To have struct fields named differently than their columns

Diesel is very good at generating code that reduces the amount of boilerplate you have to write. You can explicitly ask something like
.filter(email.like(""))
and it will know how to filter based on the 'email' column, and how to populate a struct based on this.

For some reason, this falls apart when you're trying to derive the  Insertable trait.

The solution is hidden away in the API documentation, which is currently at this link.

The Solution
So, you just have to set up mappings between your struct fields and your database column in order to insert your struct as a record. Here's an example:

table:
Create Table User(EmailAddress text primary key not null, Name text not null)

struct:

#[derive(Queryable, Insertable)]
#[table_name="User"]
pub struct User {
   
    #[column_name(EmailAddress)]
    pub address: String,

    #[column_name(Name)]
    pub name: String
}

There aren't supposed to be quotes around the column names, as it's using the diesel-generated helper structures. As such, you have to make sure that your schema is imported for this to properly work.

Wednesday, November 22, 2017

Rust: Setting up a Sqlite Database with diesel

How does the database stack in Rust work?
For a long while, the database ecosystem in Rust was severely lacking. Thankfully, once Rust Stable 1.0.0 was released, the ecosystem has seen nothing but improvement and growth. Now, most users wind up using diesel, a query-builder that helps immensely with designing your data access layer.

What you need before you start
 You should have sqlite3 installed on your system of choice, as well as the latest version of cargo and rustc.

Step 1: Set up your project
As with any Rust project, simply running cargo init {project} will initialize a rust project with the file structure that cargo can understand. This will generate your Cargo.toml file, where we'll tell it what it needs to start using diesel with sqlite3.
 
Step 2: Set up your project's dependencies
In order to use diesel, you'll need the diesel libraries. It's also highly recommended that you include something called dotenv. This will let you specify what your database is per-project, which you really want. Otherwise, your entire system will only find one database. Or, alternatively, you will need to manually specify a database with every session.

Add these lines to the [dependencies] section in your Cargo.toml file:

diesel = { version = "0.16.0", features = ["sqlite"] }
diesel_codegen = { version = "0.16.0", features = ["sqlite"] }
dotenv = "0.9.0"


Now, you can configure the URL of your database. Create a new file in the same directory as your Cargo.toml file simply called .env. This will be a hidden file, so you may need to enable those to see it.

Make the text of this .env file simply 

DATABASE_URL={database}

So, if you want a sqlite3 database called test.sqlite3, your .env would look like:

DATABASE_URL=test.sqlite3 
 
Step 3: Initialize your database
 Much of the work you'll be doing is done through a command line tool, called simply diesel. First, you need to install this utility globally, with
cargo install diesel --no-default-features --features sqlite

On Linux, this will place the executable in your $HOME/.cargo/bin/ folder. You may need to add this folder to your $PATH in order to use diesel.

To create your database, go to the directory where your Cargo.toml is located and run diesel setup. This will initialize a blank sqlite3 database.
  
Step 4: Start designing your schema 
In order to set up an initial schema, and begin using your database, you have to initalize a migration. To do this, run diesel migration generate {migration-name}.

For this first migration, I tend to just do

diesel migration generate initialize

This will generate a new directory, with today's date and time, that contains an up.sql and down.sql. As the official guide states, down.sql simply should undo any schema changes you make in up.sql

You're all set!
I wrote this guide because I ran into a few pitfalls following the official guide. However, the official guide is absolutely fantastic, and describes everything that I summarized here in a clearer, more complete way. I recommend that you go and check it out at

http://diesel.rs/guides/getting-started/    

Sunday, November 12, 2017

Rust: How to use the latest version of a Cargo Crate

Why Cargo is so brilliant
Cargo is a fantastic tool. From the get-go, it's gotten a lot of things right that package managers like npm and nuget still stumble over. The competitors to these tools, yarn and paket address the issues that, thankfully, Cargo has already solved.

You know what npm got right, though? It's dead simple to hook a package up. Do you want to use react? You can just type npm install --save react, and it will find the latest version of React, and add it to the packages.json file. Cargo, too, can be abused to have this functionality.

Warning: You really shouldn't do this...
By explicitly listing versions in your Cargo.toml file, you know for a fact that a future release of the package won't break your project. Furthermore, you know for a fact that the different dependencies that you're relying on play nicely together.

One of the goals of dev-ops is minimizing risk. Having an explicit definition of the packages that could break your project is one of the most important ways to do this.

With that said...
A quick spiel on Semver
Semver, the syntax for identifying the version of the package you want to use, is a widely upheld standard. You can read the full semver guidelines to grok exactly what's happening when you type a version in. Cargo use's a Rust semver library (which can be found here) to parse its dependencies version section.

By abusing the Greater-Than functionality, we can trick Cargo to installing the latest version of a package.

Example: Installing the latest Serde
This line in your Cargo.toml file:

serde = ">0.0.0"

is all you need for Cargo to install the latest version of Serde. This can be used for any package that you don't care about the version for. Be careful with this trick. There's a reason Cargo doesn't technically support this type of workflow outside of this hack.

How To: Remove part of i3's bar with i3status

The Problem
i3bar is perfectly simple. It's not this gaudy, in-your-face information supernova that Windows has been iterating on for the last ~20 years. It tells you just what you need to know: space left, internet speed, CPU load, and the time. But... what if you don't care about your internet speed? Or the time?

Thankfully, removing it is dead simple.

i3status.conf
If you're using the default i3status for your i3bar, the fix is simple. A configuration file in your home directory overrides the default in /etc/i3status.conf. (placing one in $HOME/.config/i3status/config will override the one in your home directory).

Setting the Overrides 
So, you have two options. You can make a NEW file in either of those locations, copying the existing data over. Or, you can edit /etc/i3status.conf directly as root (not recommended).

Using the documentation in man i3status and your existing file (/etc/i3status.conf), you should be able to find the commands you want. Then, make sure that only the commands you care about are in the order variable.

For instance, by default, my order looked like this:

order += "disk /"
order += "ethernet _first_"
order += "load"
order += "tztime local"
Since I don't care about my ethernet, I struck it. Resulting in this:

order += "disk /"
order += "load"
order += "tztime local"

Rust: Returning JSON with Rocket

Preamble
JSON is one of the technologies that, while very simple at its core, has had a profound impact on the technical landscape. Most modern public API's rely on JSON to transport information between application layers. It's become essential from everything ranging from Web API's, compiler technologies, and even data storage.

With that said, it's essential for an up-and-coming web tech stack like Rust and Rocket to support it. And support it they do.

Step 1: Install Dependencies
For some reason, the official Rocket documentation hides the fact that you need dependencies outside of rocket_codegen and rocket in order to return JSON.

You need to have all of the following lines in your Cargo.toml file:

rocket = "0.3.3"
rocket_codegen = "0.3.3"
rocket_contrib = "0.3.3"
Obviously, these versions will change over time. Without these dependencies, you may find yourself with errors such as:

"Cannot find macro json!", "Value cannot be found", etc. when following Rocket's documentation.

Step 2: Start returning JSON
Now, the easy part. I'm going to post the single most rudimentary example of a JSON return ever. In real web projects, you'll want to use serde and serde_derive to turn your data-transfer objects / models into a consistent JSON format. For now, we'll use the json! macro and Json struct to hard-return a value on the root route:

#[get("/")]fn index() -> Json<Value> {
    Json(json!({"value": 5}))
}

fn main() {
    rocket::ignite().mount("/", routes![index]).launch();}

Sunday, October 29, 2017

Rust: How To Fix "no default toolchain configured" and "the following required arguments were not provided: toolchain"

The Problem
What's happening here, is that you've installed rust utilities via rustup: cargo, rustc, etc. The problem is, these utilities cannot find the default rust 'toolchain', as it has not been configured yet.

The Fix
This is a very simple fix. The following command:

rustup default stable

In this case, you want to change stable to be whatever release channel you're on. For nightly rust, this would switch to 

 rustup default nightly


Saturday, October 7, 2017

[Fix]: No Audio in Linux / Videos Don't Play

The Problem
There exists an especially frustrating problem on some linux distributions where audio will simply not play. The lack of audio playing can mean that you're also not able to watch videos, even completely without audio! Most recently, I installed Ubuntu 17.04 to find that I was unable to listen to music or even watch Youtube videos with subtitles. That's untenable, and a complete deal-breaker for most people (for obvious reason).

The Explanation
So, what's really happening here is that your user isn't in the audio group. Because they're not in the audio group, videos which contain audio (i.e all video file formats) are simply not allowed to play! Why users aren't de facto members of the audio group, especially on Linux, is completely lost on me. But, it's enough information to fix the problem thankfully. 

(Optional) A Quick Rundown on Groups
For the uninitiated; Linux has two essential components of its security system: groups and users. Users are added to groups to give them certain privileges. So, since your user is NOT a member of the audio group, you cannot play audio.

The Fix
The fix for this couldn't be easier. You need to run the following command:

sudo usermod -a -G groupName userName
Then, we're already half way done! You are now a member of the audio group. 
To apply this fix, you *need* to shut down and power back up
For me, nothing actually applied this change other than a full shutdown / restart cycle. Even rebooting didn't actually do it for me, strangely enough. That goes against all of my intuition as an avid computer user and programmer. I can't explain it.
Once you've reboot, you should then be able to run the following command:
alsamixer
Then, hover over the columns which read 'MM' and hit 'm' to unmute them. Raise their volume to 100%. Your desktop environment will control the actual volume you here, but you don't want the underlying sound system to artificially lower your volume.
The Last Step
The final thing you have to do is to run the following command:
pavucontrol
Then, from here, set up whatever audio device you'll be using. In my case, with headphones plugged into the audio jack it wound up being 'Line Out'. Under the configuration tab, I needed to select Audio Stereo Duplex for it to work. I'm also unsure why the Audio Stereo Duplex configuration works, but I know it does.
You're Done!
After all of this, you should have working audio. As you can tell, the rumor that Linux audio can be a headache is proven to be true. Videos should already be working. If you are still having trouble with it, leave a comment below and I'll do my best to help out.

Tuesday, March 7, 2017

[How To]: Not commit when a string is present in git

The Use Case
Howdy! So, I ran into an interesting problem this week. In order to test something, I had to make a change to code that should NEVER be commit... then, I commit it. D'oh!

If you want to protect yourself from something similar, or prevent data leakage, read on.

The Setup
When you clone or initialize a git project, git creates a "./.git" folder for you. This is where it tracks information about the repository, and stores some git metadata and logs.


Another thing this folder contains are "hooks", which are run at some point during the git workflow. For instance, if we had a "pre-push" hook, it would run before the push goes up to the remote.

So, what can we do with these hooks? Anything that BASH lets us. The most common use case for hooks is stopping a commit if something isn't right with the project, which is what we'll be doing in our case. However, you could do things like:


  • Print out the total number of lines changed
  • Check for a string and just give warnings (i,e mispellings, swear words)
  • Queue a local or remote backup
  • Package the project
Really, anything.

Protecting Ourselves
We would like to not commit if a string is present in the file. Imagine if we wanted to search for //[DANGER]. The presence of [DANGER] in any tracked file would stop the commit, so you could remember to go back and change the reason for it being there.

To do this, open the pre-commit.sample file and save it as pre-commit (with no extension). If there isn't a sample file, simply make the pre-commit file.

Before we write any code, let's look at what we want:

  • If a string is present ANYWHERE in the directory, fail
Alternatively, in large code bases, we want:
  • Search the diff for newly added instances of the string
Since searching through every file is going to take too long to do on every commit.

The Steps

Make the File
You should now have a file called ./.git/hooks/pre-commit. You make need to mark the pre-commit file as executable with chmod + x pre-commit.

Add this code to the file


printf "[Check]: Searching for commit mollyguard: '%s'..." "$breakingString"
sleep 1
#                                                                       VVVVVVV YOUR REGEX RIGHT THERE
if [ $(git diff --word-diff --cached --diff-filter=ACUM -U0 | grep -E "+*\[DANGER\]" | wc -l) -gt 0 ]
then 
printf "\n"
cat <<\EOF
Error: Trying to commit a tree that contains uncommitable changes 

Search the projects for $breakingString and figure out why the comment is there
EOF
exit 1;
fi

printf "Good!\n"


What this code is doing: 
  • Look at the last diff
  • Search the diff for "+{string}" (because + means it was ADDED, not removed in the last commit)
  • Count the lines
If the lines are greater than 0, and instance was found, so we shouldn't commit this branch.

Of course, this is searching just the latest commit, because searching a large software project is going to take a while. If you're working on a smaller project (or on a very fast machine), you can replace the if statement with simply

if [ $(grep -R "\[DANGER\]"../../  | wc- l) > 0 ]


Sunday, March 5, 2017

.NET - "%5C" Showing Up on URLs

Are you a developer, who is accepting a file or some string of text and putting it into a cloud? Are you, also, per chance, retrieving its URL, and notcing this pesky "%5C popping up in our URLs? Well, I know recently I was. It was a pretty strange, out of the blue error with an interesting cause. Here are some details for you:

What Causes This?
The actual causes of this problem are numerous. The bottom line is, '%5C' in a URL means that the backslash '\' got encoded in the URL. Somewhere along the way, either unintentionally or on purpose, a string containing one wound up in the encoding step.

The culprit for many users is using .NET's Path utility for trying to parse URLs. Here's an example:

Let's look at expectation vs Reality

Code: Path.GetDirectoryName("I/want/to/upload/to/this/place")
Expected: "I/want/to/upload/to/this/"
Actual: "I\want\to\upload\to\this"

This is because Path uses a field called Path.DirectorySeperatorChar, which on Windows is '\'... A bit backwards that Path can READ forward slashes as directory names, yet only output backslashes in the return value.

How Do I Fix This?
There are plenty of good solutions to fix this. However, with so many different causes to the problem, it's hard to say exactly.

To Fix it in a simple string
It's possible that all you need to do is Path.GetDirectoryName(your_string).Replace(Path.DirectorySeperatorChar, Path.AltDirectorySeperatorChar), and you're good to go.

Hopefully this shed some light on what the problem may be in your case. Hopefully, moving forward, Microsoft is not afraid to add a .NET feature like PathFlags or something so that this doesn't crop up as much as it currently does.

Saturday, November 12, 2016

Installing Racer

Prologue 
Let's talk about Rust for a moment, folks. If C is akin to the Bourgeoise, then Rust is kind of like Robin Hood. It takes ideas from C, and implements them in safe and better ways. In my opinion, it's almost foolish to start a new C project today, with all the extensibility of the High-level languages today and the safety and relative ease-of-use of languages like Rust and Go. One front where Rust is lacking, however, is IDE integration. At the time of writing, there is an Active RFC for features in the Rust compiler that will make it easier for IDE plugin authors to add Rust integration. Thankfully, in the interim, there's the Racer project which does something similar.

What does Racer do?
Racer provides automcompletion for Rust code. It does this by sending incomplete symbols in your Rust source file to racer, searching the standard library for a candidate, and providing the name and type information as you go.

So, in order for Racer to work, we need the Rust source code.

How do I install Racer?
The easiest way is with Cargo, Rust's package manager. The following commands should get you up and running. You need to have cargo, rustc, and git installed for these to work.

**NOTE FOR ZSH USERS**
In the following commands, simply replace .bashrc with .zshrc. Everything else should stay the same.

Install racer from crates.io
cargo install racer

Add the cargo directory to your $PATH variable

echo "export PATH=\"\$PATH:$HOME/.cargo/bin\"" >> .bashrc

Download the rust source code 
git clone --depth 1 https://github.com/rust-lang/rust.git $HOME/.rustsrc

Set your RUST_SRC_PATH environment variable to look for your rust src checkout
echo "export RUST_SRC_PATH=\"$HOME/.rustsrc/src\"" >> .bashrc

Sunday, July 31, 2016

Setting up Emacs for Haskell

Haskell is a bit of a quirky language. It has been around for a very long time, yet hasn't nearly received the professional love that languages like Java, C#, or even F# have gotten. Professional IDE's for the language doesn't exist; good IDE plugins for the languages barely do. So, it has become standard within Haskell community that either Emacs or Vim be used. In this article we'll be examining how to start working with Emacs and Haskell effectively.

First Things First
Prerequisites 
We have to get Emacs and Haskell installed, if they're not already. If you already have emacs and ghc installed, then you can safely skip this step. Otherwise, you need to install the following packages:

emacs (Version 24 or greater)
ghc (Version 7.5 or greater)

Ubuntu
sudo apt-get install ghc emacs
Fedora
sudo dnf install ghc emacs
Arch
sudo pacman -S ghc emacs
Melpa
The next steps are much easier to perform if you already have Emacs set to look at the MELPA repositories. If you'd like instructions on how to set up MELPA, I've written a guide about it Here.

Emacs Packages

The Emacs package which controls Haskell can be found in the "haskell-mode" package. The two other helpful packages that are also in the MELPA repositories are haskell-emacs, which lets you make Emacs extensions in Haskell, and haskell-snippets, which will present a certain type of code completion into Emacs for haskell by the use of yasnippet.

To install the necessary packages, do:

M-x package-install [ret] haskell-mode [ret]
M-x package-install [ret] yasnippet
M-x package-install [ret] haskell-snippets [ret]
haskell-snippets and yasnippet are completely optional, but they will make your experience far better.

Configuration
 A bit of a frustrating trend with haskell-mode is that it's making more and more features opt-in, in order to reduce the memory overhead of using haskell mode. What this means for the average user is inconvenience in configuring haskell-mode.

In your ~/.emacs file (or wherever you prefer to extend Emacs from), put in the following lines:

(add-hook 'haskell-mode-hook 'haskell-indentation-mode)
(add-hook 'haskell-mode'hook 'interactive-haskell-mode)
(add-hook 'haskell-mode-hook 'yas-minor-mode)
Or, if you plan to use all of these features:
(add-hook 'haskell-mode-hook (lambda () (interactive) (haskell-indentation-mode) (interactive-haskell-mode) (yas-minor-mode)))
This makes it so that when you enter haskell-mode (which you do when you open any .hs file), the haskell indentation mode, interactive haskell mode, and the yas minor mode are all enabled.

haskell-mode is what provides syntax highlighting for haskell, amongst a few other features.
haskell-indentation-mode is what allows you to automatically indent haskell source with text
interactive-haskell-mode is what lets you run Haskell code right in Emacs to check for errors, make tests, etc.
yas-minor-mode lets you insert snippets of Haskell code into the file you're in, which makes working with Haskell far easier, due to the immense amount of syntax sugar that Haskell makes use of.

Using Emacs for Haskell
Let's make sure that we have everything working. In Emacs, open up a file called "test.hs" and add the following lines:

myFunction :: (a -> b) -> a -> b
myFunction f a = f a
myFunction2 :: Int -> Int
myFunction2 a = myFunction (+ 2) a
Now, do C-c C-l (hitting c and l while holding control). If everything is installed correctly, it should show you a lambda and a greater-than symbol. This is a ghci prompt, which will evaluate your current file and let you interact with the functions and data types you've defined in this file or any that are imported into it.

Just to see what would happen when there's an error, change the first line to this:
myFunction :: (a -> b) -> a -> c 
And try to run C-c C-l again. Notice that it leaves a white exclamation point to the right of the problem in the source code, and underlines exactly where the error is in red.
  as well as showing you the error in the bottom part of the window
This is how your Emacs workflow will go.

Further Reading
There is plenty more to do with Emacs and Haskell, but this will get you started. You can activate true code completion, get linting messages, etc. through the use of an extra extension called ghc-mod. However, ghc-mod is currently broken in cabal... Whenever the version in the cabal repositories is fixed, I will add information about ghc-mod to the guide.

If your distribution has ghc-mod, it's would betroth you to read about installing it in Emacs here.

How To: Change your desktop background in BSPWM

Howdy! So, you've installed bspwm and you noticed that it's not as fancy as what you're used to. No fancy settings for changing your desktop environment, no 'settings', nothing. We're left to our own devices.

Thankfully, Linux is full of devices for us to use. One of which, feh, will let us change what wallpaper is used by bspwm.

Install feh

The first step is to install feh. Feh is small program used to view images without using a ton of resources. Luckily, feh has been adapted to work as a background setter.

Ubuntu
    sudo apt-get install feh
Fedora
    sudo dnf install feh
Arch
    sudo pacman -S feh

Use Feh
In order to simply use feh as a photo viewer, you can call "feh picture.jpg", where "picture.jpg" is any image file that ImageMagick can open. In order to use feh to create a desktop background, call this command:

feh --bg-center picture.jpg
Once again, where picture.jpg is just about any image file.

And there you have it! A desktop background... for now. The thing is, after this session, the background will revert to normal. In case you want this to remain your desktop background, we need to edit out bspwmrc file.

Configure feh
 In your bspwmrc file, which is usually located in ~/.config/bspwm/bspwmrc, we need to call feh on startup. On mostly any line, preferably towards the end of the file, add this line:
feh --bg-center ~/Pictures/background.jpg
where the filepath is the path to the file you plan to use for your background. Now, when you start bspwm, it should set up the background for you automatically.

Further Configuration

Now, there are further ways that you can configure feh. Right now it's set to center the image in the middle of the desktop, surrounding it with a black border. If you'd prefer to have it stretch, or tile, you can use these flags in place of --bg-center:

--bg-fill : This will preserve the aspect ratio of the file, and optionally cut off whatever part of the image doesn't scale.

--bg-max : this will scale the image to its maximum size, and put black borders on one side.


--bg-scale : Will distort the image to fit exactly within the confines of the desktop. Since this doesn't preserve the aspect ratio, stretching may be a result.

--bg-tile : Will repeat the image until it fits within the monitor.

Advanced Configuration
So, now you know how to get the desktop set up in bspwm. But, bspwm "configurability" is a bit more than your average configurability. We can do some pretty cool stuff with the fact that we're using a shell to set the desktop background.

How about instead of the earlier command, we put this in the configuration file:

feh --bg-center $(find ~/Pictures/Backgrounds -type f | shuf -n 1)
So that it randomly chooses a background out of a "Backgrounds" folder in your Pictures directory?

Anything like this is possible in bspwm. You can have it change based on the time of day, or the type of music playing, or... anything really. The choice is always yours with BSPWM!

How To: Install bspwm

Are you a Gnome or KDE user who is tired of the ordinary? Are you an i3 or herbstluftwm user who has heard the benefits of using bspwm? If you are, you have probably looked into it. And, furthermore, when you looked into it you may have found that it's a bit more involved than you had hoped. No worries! Following this guide should get you up and running with your very own bspwm configuration.

Install the Packages
Installing the packages is, unfortunately, a little but harder than just calling apt-get. We have to compile them. Now, a bit more fortunately, all of the dependencies for bspwm are readily available in your repositories.

Dependencies
On Ubuntu:
sudo apt-get install xcb libxcb-util0-dev libxcb-ewmh-dev libxcb-randr0-dev libxcb-icccm4-dev libxcb-keysyms1-dev libxcb-xinerama0-dev libasound2-dev libxcb-xtest0-dev

On Fedora:
sudo dnf install xcb-util-devel xcb-util-keysyms-devel xcb-util-wm-devel alsa-lib-devel dmenu rxvt-unicode terminus-fonts


Not too bad. What these commands do is install the dependencies that will be needed when we're building bspwm.

Build Tools
Since we're manually building bspwm, we need certain build tools. Libraries, compilers, etc. 
Ubuntu
sudo apt-get install gcc make
Fedora
sudo dnf groupinstall "Development Tools"
Alrighty. So, we have the dependencies and the build tools. Now, we need to download BSPWM.

The Packages

Let's download the two packages using git, now.
cd Downloads
git clone https://github.com/baskerville/bspwm.git
git clone https://github.com/baskerville/sxhkd.git
If all went well, you should have two folders in your 'Downloads' folder - "bspwm" and "sxhkd"

Compilation
The final step to installing the bspwm system itself is compiling it. To do this, go into the Downloads folder and type 

cd bspwm
make
sudo make install

   Now, if you're not used to compiling things, this process probably looks a tad scary. There should be a lot of text scrolling along in the terminal - that is okay. Unless you see "Error, exiting" during this step, everything is working fine.

After this, issue:

cd ..
cd sxhkd
make
sudo make install
If everything went well, we're ready to start doing some of the initial configuration. Awesome!

Configuration

Since bspwm and sxhkd are both installed, let's take a minute to analyze exactly where we are. We've installed bspwm, the window manager, and sxhkd, the keyboard daemon. How bspwm works this:

bspwm manages your windows. Where they're placed, their layout on your desktop, etc. It doesn't handle anything else, unlike traditional window managers.

bspc tells bspwm what to do. When you see bspwm moving windows, closing windows, etc. it's because bspc told bspwm over its socket file.

sxhkd listens to the keyboard for keyboard shortcuts. These keyboard shortcuts can execute any arbitrary command, although they are commonly used to issue bspc commands, such as the command that tells bspc to close a window, etc.

Running - With a display manager
Display managers control your X11 / Wayland (what makes your computer graphical, instead of all commands) sessions, as well as logging in. When you start your computer and see "username" and "password", you're looking at your display manager. Some common display managers on Linux are lightdm (commonly ubuntu), gdm (commonly gnome), lxdm (commonly lxde), and kdm (commonly kde).

bspwm comes with the files necessary to add the window manager to the display manager you already use. In order to use your display manager to log in, you need to copy the "bspwm.desktop" file (from bspwm/contrib/freedesktop). In the Downloads folder:

sudo cp bspwm/contrib/freedesktop/bspwm.desktop /usr/share/xsessions
Now, you can restart your display manager, or choose to reboot. bspwm should be an option in your display manager. You can skip the "Without a display manager" section and move onto configuration.


Running - Without a display manager

Without a display manager, things are a bit more advanced. I'm assuming that if you're choosing to forego using a display manager, that you're a fairly advanced user, so I'll keep it short. Change the contents of your ~/.xinitrc file to say
exec bspwm
And you'll be good to go. But wait! We have to configure the keybindings.

Keybindings and Autostart
Now, you should have bspwm completely installed on your system. However, if you tried to log into bspwm right now you would be greeted with... well, nothing. And, for the moment, that's fine. But the bad part is you won't be able to do anything either. In order to fix this, we need to set ourselves up some keybindings.

Bspwm comes with files with default configurations. Let's get started with the window manager by copying these default keybindings and behavior's over to their respective directories. First, we must create those directories.

mkdir ~/.config/bspwm
mkdir ~/.config/sxhkd

bspwm's configuration:

cp ~/Downloads/bspwm/examples/bspwmrc ~/.config/bspwm/
chmod +x ~/.config/bspwm/bspwmrc
sxhkd's configuration:


cp ~/Downloads/bspwm/examples/sxhkdrc ~/.config/sxhkd
Further configuration
Now, it may be preferable to edit either of these files. The function of bspwmrc in .config/bspwm is essentially an "autostart" file. It lists commands that bspwm should run when it starts up. Now, remember, bspwm is controlled via commands, so rules which govern how bspwm behaves are located in this file.

Things such as how monitors should be handled, window borders, rules for allowing certain windows to float, etc. are all configured here, as well as any programs you want to run when bspwm starts up (like, say, your bluetooth daemon).

sxhkdrc in .config/sxhkd controls keybindings. Keybindings for bspc are common, and important to make note of. By default, some of the most important default commands are:

super + return | urxvt
super + space | dmenu_run
super + escape | [reload sxhkd configuration]
super+alt+Escape | logout of bspwm
super+m | Change the desktop mode

These commands, especially super+return should get you started.
Note: super is the mod4 key on the keyboard, commonly called the "Windows" key as it usually dons the Windows logo
Now, with all of these files in place, we should be ready to start bspwm for the first time. If you don't have urxvt already installed, it's important to issue:

Fedora
sudo dnf install rxvt-unicode
Ubuntu
sudo apt-get install rxvt-unicode
So that you're able to use the terminal when you start using bspwm. The terminal is essential, as it will allow you to modify your sxhkd configuration once you're in bspwm.

If you'd like dmenu (it's an application launcher), issue:

Fedora
sudo dnf install dmenu

Ubuntu
sudo apt-get install suckless-tools
Using bspwm
 Now, start bspwm. If you installed bspwm into your display manager, you can select it from the menu there. If you chose to use .xinitrc, then use "startx" at a tty.

It may seem like it's not working when you start it. It will either show a black screen or the "splash screen" of the display manager when you first log into it.


This is how it looks on Ubuntu

Since we installed urxvt, we can launch the terminal with super+enter, or whatever you changed it to.

By default, it takes up the entire screen like this. But, notice that when you open another program, it will split the total space in half.


This is showcasing bspwm's tiling ability. However, bspwm can do so much more than the default sxhkd and bspwmrc files allow you to do.

Conclusion
Well, there we have it. We now have a working bspwm configuration, and we're ready to get tinkering! Typing "man bspc" at a terminal will show you all of the awesome things that you can do with bspwm, so there's plenty to try out in the new desktop.

This is the beginning of a series of articles about bspwm and sxhkd on this blog. For more, see below, where I'll be adding additional content as it is made:

Modifying keybindings in bspwm : [In Progress]

Happy hacking!

Huge thanks to https://github.com/windelicato/dotfiles/wiki/bspwm-for-dummies by windelicato, where most of this guide was sourced from. I paraphrased much of this guide from his original writeup here for the purpose of clearing up the process, and fixing one mistake he made in the original guide.

What is a Monad? What is a Higher Kinded type?

If you're stumbling upon this document, I'm guessing that you're just getting into a statically typed programming language and are (rightfully) confused about what all the kerfuffle regarding Monads and Higher-Kinded types are. This blog article will clear all the confusion you have about what monads and higher kinded types really are, but how to use higher kinded types (in, say, Haskell) is not covered here.

Background - Static Types
Before we can really talk about what a monad is, let's take a step back. In terms of programming, monads only exist in programming langauges which support strong static typing. That means, that the type system of the programming language follows a mathematical type system. See: Wikipedia.  The reason that this is so neat is that Type Theory is the basis of all of mathematics. Every other branch, including Calculus, Algebra, Trigonometry, Statistics, etc. can all be defined in terms of Type Theory operations.

In programming languages, types denote the type of data you're working with. Numbers may be classified as "int" or "double" in a lot of languages, which are different types which represent numbers. Text may be of type "string", etc. Languages that have "strong" static types take this a step further, being a direct representation of their type-theory counterparts.

Background - Type Theory
Types in type theory work as such: there are two variations of information. 'Types' and 'Kinds'. Kinds are, in a way, the father of types.

The kind, denoted "*", is literally pronounced "type". It is used to describe any type that doesn't take a type argument (more on this later). This can include things like functions, ints, doubles, strings... pretty much every type that you're experienced with, outside of generics.

The kind * -> * is where things begin to be interesting. This is about as far as Haskell and many other strongly-typed languages truly go (with the exception of kind (* -> *) -> *, the higher-order kind). This is the kind of any type that takes one type parameter... a little unclear? Read on.

Higher Kinded Types and Monads
* -> * you can think of as a "template template". Think of, in Java, this class:

class myClass<T>{
  T myItem;
}

In this case, the type "myClass" is the first * and the type "T" is the second *. In Haskell, which is strongly statically typed, a data type declaration, such as

data List a = Node a | Rest (List a)

In this case here, List a has the kind "* -> *" because it takes one type parameter (a). 

The important thing to realize about this, is that kinds of this type can be generecized within the type declarations of other functions. As an example of exactly what that means:

myFunction :: l a -> [a]

would take any * -> * that takes 'a' as a type paramter, and returns an array of that type from it.

Where do Monads fit into the equation?
Monads are a concept from category theory. There, monads are an endofunctor paired with two natural operations. The details of this are interesting, but outside the scope of this quick run-down of HKT's and monads. Check out Category Theory if you'd like to know more.

The function declaration of monad's function is as such:

Monad m => m a -> (a -> m b) -> m b

So, as you can see, Monads are only possible with the use of Higher-Kinded types ((m a) and (m b), make the kind * -> *). Monads also define particular functions which follow the 'monadic laws', however, these are unrelated to their basis in the type system.

Conclusion
Type theory is a gigantic branch of mathematics, and this quick run-down of how types work in statically typed languages does not even closely begin to scratch the surface. Hopefully you've taken away a bit more about what it means to be "higher kinded", and a bit how monads use the type system to exist.

If you'd like to learn more or correct me on something I said wrong up above, by all means feel free to comment!


Thursday, July 28, 2016

Comparison of Java IDEs

Why use an IDE?
If you're writing Java programs, you are without a doubt using a Java IDE of some sort. The way that Java runs, that is, using managed bytecode on a virtual machine means that the compilation step is a bit more rigorous than a call to gcc as you would do for a C source file. In any given Java program, you go through so many steps to compile and run the bytecote that it's nearly nonsensical to use the language without an IDE.

Additionally, the language's complex foundation (meaning, it's extensive standard library, OO model, and verbose nature) make it even more perfect of a fit for a language to use along with an IDE. So, you can't be too informed about what IDE is good for you.

Personal IDE Ranking
Quickly, here are the IDE's that I've used and most like for Java development:

#1 - IntelliJ
Download

#2 - Eclipse
Download

#3 - Netbeans
Download

These are the basic Java IDE's. For a more in-depth look, check out this list here.

Strengths and Weaknesses

IntelliJ
IntelliJ, Jetbrain's Java IDE, has become the de-facto Java IDE in the industry. As a matter of fact, many of Jetbrain's products are used all across the software industry (most famously, their Resharper plugin for the Visual Studio ecosystem).

Its strength lies in a very strong out-of-the box experience with heaps of features missing from the other two popular Java IDE's. Some of these features include:

Out-of-the-box experimental technologies

With Netbeans or Eclipse, you can feasibly use technologies such as groovy and maven. With Eclipse, you can integrate Scala, Clojure, kotlin, etc. into the IDE with the use of addons. However, IntelliJ IDE comes with support for these supplemental JVM languages out of the box. Support includes first-party support, syntax highlighting, code completion, etc. They are first class members of the IntelliJ ecosystem.

Unbelievable code completion and Linting

IntelliJ's code completion is good. Creepily good. It will never fail to resolve the name of some class's methods, if it's your class or some other library's class. It supports sensible completion for the newest of Java features, such as generating lambdas.

Speaking of lambdas, IntelliJ's linting process will often tell you to replace lines of code. It will automatically transform uncessary loops into lambda calls to tidy up your source, or report sources of possible error as you write them.

Stellar Android support
Android Studio, the de-facto IDE for creating Android programs at the moment, is based on IntelliJ. So, in order to install Android Studio, you need only install one plugin for this IDE, retaining all of your other settings you have within it.

The Android plugin includes a drag-and-drop GUI designer, cached emulation for fast in-pc testing of android apps, and the same great code completion that it has for all java source files, which is even more important for Android.

Unlike some other IDE's which support Android, IntelliJ's XML editing leaves almost nothing to be desired.

Next-level debugging
To emphasize just how good the built-in debugger in IntelliJ is, understand that I am no stranger to debugging, inside and outside of IDE's. I've been mucking around with debuggers since it meant crawling through assembly outputs to identify your issue. In that time, I have never come across an IDE as featureful and powerful as IntelliJ's. The debugger is my personal favorite feature of this IDE.

Breakpoints support more feature's than a lot of other IDE's.
You can choose to sleep  just the thread where the breakpoint is set, or the entire thing. You can choose to not even suspend execution on your breakpoints. And, most importantly, you can set a condition for the breakpoint to break on. Even cooler is the fact that the condition line supports code completion.

The debugging process itself is just as featureful as the breakpoints are. It supports all the standard features that a debugger should have, such as "Step Into", "Step out", "Step Over", etc. But it also has some advanced features like "Drop Frame" (for recursive functions), and "Force Step" for situations resistant to debugging... which, surpirsingly, do come up from time to time for some reason or another.

As you debug, the values of local variables are displayed alongside of your code as gray text.

As is the case with many IDE's, IntelliJ supports evaluating expressions at any point during the debugging process.

Integrated VCS management
IntelliJ has top-notch VCS integration. You can pull changes, push changes, edit commit logs, and even apply patches from diff to your project directly.

Other
A feature that, for me, sums up the type of IDE IntelliJ is is "Create gist"
When I saw that the context menu had an option to paste to gist, I was excited. Partly because it makes it easier to ask for help via IRC, but mostly because it shows the type of IDE that IntelliJ is. It is truly the frontrunner in keeping up with the latest in the software industry.

Weaknesses
As biased as I may be for this IDE, I truly feel that IntelliJ doesn't have anything wrong with it as far as the software itself is concerned. I've used this IDE for a bit over one year, at one point for a pretty major project (hundreds of thousands of lines of source, 100+ files). In that year, it has never crashed for me, or behaved in a way that it would not allow me to accomplish my work.

The IDE itself, or some addon, always had whatever feature I was looking for in the Java IDE.

The only problem with the product, in my opinion, is the fact that the non-community edition (which has features that do make it worth it) costs quite a bit of money, and the subscription doesn't particularly make sense. In addition, Jetbrains products are not Open Source or Free, so you can't modify them if you do need to integrate deeply with the IDE, fix some bug, or just see how it's running.

Eclipse
Eclipse, for many years, was considered the "professional" Java development environment. Interestingly, Eclipse is as much a plugin framework as it is a desktop environment. Behind Firefox, it is the second most extensible desktop application ever created. Quite literally, "eclipse" is just a plugin framework that has a lot of good Java editing addons pre-installed. Or C++ editing addons. Or brainfuck editing addons. Or, really, anything you can conceive.


Extensibility
Extensibility within Eclipse is no joke. It gives you the tools to fine-tune everything about the IDE. You can transform the IDE to do anything you want to. It has very good "official" addons for it, such as Android, C++, and Java. On top of that, there are tens of thousands of community plugins, which can turn Eclipse from a Java development environment to, say, a Lisp environment, a Ruby environment, a D environment, or a development environment for just about any language imaginable.

It's rather easy to create a plugin for Eclipse. You do so with plain Java and XML, and you're able to install plugins that you make yourself to fine tune anything about Eclipse that you would like.

Featureful Project management
Eclipse, would like to make sure that it has every possible use-case covered. The user should barely have to do anything with setting their project up manually. This includes things like inheritance and interface implementation. Take a look at the "New Class" dialog box:

This makes it very simple for somebody new to Java to generate a ton of code. Or, if you're extending a large class or implementing a large interface, it streamlines the experience by quite a bit.

Other
Like IntelliJ, Eclipse supports code completion very well (not quite as well as IntelliJ, mind you). It supports debugging, but, once again, not as well as IntelliJ can debug.

However, one key advantage that Eclipse has over IntelliJ is the fact that it is Open Source and completely free to use. You can edit it, or add one plugin to the tens of thousands that already exist. 

If you're a programming multiglot, you should seriously consider Eclipse, since it could become your environemnt for all of your programming languages if you choose for it too. However, if you're looking to purely write Java programs, IntelliJ may be the better option.

Weaknesses
Eclipse has quite a few weaknesses. As it is so extensible, you will often get the feeling that the IDE is "held together with duct tape", as I heard my professor say about Eclipse. 

It uses something called "Perspectives" to display information to the user. These perspectives are essentially windows, and Eclipse is the window manager. That leads to odd behavior like this: 
where you may have to resize your IDE to fit the entire Eclipse window, since the IDE features are themselves plugins. This is a good display of just how truly extensible Eclipse is, however, it's a bit cumbersome to work with if all you're looking to do is edit some Java.

The fact that Eclipse is so extensible also makes the memory usage quite high, even by Java program standards. This is not a problem on most systems, but on slower computer the overhead of being so extensible can really slow things down.

Unlike IntelliJ, there is no baked-in support for version control. However, there are many git / subversion plugins available in the Eclipse repositories to fit these needs.


Netbeans

Netbeans is another traditional Java IDE, like IntelliJ. It's been around for years, and is the default Java IDE (meaning, that Oracle develops it and bundles it with their Java downloads). Despite Oracle having control over Netbeans, the IDE is completely open source.

And, although it is most comparable to IntelliJ, it shares some similarities with Eclipse, including its numerous addons. However, most of the features that you would end up using Netbeans for are pre-installed.

Simplicity
Something can be said about Netbeans that cannot be said about Eclipse. It's dead-simple. It's a traditional editor, reminiscent of code::blocks and the like. Its RAM usage tends to be quite low, and the learning curve for Netbeans is high, making it a great instructional or introductory IDE.

When you create a Java project, it will create a default package and the main class automatically. Unlike IntelliJ and Eclipse, you are ready to execute your java program the second you create it.

Enterprise Support
When you think of Oracle, you think "Enterprise", and Netbeans has features that back this up. It has features, such as allowing you to automatically Globalize your applications with international strings. It allows you to do most things you may need to do in Java through the IDE in pre-defined ways as well. Inheritance, encapsulation, code formatting, comment generation, etc. 

Code Completion
Netbeans has powerful code completion, but it falls short of both Eclipse and IntelliJ. It can automatically fix issues from the context menu, but it won't provide this option as well as Eclipse or IntelliJ does, and it will mostly be able to fix warnings and errors only; it won't improve already working code like IntelliJ. 

The downside with the code completion is that many times it incorrectly assumes what you want. If you put the line import javax.swing.UIManaer; where you actually want javax.swing.UIManager, it will suggest that you create a new local package called javax.swing.UIManaer and write the class yourself.

VCS support
Netbeans has support for all of the major Version Control systems out of the box, including mercurial, git, and subversion. This is one clear advantage it has got over Eclipse, however, the VCS integration doesn't offer all of the features that IntelliJ's does.

Multi-Project support
Netbeans has my favorite multi-project support out of any IDE. It lets you quickly zip back and forth between various projects from the project browser. 
Weaknesses
Netbeans is, in my opinion, the weakest of all the 3 major Java IDE's. It will allow a beginner to get started in Java, but it lacks  so many features that Eclipse and IntelliJ have that it's not worth trying to work around it. The linting is non-existant. The code completion doesn't work 100% of the time. The interface is surprisingly error-prone in contrast with Eclipse, which, since everything is a perspective, you may imagine that it would have a lot of errors.

I stopped using Eclipse a few versions ago, but at one point it crashed and refused to open the project I was working on again. I recall this happening twice during one semester of a java course, which means that it may be common with the IDE. At the time, we were just working with classes for educational purposes. That is, no external libraries or unit tests, which may increase the chances of it crashing.

Other Java IDE's
The only other Java IDE that I have experience with is BlueJ, which I have nothing good to say about. The interface is built around the idea that a problem that literally does not exist should be solved (that using user-defined classes is too difficult). It provides syntax highlighting and rudimentary code completion / debugging / unit test generation, but that is literally all. It isn't worth it to look into BlueJ unless you're going to do so for the novelty of the experience.

Comparison
Here, I will list the scores for some goals you may have for your Java IDE.

Extensibility
Eclipse - 10
IntelliJ - 8
Netbeans - 5

Stability
IntelliJ - 9
Eclipse - 7
Netbeans - 4


Code Completion
IntelliJ - 8
Eclipse - 6
Netbeans - 5

Code Generation
Eclipse - 9
IntelliJ - 8
Netbeans - 4

Debugging
IntelliJ - 10
Eclipse - 5
Netbeans - 3

Unit Tests
IntelliJ - 8
Eclipse - 6
Netbeans - 4

Version Control
IntelliJ - 8
Netbeans - 7
Eclipse - 2 (must be used via addon)

Open Source
Eclipse - 10
Netbeans - 5
IntelliJ - 1

Android
IntelliJ - 10
Eclipse - 8
Netbeans - 4

HTML, CSS, Javascript
IntelliJ - 9
Eclipse - 9
Netbeans - 3

Kotlin, Scala, Clojure
IntelliJ - 10
Eclipse - 9
Netbeans - 0

Learning Curve
Netbeans - 8
IntelliJ - 7
Eclipse - 5

Totals
IntelliJ: 96 / 215
Eclipse: 67 / 215
Netbeans: 52 / 215

You should use IntelliJ if...
You just straight-up want to edit Java source. You don't need insane amounts of extensibility or the ease and official support of using Netbeans. Additionally, if you're going to be working with Android, you want IntelliJ.

You should use Eclipse if...
You need an open-source IDE that is as extensible as it is deep. If your project using many languages and technologies, Eclipse may also be the best bet for you.

You should use Netbeans if...
You are just starting out in the world of Java, or, for some reason, you're not comfortable with IntelliJ or Eclipse. If you are an instructor, you may also want to use Netbeans for your students.

You should use BlueJ if...
You have a mental defect, but don't want it to get in the way of your programming fun.

Hopefully this article helped you make your choice. Good luck!