Javascript dump or print_r output or toSource() alternative output

You can use JSON.stringify to receive a JSON-style string, but sometimes you want an actual javascript notation, not just object notation. In that case, it’s tough to find a reasonable output. I’ve heard that firefox has a toSource method of objects that outputs something like I needed, but I wanted cross-browser compatibility. Here’s how I did it:

function dump(obj, pad)
{
	var output = "";
		if(!pad)
	{
		pad = "";
	}
		if(Array.isArray(obj))
	{
		output +="[\n ";
		for(var o in obj)
		{
			output += "\t"+pad+dump(obj[o], pad+"\t")+",\n";
		}
		output = output.slice(0,-2);
		output +="\n"+pad+"]";
	}
	else if(typeof(obj)==="object")
	{
		output +="{\n ";
		for(var o in obj)
		{
			output += "\t"+pad+o + ":" + dump(obj[o], pad+"\t") + ",\n";
		}
		output = output.slice(0,-2);
		output +="\n"+pad+"}";
	}
	else if(typeof(obj)==="string")
	{
		output += "\"" + obj.replace("\"", "\\\"") + "\"";
	}
	else if(typeof(obj)!=="function")
	{
		output += obj.toString();
	}
	else
	{
		output += "\"FUNCTION\"";
	}
	return output;
}

Google Hangouts™ is a Contender to Solving Remote Team Members

Everyone’s been talking about how remote working is important and how we should be embracing it as a coding culture, but few people have outlined the problems and solutions to fix them.
We’ve begun using a persistent Google Hangout as a means of connecting remote people to our office. The effect has been extraordinary and has really bridged a gap we had all felt in our team.

Some of the problems we’ve faced:

  • People use work from home/remote as vacation
  • Managers unable to verify if people are fully utilized
  • Lack of communication between team members

I’ve chosen these three because they stood out in the company I work for. I’ve been denied work from home requests due to the lack of oversight, and the abuse by people on my team. Honestly, I feel like it’s the output that matters and if we’re pushing things out on time in high quality, we should worry less about hours in front of a computer and effort spent. Convincing other people of that, however is an impossible task and won’t be discussed here, especially since I think we’ve found an excellent compromise.

Some of the facets that make this solution better than things we’ve tried before:

  • Google Hangouts are more like a room where people can meet, not a phone call people occasionally make
  • Remote people can hear what’s going on in the room while they’re out and chime in as though they were present.
  • They also can mute themselves if they don’t want to have home noise affect others.

This has had some unexpected results. People who enter our office say hello to our remote workers when they see they’re Google Hanging out. I feel that the perception of remote working as bad comes from a barrier to communication. If four people are in a room and the boss wants to notify them of something, the boss walks in, says it, and off they go. If the whole team is remote, you send an email, but who knows if they read it or when. If the whole team is on a persistent Google Hangout, the boss can join, say their piece, and be off. No problem. There’s no chance of thinking that our remote worker is slacking off because they’re there, working all the time. Whether they’re looking at Facebook or watching March Madness without us knowing is irrelevant, and isn’t solved by putting people in an office.

Is this a perfect solution for our team? It’s pretty close! We’ve got a good team to begin with, so remote work has never been an issue—the easier communication is just an added bonus. Given that, as someone who occasionally wants to work remotely, the ability to do so with more confidence, happiness from my managers, and being better able to support my team make it something definitely worthwhile.

My Shell and Bash Prompt Configuration

I use Bash every day in my work and at home on projects and I find the default setup of the system to be cumbersome. Call me a geezer, but I missed my dos-style information on the command line. I’ve replicated some of that in the following way:

This is in my ~/.profile file
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/|\1/'
}
PS1="\[\033[1;32m\]\w\$(parse_git_branch)\[\033[0;0m\]\n>"

This code provides a pwd in green and then a simple > character to indicate where your command line begins. This is easy enough to customize — replace the > with a $ if you like that, or change the color with something other than 0;32m.
Then, it creates a pipe and the current git branch (if available)

from arwin.net

abe hassan | blog: off by one on purpose

Here are two problems that we ran into at LiveJournal: In a paginated system, how do you know whether to display the next/previous links? With password prompts being masked out, how can you tell a user that they kept typing but the prompt didnt record more? In the first case: if you display 20 items per page, load 21. If you have 21, throw out the last one and add the pagination links. If you get 20, then you know its exactly right. Doing a database query with a "LIMIT 20" doesnt give you enough information to do this.In the latter case, its also easy: if your system limits passwords to say 15 characters, then make the change-password prompt take 16 characters. If the person typed in exactly 15 characters, then youre great. If you got 16, you can tell them that you didnt save their password the way they think you did.I love this idea. In order to gain certainty about something, you might just need one more piece of information. This trick is a bit clever and comes up every so often.

via abe hassan | blog: off by one on purpose.

ssh_askpass missing on osx

I recently installed tower on my system and tried to connect to a git repo I use. I don’t have my default password set in git (I like typing passwords), so when it tried to prompt me, I didn’t have a means of telling Tower what my password was. This is an oversight in OSX, but hey, we can fix that. This is the error I received
ssh_askpass: exec(/usr/libexec/ssh-askpass): No such file or directory
Permission denied, please try again.

And here’s the language to fix it. Place this code into a file at /usr/libexec/ssh-askpass and make it executable chmod +x /usr/libexec/ssh-askpass and you should be good to go. Tower will prompt you for passwords using a handy dialog.

#! /bin/sh

#
# An SSH_ASKPASS command for MacOS X
#
# Author: Joseph Mocker, Sun Microsystems

#
# To use this script:
# setenv SSH_ASKPASS "macos-askpass"
# setenv DISPLAY ":0"
#

TITLE=${MACOS_ASKPASS_TITLE:-"SSH"}

DIALOG="display dialog \"$@\" default answer \"\" with title \"$TITLE\""
DIALOG="$DIALOG with icon caution with hidden answer"

result=`osascript -e 'tell application "Finder"' -e "activate" -e "$DIALOG" -e 'end tell'`

if [ "$result" = "" ]; then
exit 1
else
echo "$result" | sed -e 's/^text returned://' -e 's/, button returned:.*$//'
exit 0
fi

Flex MXML ArrayCollection cannot ObjectUtil.compare

The ObjectUtil Class has lots of fun things in it that can make your life easier. When I needed to check to see if an ArrayCollection had changed, I thought to myself, why not use ObjectUtil.compare? Turns out that no matter what you do, every ArrayCollection is unique because they produce a UID parameter in every ArrayCollection instance that differentiates it. What’s the purpose of this? I have no idea, but someone thought it was a good idea.

Sadly, I did not find a solution to this problem, so I ended up using an Array instead of an ArrayCollection.

Learning the Basics of Git – Fun!

I’ve been forced to switch from SVN to Git at work. The path has been difficult so far, but there’s a number of good svn to git guides out there, so I won’t really be posting about those. But, as a decent subversion user who’s been forced into git, I’d be remiss in not documenting the realizations I’ve had about the various workflow differences.

git clone url – this is used to pull files from a repo to your computer. This is the equivalent of hey, git, go and copy the main folder of this project to my computer and set it up for version control.

The following command have to be done in a project that’s under version control.

git branch – asks git to remind us which branch we’re on and which branches we have available, should we want to switch to them.

git branch mybranch – This tells git to create a new branch on your computer to which we can switch if we like.

git checkout mybranch – This tells the project to stop working with whatever branch it had and to use the new branch you’ve specified. If you’ve got uncommitted changes to files and you do this, git will prompt you of any conflicts.

git pull – This says, hey git, give me all of the most recent files from the server in the current branch on which I’m working.

git pull branch – This says, hey git, give me all of the most recent files from the server in the branch I’ve specified.

git add my.file – This tells git to start paying attention to the specified file. If you don’t do this, git will assume it’s not supposed to care.

git commit or git commit my.file(s) – Tells git that the state of things is something we want to hang on to. This doesn’t do anything on the server. It just tells git that the way things are now might be something we want to push to the server later. We also have to correlate a checkout with a message.

git push – This takes every commit you’ve made and pushes them up to the server.

Determine Goal Width and Height in Image Resizing

I find myself doing image manipulation on a regular basis. One of the common things I have to do is to fit an image into a max width or height. I’ve distilled this as best I can, and I think this is as simple as it gets. It’s in PHP, but the logic should be applicable to any language.

$max_size=200;
$ratio = $image_width/$image_height;
if($ratio>1)
{
	$goal_width = $max_size;
	$goal_height = $max_size/$ratio;
}
else
{
	$goal_width = $max_size*$ratio;
	$goal_height = $max_size;
}

Add PWD to Bash Default

I grew up with DOS, so call me a throwback user, but when I open Bash on my Mac, I’d really like to know where the heck I am. By default, the prompt shows your computer’s name, then the name of the directory you’re in. This works fine if you never have duplicate directory names, but when I’m doing dev work, I often have multiple windows open for staging/prod/dev work, and they all have the same name as they’re supposed to. This script goes into your ~/.profile file and will change bash to show something like

~/Documents/Workspace/
>

Here’s the code you put into your ~/.profile file (create one if you need to):

PS1="\[\033[0;32m\]\w/\n\[\033[0;0m\]>"

Dynamically Add Collapsible Elements in Jquery Mobile

I spent a long time trying to get this to work and finally success.
This code produces a div with content and then reminds the JQuery Mobile framework to actually wire it up with classes, functionality, etc.

$('<div data-role="collapsible"><h3>Title</h3><p>Content</p></div>').appendTo('[data-role="content"]');
$('div[data-role=collapsible]').collapsible({theme:'c',refresh:true});