www.pokeroconnor.com

Mac Terminal Themes and Bash Tips

March5

If you’re tired of the usual terminal color schemes and font style, try this great theme. Something that deserves a mention is Ciaran O’Leary’s blog entry on customizing color schemes in Leapord. This is an essential pre-requisite to any color themes you wish to use. You must install SIMBL first, and then extract the theme file to ~/Library/Application Support/SIMBL/Plugins on your machine. In other words, just place the theme.bundle file in your ~/Library/Application Support/SIMBL/Plugins directory, that is all you need to do, then restart terminal and viola.

However, there are some bash changes needed, some of them essential, before these changes will work. For example, you need to create a .bash_profile file (in your ~ directory) if one did not exist before. There are 2 things you need in that file in order to really make use of these themes. A basic .bash_profile would look like so:

if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
export CLICOLOR=1;
export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'

The if statement just ensures that the contents of your .bashrc file are read. The export CLICOLOR=1; line is essential for the themes to work correctly, for example if you do not include this, doing an ls will not result in any color schemes being rendered. 

The export GREP_OPTIONS line is optional, but one I find very useful, as your grep’s now have the search patterns differentiated in the output, very readable.

This leads on to the .bashrc file, which you should also need to create of none exists. Your aliases should go in this file. Bash aliases are a great way to speed up your work in terminal, from simple stuff like ls shorthands such as alias l=’ls -la’ or alias h=’history’ to more complex ones corresponding to scripts. There’s a tonne of pages about aliases out there of course, here’s just a small taste. I especially like customizing your prompt via PS1, e.g. adding export PS1=”[\u][\h][\#][\w]# “ to your bash_profile file. This would give you a terminal prompt of [user][host][command number][current directory], very useful.

Random Useful Linux Information

March3

Verify SHA1 checksums

Handy little checker on the above page:
#! /bin/bash

hash=$(openssl sha1 $1)
if [ "SHA1(${1})= $2" = "${hash}" ]; then echo "Key is valid."; else echo "Key is _not_ valid!!!"; fi

Great resource for awk examples, and at the bottom of this page some useful greps.

And the classic SSH public keys. Handy starter for crontabs here.

To create a tarball use this syntax: tar -cf [name of tarball].tar [list of files or directories] To gzip it, do:  gzip -9 [file].tar.

Then to unpack the tarball, do: gzip -d file.gz for zip files, and tar -xvf file.tar for .tar files. If you’ve got a .tgz file, just do tar -xvzf file.tgz.

Zend Framework Starters

March2

Here is a collection of the better Zend tutorials, resources and tidbits I have started collecting.

First off, here is probably the best quick tutorial to the ZF, or rather its the best that I have seen so far.

Something obvious, that cost me time, was that when you create a custom class in Zend for which to extend other classes such as controllers from, you must place the new custom class in your applications include path, e.g create a new directory in your applications dir called ‘custom’, and then add this to your damn include path

set_include_path(’../library’ . PATH_SEPARATOR .
‘../application/custom’ . PATH_SEPARATOR . get_include_path());

Also important, in any child classes of the new custom class:

If we want an init()function in any of the child classes, we must also call parent::init() from that class.

Two decent Apress books on the Zend Framework, http://www.amazon.com/Pro-PHP-Patterns-Frameworks-Testing/dp/1590598199 and Practical Web 2.0 Applications with PHP. The latter is especially recommended…

The symlink for library directory on A2 is library -> /usr/lib/php/ZendFramework/latest/library/. Fascinating, I know. To remove a symlink, do unlink symbolic_link.

Zend Forms and Related

Fetching row data from mysql in Zend Framework.

From the official Zend docs, here’s the basics of a form controller – nice.

class UserController extends Zend_Controller_Action
{
public function getForm()
{
// create form as above
return $form;
}

public function indexAction()
{
// render user/form.phtml
$this->view->form = $this->getForm();
$this->render('form');
}

public function loginAction()
{
if (!$this->getRequest()->isPost()) {
return $this->_forward('index');
}
$form = $this->getForm();
if (!$form->isValid($_POST)) {
// Failed validation; redisplay form
$this->view->form = $form;
return $this->render('form');
}

$values = $form->getValues();
// now try and authenticate....
}
}

And $values = $form->getValues(); returns an assoc array of values, like so:

Array ( [username] => pokero [password] => 99998883 [login] => Login )

To start off your form, probably best to do the following – set the form action and method:

$form->setAction('/resource/process')
     ->setMethod('post');

Also, to render a form in a view, just stick this in your view:

 <?php echo $this->form ?>

And here is a list of the validators shipped with the Zend framework (frustratingly hard to find on the Zend site :(  ), which I found on this great ZF blog:

  • Alnum
  • Alpha
  • Barcode
  • Between
  • Ccnum
  • Date
  • Digits
  • EmailAddress
  • Float
  • GreaterThan
  • Hex
  • Hostname
  • InArray
  • Int
  • Ip
  • LessThan
  • NotEmpty
  • Regex
  • StringLength

OK, I eventually found them on the Zend site, here.

And a list of the different form elements:

  • Zend_Form_Element_Button
  • Zend_Form_Element_Captcha
  • Zend_Form_Element_Checkbox
  • Zend_Form_Element_File
  • Zend_Form_Element_Hidden
  • Zend_Form_Element_Hash
  • Zend_Form_Element_Image
  • Zend_Form_Element_MultiCheckbox
  • Zend_Form_Element_Multiselect
  • Zend_Form_Element_Password
  • Zend_Form_Element_Radio
  • Zend_Form_Element_Reset
  • Zend_Form_Element_Select
  • Zend_Form_Element_Submit
  • Zend_Form_Element_Text
  • Zend_Form_Element_Textarea

Form styling and decorators

Will be adding more on decorators here, but for now, just a quick one to sort the annoying dt and dd elements that render the labels on different lines in default forms. Just sticking a <br> tag after each <dd> will sort that:

<script type=”text/javascript”>

$(document).ready(function(){

$(”dd”).after( $(”br”) );

});

</script>

And something that really drove me mad for while trying to get the info, is the really simple way of rendering a different than standard view for an action:

$this->_helper->viewRenderer(’popular’);

This was well hidden in the docs on Zend site, they really try and make shit difficult to find, f*ckers. The thing is most of the examples on the Zend site actually assume you are instantiating a View object from scratch – of course it is explained here that in your actions the view object already exists, and there is a much simpler way.

For setting up an automatic email verification system, this example from Cake PHP is great. And a very nicely written piece of code on validating identical fields. Good one on throwing exceptions.

For an example on a basic Zend Form in action, check out this Lotto Results checker form.

Something else you should really do is change the default htaccess from, for example,

RewriteEngine on

RewriteRule !\.(swf|js|ico|gif|jpg|png|css)$ index.php

to the following, as suggested here. This has the major advantage that you can now add non-framework files easily to your public folder.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php

Useful redirect is return $this->_forward(’index’);

And styling Zend forms with Dojo…simple example and worth a look.

And on that link above, here is some nice simple styling, easily expandable for Zend Forms:

/* Zend Form Styling */
.zend_form, .zend_form_dojo {
width: 700px;
margin: 5px auto;
padding: 2px;
overflow: auto;
}

.zend_form dt, .zend_form_dojo dt {
padding: 0;
clear: both;
width: 15%;
float: left;
text-align: left;
margin: 5px 5px 5px 0;
}

.zend_form dd, .zend_form_dojo dd {
padding: 0;
float: left;
width: 68%;
margin: 5px 2px 5px 0;
}

.zend_form p, .zend_form_dojo p {
padding: 0;
margin: 0;
}

.zend_form input, .zend_form_dojo input, .zend_form textarea, .zend_form_dojo textarea {
margin: 0 0 2px 0;
padding: 0;
}
posted under php | 4 Comments »