Ryan Rampersad's Chronicles
an retired blog of thoughts, opinions, ideas and links
  • About
  • Podcast
  • Links

Archives

git clone to new directory

Normally when you clone a git repository, it will make a directory named after the project in whatever directory you’re working in. Sometimes you might want to make a new directory though instead of using the project based name.

git clone git://github.com/ryanmr/soundboard.git new_directory

This example clones my soundboard, but it will put into a directory called new_directory instead of soundboard.

Custom Post Type Pagination Redirects

If you’re trying to paginate on a custom post type, you’re going to run into some trouble. I’ve been paginating a custom query on the biographies at The Nexus. The way pagination works is it listens only to the top level query, and the built in hooks will redirect a any URL like: example.com/person/ryan-rampersad/page/2/ back to example.com/person/ryan-rampersad/ because it doesn’t know any better. Basically, you’ll have infinite redirects without turning off this type of transparent redirection.

From Justin Tadlock of Hybrid fame.

add_filter( 'redirect_canonical', 'my_disable_redirect_canonical' );

function my_disable_redirect_canonical( $redirect_url ) {

	if ( is_singular( 'client' ) )
		$redirect_url = false;

	return $redirect_url;
}

This with this code, you need to change the is_singular call to check your custom post type’s name.

Upon further digging, I found a WordPress trac ticket about this from two years ago. Yep. I spent hours fiddling with my navigation, various types of URL rewriting and more, just to find out another piece of the magical system was pushing its magic into my neighborhood without any warrant.

Browser Shortcuts Everyone Should Know

My dad and I were in the living room recently and he was using our TV computer.

While using Chrome, he kept mouse over to each little circular x to close each tab. I suggested he try a different shortcut. If you’re using a mouse with a scroll wheel, you can press the scroll wheel to close a tab. Just hover over a tab and click the middle-button and away the tab will go. He liked that since it’s tricky to be precise on the screen with the mouse when you’re ten feet away.

A little later, he accidentally closed a tab he wanted to get back. He was about to retrace his steps through a bunch of links but I suggested a different shortcut. Control + Shift + T will reopen the last tab closed, and continue to go through historically closed tabs too. He really liked that little trick.

Finally, he was typing an email up. Somehow, he got to the word “monotny” but it was underlined so he knew it was wrong (because that’s what Word does when words are wrong). I told him if he right clicks the underlined word in Chrome, it will bring up spelling suggestions from Chrome’s dictionary. I warned him that it’s not same dictionary as the one Google uses to fix keywords when searching. Instead of copy and pasting into a new tab, there was a better way. Like right clicking for built-in word corrections, he could look down the list for ‘Search Google for “monotny”‘. He thought that was pretty helpful too.

In short, my dad learned a lot today while sitting in the living room around the TV computer. That’s pretty good for a rainy day, isn’t it?

C: undefined reference to `pow’

I was helping a friend with a little C-program recently. C is not my language of choice, and when I was tinkering years ago, I was using a Windows compiler and not gcc. Things are a little different in the Ubuntu compilation world. I was receiving an unexpected error message even though I had proper headers.

testing.c:(.text+0x156): undefined reference to `pow’
testing.c:(.text+0x174): undefined reference to `pow’
testing.c:(.text+0x18e): undefined reference to `pow’

#include <stdio.h>
#include <stdlib.h>
#include <math.h> 
#include <time.h>

My code was modestly simple. The variables were previously defined and perfectly in order.

      double left = a * pow(b, 3);
      double right = pow(c, 2) - pow(d, e);

Apparently, in order to make gcc load the proper library, you need to explicitly tell it to in the call to compilation. gcc -lm testing.c -o math && ./testing

Notice the -lm flag? That tells gcc to load a specific library, and in this case, the math library.

Save curl output to bash variable

Saving the contents of a bash curl call is pretty easy.

Variables in bash work something like this.

variable='hi'

Capturing input is relatively easy too.

variable=$(ls -la)

If you echo’d out the contents of variable, you’d print out whatever was in the directory when you ran the command.

So if you put the two together along with curl, you get the answer.

variable=$(curl http://ifupdown.com/apache/logs/log-2011-08-11.log)

Now, if you echo’d out the contents of variable again, you’ll see a gigantic log file! No, not really, but obviously you’ll see the contents of whatever file you want to curl.

Happy curling

Meta Refresh

Doing the classic meta refresh, even with today’s fancy server side and JavaScript tricks, is one of the most reliable and easy ways to transfer a user from one page to another across all browsers. In fact, I used it just today when I needed to redirect users to a special Internet Explorer 6 version of a particular page.

    <meta http-equiv="refresh" content="5; url=home/classic/" />

The URL should obviously be set to the location that you want to user to be redirected to.

As I mentioned needing to redirect Internet Explorer 6 users, I can wrap them in Internet Explorer conditional comments to redirect those visitors only.

<!--[if IE 6}>
    <meta http-equiv="refresh" content="5; url=home/classic/" />
<![endif]-->

Happy refreshing.

How to Write byte[] to a File in Java

For a little project I needed to save lots of index positions from many arrays to file. They were numbers, so they were basically ints, but instead of four bytes, I could get away with a single byte, saving on memory. It was more efficient too save bytes directly instead of parsing strings of integers out later.

I have an array of bytes which comprise my data, then I have an OutputStream with an attached data container file created by a FileOutputStream. All it takes after that is a .write with the data array and finally a .close to close the file stream.

public static void main(String[] args) throws Exception {
 byte[] data = {-1, 0, 0, 1, -1, 0, 1, 1, 1, 0, -1, -1};
 OutputStream out = new FileOutputStream("test.txt");
 out.write(bytes);
 out.close();
}

There are a couple things to keep in mind though. Since this uses the file system, this will may throw exceptions which means you need to catch them. I used throws Exception but you may want to use a try/catch series.

Multiplication Table in Java

One of my friends came to me the other day asking I could help out with the logic for printing a multiplication table out. Go back to those days in second grade when you were learning all the combinations of small numbers from 1 to 12 for multiplication products. That was a long time ago. Quick, what’s 7 times 9?

Anyway, printing out a multiplication table like this one isn’t too hard.

x	1	2	3	4	5	
1	1	2	3	4	5	
2	2	4	6	8	10	
3	3	6	9	12	15	
4	4	8	12	16	20	
5	5	10	15	20	25	

Let’s think of the logic behind this. In the first row, we have the X value, so it counts from 1 up to Xmax. In the first column, we have the Y value, so it counts from 1 up to Ymax. It is duly noted that the table only has a X listed, but that’s for simplicity, though there is a X and a Y, horizontal and vertical.

In the next row, where X is 1 and Y is 1, we have simple multiplication, 1 times 1, so the value is 1. In the second row and column we have the same numbers as the first row and column because of that by 1 multiplication. Now, we’ll take a look at some code.

public static String getNumber(int i, int j) {
	int value = i * j;
	
	if ( i == 0 ) {
		value = (i+1) * j;
	} else if ( j == 0 ) {
		value = i * (j+1);
	}
	
	if ( i == 0 && j == 0 ) return "x";
	else return value+"";
}

So let’s break this down. You have a value which is obvious in its purpose: it’s the resultant multiplication of some row and some column. Then things get complicated. Since we’re, what? Either computer scientists or insane, we count from zero. Counting from zero makes us think about out logic.

If you’re in the first row, you’ll need to print out what would be the next row. What? Imagine you’re making a table for 5 by 5. You’ll need 1 2 3 4 5. How do you get that? You multiply the column value by 1. But you only want this to happen naturally in the first row (which is actually the zeroth row by our zero-count). And we’ll need the same for the columns going down. So that’s what happens in that first if-else-if stack. If i or j is 0, then it will be temporarily incremented by 1 and it’ll print out the proper rows.

The the if-else stack at the end of this statement is pretty easy too. The if-statement checks to see if i == 0 && j == 0, or if i and j are equal to zero, it will return the X that is the first value in the table. If that condition isn’t true, then it’ll print out either the regular i * j value or the specialized first row/column value.

Now we’ll need a loop.

	for (int i = 0; i <= height; i++) {
		for (int j = 0; j <= width; j++) {
			System.out.print( getNumber(i, j) + "\t" );
		}
		System.out.println("");
	}

This is a nested for-loop. The outside loop will handle the rows (e.g. the height) and the inner loop will handle the columns (e.g. the width). There is a method call to the getNumber with arguments i and j, and a printout that has getNumber and appends a tab character at the end. Finally, in the outside loop, when the inner loop is done running, a newline is printed so that the next row will begin to form.

The logic for this type of program is an exercise in looping and conditional logic with index values. You can grab the source code here.

Happy multiplying.

Text Wrapping in TextWrangler

TextWrangler has a strange default window setting where it won’t wrap automatically and won’t push the white background to encapsulate the overflow. Text wrapping is important on a small screen.

Menubar Access - Per-document wrapping

Text wraping is available on a per document basis via TextWrangler > View > Text Display > Soft Wrap Text. The soft means that it won’t add line breaks into your code, it will just display lines so they don’t go off the screen. By default though, the wrapping will only expand to the page guide, or where the white ends and the gray begins, then go to the next line, it seems.

Software Options

Changing the text wrapping globally is possible via TextWrangler > Preferences > Editor Defaults. On the right, there is a checkbox with Soft wrap text a few options. My preference is Window width since full screen editing is awesome.

Happy wrapping.

Tabs to Spaces in TextWrangler

I used TextWrangler for years in high school. It was better than Eclipse for quick and light needs when I was programming in Java. Earlier this year, I decided to stop using the tab key and replace them with spaces.

TextWrangler is setup by default to use tabs. Overriding is a little obscure. Go to TextWrangler > Preferences > Editor Defaults. Once there, tick the Auto-expand tabs checkbox and you can specify the number of spaces per tab-key in the Default font selector below.

Happy tabbing!

Useful Mail.app Shortcuts

I don’t mind Mail.app for most uses, it’s nice to have a native mail application. In Gmail, I use shortcuts. These are some of the shortcuts I’ve discovered in Mail.

  • Cmd+N – Starts a new message
  • Cmd+Control+N – Starts a new note (like a self-email)
  • Cmd+R – Replies to a highlighted message
  • Cmd+Shift+D – Sends a message
  • Cmd+Shift+N – Refreshes the inboxes
  • delete – Removes the message from the inbox (but leaves it in All Mail)

These shortcuts aren’t hard to find but unlike Windows, there are more functional keys on the Mac. This means there are more combinations and shortcuts than I would’ve expected. There are of course numerous other shortcuts but they’re a little hard to find in a single location.

Option + 3-Finger Swiping for Back/Forward in OSX Lion

I asked, “Where is 2-Finger Swiping in Finder & OSX Lion?”. Tinkering takes time but I finally found a way to get closer to the Safari-like swiping I want.

Go back or forward? No way!

You can go back and forward with a key hold in combination with a swipe. To do it, hold option and 3-finger swipe like you would in Safari with 2-fingers to go back or forward. That should do it.

I like my system to be as default as possible; as close to the original fresh installation as possible. I’ve lived like this for years on Windows and even while I tinkered with Linux. The closer the system is to acceptable on default, the better.

Unify the gestures, Apple.

The latest version of Chrome supports 2-finger swipes to go back and forward now too, but it’s still not on par with Safari.

Import Gmail Contacts into Address Book

I just got this MacBook Air yesterday and I’m already discovering things about it. For instance, when I synced Gmail with Mail.app, it didn’t sync my contacts along with it. I’m pretty sure that it did on the iPod touch. For the most part, the solutions I found online weren’t exactly quick and easy nor were they very up to date. With 10.7 Lion and the new Address Book app, it’s probably a bit newer.

To get those Gmail or Google contacts imported into the system-wide Address Book, most people pointed to a little export button in Gmail itself, located under Contacts > More Actions > Export. This method works well enough for importing specific groups but if you add people to those groups via your Android phone or via the web interface, it doesn’t sync.

Export to Google Contacts

I found that a better solution is to allow the Address Book sync with your Google contacts directly. To setup sync, go to Address Book > Preferences > Accounts > Synchronize with Google. That will let you configure your account and that should export your main group of contacts and sync them in the future.

Address Book Sync

While you can’t export specific groups with this method, I feel for general use, sync with mixed devices is probably the better route to go.

Happy contacting!

Hand Typed Path in Nautilus – Ubuntu

I’m not sure when the little pencil icon disappeared from the Nautilus window. In Windows 7, I love being able to edit the path in the window to jump anywhere in the system quickly. This is even more useful in Linux actually because there are loads of hidden folders you cannot get to unless you jump there directly. You’d think the Ubuntu equivalent to Windows Explorer would offer this feature in a more obvious way.

When I look at my home directory, /home/ryan, all I see is the two buttons in the path that make up those directories. That’s great because I can jump between the levels easily, but it doesn’t allow me to jump anywhere else outside that hierarchy.

Luckily, you can hand type a path in place of those jumping navigation buttons easily. Simply use CTRL + L. This will transition the buttons to an input box where you can easily type out any path you want. It’ll even attempt to auto-complete for you just as if you were running in the terminal. When you’re ready to jump, hit enter and off you go. I demonstrated above how I navigated with the by hand editor to jump to my /home/ryan/.config folder which is normally hidden from display.

That’s all there is to typing paths by hand in Nautilus on Ubuntu!

SSH, SCP and SOCKS

SSH and SFTP are easy to setup.

For SSH, it’s as easy as.

ssh ryan@244.103.24.242

While SSH can send commands to the remote computer, files need secure copy, or SCP..

scp -r /home/ryan/www/files/ ryan@244.103.24.242:/home/ryan/www/files

The -r switch is for copying directories recursively. If it were just a single file being copied, then it wouldn’t be needed.

Covering commands and files, seeing the web as the remote computer does is quite useful as well. To do that, sockets are required.

SSH -D 8899 ryan@244.103.24.242

This will make a tunnel to the remote computer at localhost:8899. To finish, set your browser’s proxy settings to run off of localhost:8899 and that should do it.

Next>

© 2021 Ryan Rampersad's Chronicles.