public
Description: My personal dotfiles for vim, powershell, bash and other tools
Homepage: http://winterdom.com/weblog/
Clone URL: git://github.com/tomasr/dotfiles.git
dotfiles / .profile.ps1
100644 212 lines (193 sloc) 6.171 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
###############################################################################
# powershell initialization script
# call from profile.ps1, like this:
# . "$env:HOME\.profile.ps1"
# (notice the '.')
###############################################################################
 
#
# Set the $HOME variable for our use
# and make powershell recognize ~\ as $HOME
# in paths
#
set-variable -name HOME -value (resolve-path $env:Home) -force
(get-psprovider FileSystem).Home = $HOME
 
#
# global variables and core env variables
#
$TOOLS = 'e:\tools'
$SCRIPTS = "$HOME\scripts"
$env:EDITOR = 'gvim.exe'
 
#
# set path to include my usual directories
# and configure dev environment
#
function script:append-path {
   if ( -not $env:PATH.contains($args) ) {
      $env:PATH += ';' + $args
   }
}
 
 
append-path "$TOOLS"
append-path (resolve-path "$TOOLS\svn-*\bin")
append-path (resolve-path "$TOOLS\nant-*")
append-path "$TOOLS\vim"
append-path "$TOOLS\gnu"
append-path "$TOOLS\git\bin"
 
& "$SCRIPTS\devenv.ps1"
& "$SCRIPTS\javaenv.ps1"
 
#
# Define our prompt. Show '~' instead of $HOME
#
function shorten-path([string] $path) {
   $loc = $path.Replace($HOME, '~')
   # remove prefix for UNC paths
   $loc = $loc -replace '^[^:]+::', ''
   # make path shorter like tabs in Vim,
   # handle paths starting with \\ and . correctly
   return ($loc -replace '\\(\.?)([^\\])[^\\]*(?=\\)','\$1$2')
}
 
function prompt {
   # our theme
   $cdelim = [ConsoleColor]::DarkCyan
   $chost = [ConsoleColor]::Green
   $cpref = [ConsoleColor]::Cyan
   $cloc = [ConsoleColor]::Magenta
 
   write-host "$([char]0x0A7) " -n -f $cpref
   write-host ([net.dns]::GetHostName()) -n -f $chost
   write-host ' {' -n -f $cdelim
   write-host (shorten-path (pwd).Path) -n -f $cloc
   write-host '}' -n -f $cdelim
   return ' '
}
 
###############################################################################
# Other helper functions
###############################################################################
function to-hex([long] $dec) {
   return "0x" + $dec.ToString("X")
}
# open explorer in this directory
function exp([string] $loc = '.') {
   explorer "/e,"$loc""
}
# return all IP addresses
function get-ips() {
   $ent = [net.dns]::GetHostEntry([net.dns]::GetHostName())
   return $ent.AddressList | ?{ $_.ScopeId -ne 0 } | %{
      [string]$_
   }
}
# get the public IP address of my
# home internet connection
function get-homeip() {
   $ent = [net.dns]::GetHostEntry("home.winterdom.com")
   return [string]$ent.AddressList[0]
}
# do a garbage collection
function run-gc() {
   [void]([System.GC]::Collect())
}
# launch VS dev webserver, from Harry Pierson
# http://devhawk.net/2008/03/20/WebDevWebServer+PowerShell+Function.aspx
function webdev($path,$port=8080,$vpath='/') {
    $spath = "$env:ProgramFiles\Common*\microsoft*\DevServer\9.0\WebDev.WebServer.EXE"
 
    $spath = resolve-path $spath
    $rpath = resolve-path $path
    &$spath "/path:$rpath" "/port:$port" "/vpath:$vpath"
    "Started WebDev Server for '$path' directory on port $port"
}
 
# start gitk without having to go through bash first
function gitk {
   wish "$TOOLS\git\bin\gitk"
}
 
# uuidgen.exe replacement
function uuidgen {
   [guid]::NewGuid().ToString('d')
}
# get our own process information
function get-myprocess {
   [diagnostics.process]::GetCurrentProcess()
}
# remove .svn directories
function remove-svn($path = '.') {
   ls -r -fo $path | ?{
      $_.PSIsContainer -and $_.Name -match '\.svn'
   } | rm -r -fo
}
# get the syntax of a cmdlet, even if we have no help for it
function get-syntax([string] $cmdlet) {
   get-command $cmdlet -syntax
}
# calculate a hash from a string
function convert-tobinhex($array) {
   $str = new-object system.text.stringbuilder
   $array | %{
      [void]$str.Append($_.ToString('x2'));
   }
   return $str.ToString()
}
function convert-frombinhex([string]$binhex) {
   $arr = new-object byte[] ($binhex.Length/2)
   for ( $i=0; $i -lt $arr.Length; $i++ ) {
      $arr[$i] = [Convert]::ToByte($binhex.substring($i*2,2), 16)
   }
   return $arr
}
function get-hash($value, $hashalgo = 'MD5') {
   $tohash = $value
   if ( $value -is [string] ) {
      $tohash = [text.encoding]::UTF8.GetBytes($value)
   }
   $hash = [security.cryptography.hashalgorithm]::Create($hashalgo)
   return convert-tobinhex($hash.ComputeHash($tohash));
}
function escape-html($text) {
   $text = $text.Replace('&', '&')
   $text = $text.Replace('"', '"')
   $text = $text.Replace('<', '&lt;')
   $text.Replace('>', '&gt;')
}
 
# ugly, ugly, ugly
function to-binle([long]$val) {
   [Convert]::ToString($val, 2)
}
 
function byteToChar([byte]$b) {
   if ( $b -lt 32 -or $b -gt 127 ) {
      '.'
   } else {
      [char]$b
   }
}
function format-bytes($bytes, $bytesPerLine = 8) {
   $buffer = new-object system.text.stringbuilder
   for ( $offset=0; $offset -lt $bytes.Length; $offset += $bytesPerLine ) {
      [void]$buffer.AppendFormat('{0:X8} ', $offset)
      $numBytes = [math]::min($bytesPerLine, $bytes.Length - $offset)
      for ( $i=0; $i -lt $numBytes; $i++ ) {
         [void]$buffer.AppendFormat('{0:X2} ', $bytes[$offset+$i])
      }
      [void]$buffer.Append(' ' *((($bytesPerLine - $numBytes)*3)+3))
      for ( $i=0; $i -lt $numBytes; $i++ ) {
         [void]$buffer.Append( (byteToChar $bytes[$offset + $i]) )
      }
      [void]$buffer.Append("`n")
   }
   $buffer.ToString()
}
function convertfrom-b64([string] $str) {
   [convert]::FromBase64String($str)
}
function normalize-array($array, [int]$offset, [int]$len=$array.Length-$offset) {
   $dest = new-object $array.GetType() $len
   [array]::Copy($array, $offset, $dest, 0, $len)
   $dest
}
 
# load session helpers
."$SCRIPTS\sessions.ps1"
 
###############################################################################
# aliases
###############################################################################
set-alias fortune ${SCRIPTS}\fortune.ps1
set-alias ss select-string
 
###############################################################################
# Other environment configurations
###############################################################################
set-location $HOME