Tuesday, October 5, 2010

Terminal Prompt Coloring and Line-Wrap Issues

The other day I was look scrolling back through my terminal window and needed to be able to pick out the prompts so I could differentiate output from different commands.  So I edited my ~/.profile file and added this to the bottom:

export PS1="[\e[36m\u\e[33m@\e[32m\W\e[0m]\e[32m > \e[0m"

To give me a prompt that looks like:



However, whenever I would use a command that was longer than the line, and it needed to wrap, the formatting would get all funky and the terminal wouldn't correctly represent either the command that I had entered or the position of the cursor.

I asked a colleague of mine about it and he suggested that it was because of the coloring of the prompt and that the non-printable characters (the ones that are for coloring) caused the line editor to display incorrectly. So I researched it further to realize that my PS1 string was missing the non-printable character string delimiters: \[ ... \]

So I edited my PS1 variable, wrapping all non-printing characters with \[ and \]

export PS1="[\[\e[36m\]\u\[\e[33m\]@\[\e[32m\]\W\[\e[0m\]]\[\e[32m\] > \[\e[0m\]"


and PRESTO line wrapping issues are gone!

So, remember: if you ever see weirdness in the terminal formatting check out your prompt definition first!

For a decent guide to creating a nice prompt check out Prompt Magic.

Thursday, September 9, 2010

Environment/Resource problems with Tomcat 7 in Eclipse

When developing JEE applications it is common practice (and very good practice in my humble opinion) to read in environment specific configurations from the container via JNDI.  Up until today I used Tomcat 6 with Eclipse and WST to develop on my local box.  I would edit the server.xml file to add my environment variables and my container managed database connections. The result looks somelthing like this:

...
<resource auth="Container" 
   driverclassname="com.mysql.jdbc.Driver"
   maxactive="100" maxidle="30"
   maxwait="10000" name="jdbc/mydb"
   password="root" type="javax.sql.DataSource"
   url="jdbc:mysql://localhost/mydb?autoReconnect=true"
   username="" validationquery="SELECT 1;" />
<environment name="var1" 
   type="java.lang.String" value="foo"/>
<environment name="var2" 
   type="java.lang.String" value="bar"/>
...

Today, I tried to move to using Tomcat 7--not for any particular reason except that I like to stay up on using the latest and greatest and since 7 > 6 it must be the latest; it remains to be seen whether or not it is the greatest.

When I tried to start using Tomcat 7 for local development I started by using the same setup I used for Tomcat 6 above. Everything looked good except that when I would try to start the server from the Servers tab, I would get weird NullPointerExceptions that had no apparent reason.






I tried taking the Resource and Environment stuff out of server.xml and the server would start up like a charm (except all the errors that it would throw because of my missing environment variables).

The fix is to double-click the Tomcat 7 server in the Servers tab and check the box next to Publish module contexts to separate XML files in the Server Options section.










Apparently, the startup order has changed between Tomcat 6 and Tomcat 7 causing some unhappiness.  I found the solution at the Eclipse Community Forum.