Showing posts with label Software. Show all posts
Showing posts with label Software. Show all posts

Tuesday, June 16, 2009

Using WPF in PowerShell with PowerBoots

I got the bug to play with WPF a couple of weeks ago. I'm relatively familiar with Windows Forms, and thought I'd like to finally look at WPF. I downloaded VC# 2008 Express and messed around a little bit for a few days. I had the goal of writing a WPF program that would host powershell and act like a simple Matlab. Well, I ran across PowerBoots, written by Joel Bennet (http://huddledmasses.org/powerboots/). PowerBoots brings WPF to PowerShell with some extremely simple syntax. After playing with it for a day, I wrote this relatively simple preview-images function that provides an experience similar to the image "Preview" in XP. For a tutorial see http://huddledmasses.org/powerboots-tutorial-walkthrough/


I found a very nice online source code formatter to format the below code: http://www.manoli.net/csharpformat/

The result looks like this:




And Here is the code:


function global:preview-images
{
#by default load all images supported by the WPF image control (System.Windows.Controls.Image)
param($images = (ls *.bmp,*.gif,*.ico,*.jpg,*.png,*.wdp,*.tiff,*.tif ))

$count = $images.length
$global:images = $images
$global:i = 0
$global:imindex = 1 #index of the image control in the dockpanel children array

boots -left 100 -top 100 -width 1024 -title "Preview - $pwd" {

stackpanel{
wrappanel {
#0
button "<--" -FontSize 24 -width 50 -On_Click {
$global:i = 0
$this.parent.parent.children[1].children[$global:imindex].source = $images[$global:i].fullname
$block[0].Inlines.Clear();
$block[0].Inlines.Add($images[$global:i].name)
}
#1
button "<" -FontSize 24 -width 50 -On_Click {
if ($global:i -gt 0) {
$global:i--
}
else {
$global:i = $images.length-1
}
$this.parent.parent.children[1].children[$global:imindex].source = $images[$global:i].fullname
$block[0].Inlines.Clear();
$block[0].Inlines.Add($images[$global:i].name)
}
#2
button ">" -FontSize 24 -width 50 -On_Click {
if ($global:i -lt ($images.length-1)) {
$global:i++
}
else {
$global:i = 0
}
$this.parent.parent.children[1].children[$global:imindex].source = $images[$global:i].fullname
$block[0].Inlines.Clear();
$block[0].Inlines.Add($images[$global:i].name)
}
#3
button "-->" -FontSize 24 -width 50 -On_Click {
$global:i = $count - 1
$this.parent.parent.children[1].children[$global:imindex].source = $images[$global:i].fullname
$block[0].Inlines.Clear();
$block[0].Inlines.Add($images[$global:i].name)
}
#4
button "END" -FontSize 24 -width 75 -On_Click {
#$global:i = $count
$this.parent.parent.parent.close()
#$block[0].Inlines.Clear();
#$block[0].Inlines.Add( $images[$global:i].name)
}
#5
button "Image List" -FontSize 20 -On_Click {
$images | foreach{write-host $_.name}
}
#6
textblock " " -FontSize 30 -FontWeight bold -Foreground "#0000FF"
#7
textblock $images[$global:i].name -OutVariable script:block -FontSize 30 -FontWeight bold -Foreground "#0000FF"
}
dockpanel -lastchildfill $true {

$images | %{$_.name} | listbox -SelectedIndex 0 -maxheight 900 -On_MouseDoubleClick{
$global:i = $this.selectedIndex
$this.parent.children[$global:imindex].source = $images[$global:i].fullname
$block[0].Inlines.Clear();
$block[0].Inlines.Add($images[$global:i].name)
} | ForEach-Object { $_.SetValue(
[System.Windows.Controls.DockPanel]::DockProperty,
[System.Windows.Controls.Dock]::left)
$_
}
image -source $images[$i].fullname
}

}

} -Background (linearGradientBrush $(GradientStop -Offset 0 -Color "#ffffff";GradientStop -Offset 1 -Color "#222222"))
}

Wednesday, May 16, 2007

Customizing your Shell Prompt

I do a lot of work from the command line. This involves both Linux and Windows. The BASH shell is very good and much better than the Windows cmd.exe. Microsoft has finally developed a much better shell called PowerShell (which will be the subject of a future post).

In both shells I find the prompt lacking a bit. The BASH shell only shows you the name of the lowest level directory you are in (i.e. if you are in /work/stuff/more/stuff it only shows "stuff"). The default windows prompts show you the entire path, but if the path is really long then you have only a couple of characters on the line before the command you are typing wraps around. Here is how to fix this problem with both the BASH shell and PowerShell...

I want a prompt that looks like this:
user@machine | /work/stuff/more/stuff
:: Type Command Here

This gives me the full path and I still have a full line to type my command. Also when looking back at previous commands I can see what directory they were executed in.

BASH
For the BASH shell the prompt text can be controlled by the PS1 envrionment variable. For a very good overview see this webpage or this webpage. To accomplish the above I have set my PS1 variable in the .bash_profile file to:

PS1="\e[33;1m\u@\\h | \e[34;1m\w \n\e[37;1m::"

This gives the user name in yellow, the path in blue, and the command text in yellow.

PowerShell
For PowerShell the prompt text is controlled by creating a function in your PowerShell profile file called prompt. The profile file does not exist by default, but PowerShell checks for it on start up. This file should be created as :
C:\Documents and Settings\\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Here is my prompt function:
########################################################
# Prompt
function prompt {
$d = pwd
$u = get-item Env:\USERNAME
$u = $u.value
$c = get-item Env:\COMPUTERNAME
$c = $c.value
$s = "$u@$c | "
$color = "Green"
write-host $s -ForegroundColor "Yellow" -NoNewLine
write-host $d -ForegroundColor "Green"
$promptText = "::";
$title = (get-location).Path;

write-host $promptText -NoNewLine -ForegroundColor "Yellow"
$host.UI.RawUI.WindowTitle = $title;

return " "
}
########################################################

This gives the user name in yellow and the path in green (the default PowerShell background is blue). The command text still ends up being white.

Saturday, April 28, 2007

Text Editor: PSPad

In many fields of endeavor involving a computer a good text editor goes a long way in making your job easier. Formatting offsets and input files for various (often outdated) programs can be a headache even with the right tool, much less the wrong (i.e. notepad). I've tried several over the years. I was using Super Notetab for several years, but recently having switched over to PSPad. PSPad has most the features that I liked in Super Notetab and a host of others. Both programs have the following features:
  • tabbed interface for editing multiple documents
  • multi-file search/replace
PSPad is written to be friendly to programmers. To this end it has syntax hi-lighting and syntax completion for most widely used languages. The user can also add up to five custom defined syntaxes. This is done fairly straightforwardly by defining a syntax (.ini) file. Syntax completion is initiated by pressing ctl+J. If you are feeling a little more adventurous there is also a context file (.def) which allows you to define some of the nuances and structure of the language and also to add some helpful comments to the syntax completion. With the addition of the .def file you get slightly different syntax completion by pressing ctl+SPACE.

You don't need to be dealing with a programming language for syntax coloring to be helpful. If there is one program you deal with a lot, you can just throw the keywords into a syntax file and the syntax highlighting makes the files much more readable and it's easier to catch mistakes.

PSPad also has macros. You specify a compiler or external program to run a certain file type. It can also automatically read a log file generated by the compiler or program. It has several selections modes: normal, column, and line. This is useful for extracting different bits from your file.

My only real complaints about the program are the limit of five custom file types and the print header. Sometimes you would like the filename and date printed on whatever file you have. The header, while optional, is not customizable. This wouldn't be a problem, except the footer (included if you select the header) says "PSPad Editor Version ..." and also lists the computer user's name. It seems an odd selection of footer content.

Screen shot of PSPad

Other text editors that may be worth looking at: Super Notetab, ConTEXT, Crimson Editor (no longer developed as of 2004), and TextPad.

First posting on random Windows software

I use several pieces of software in my day to day work that greatly enhance my productivity. I thought I would start my first blog off by hi-lighting several of these tools in hopes that someone out there finds them useful. I use both Windows and Linux, but do the majority of my work on Windows, so most of the software covered will be for Windows.