Language: PowerShell
Updated Code-Cat PowerShell Function
1: param ([string] $filename) 2: 3: function splitString([string]$string, [int]$length) 4: { 5: $lines = @(); 6: $stringLength = $string.Length; 7: $position = 0; 8: 9: while ($position -lt $stringLength) 10: { 11: if (($position + $length) -lt $stringLength) 12: { 13: $lines += $string.substring($position, $length); 14: } 15: else 16: { 17: $lines += $string.substring($position); 18: } 19: $position += $length; 20: } 21: 22: return $lines; 23: } 24: 25: $codeColor = "Gray"; 26: $counterColor = "White"; 27: $commentColor = "Green"; 28: $newLineColor = "Yellow"; 29: 30: # Window size, minus the counter, spacing, and a bit extra padding. 31: $maxLineSize = $Host.UI.RawUI.WindowSize.Width - 12; 32: 33: # line counter 34: $counter = 0; 35: 36: $content = get-content $filename | 37: % { 38: $counter++; 39: # remove tabs, they're counted as one character, not five. 40: $currentLine = $_.Replace("`t", " "); 41: 42: # set our default color for the current line. 43: $foregroundColor = $codeColor; 44: 45: # Color-code comments green (PowerShell, C#, SQL) 46: if ($currentLine.Trim().StartsWith("#") -or ` 47: $currentLine.Trim().StartsWith("//") -or ` 48: $currentLine.Trim().StartsWith("--")) 49: { 50: $foregroundColor = $commentColor; 51: } 52: write-host ("{0:d4} | " -f $counter) -nonewline -foregroundColor $counterColor; 53: if ($currentLine.Length -ge $maxLineSize) 54: { 55: $splitLines = splitString $currentLine $maxLineSize; 56: $countOfLines = $splitLines.Length; 57: for ($i = 0; $i -lt $countOfLines; $i++) 58: { 59: if ($i -eq 0) 60: { 61: # our first line doesn't look any different. 62: write-host ("{0}" -f $splitLines[$i]) -foregroundColor $foregroundColor; 63: } 64: else 65: { 66: # next lines are using the fancy arrow. 67: write-host ("\--> | ") -nonewline -foregroundColor $newLineColor; 68: write-host ("{0}`n" -f $splitLines[$i]) -nonewline -foregroundColor $foregroundColor; 69: } 70: } 71: } 72: else 73: { 74: write-host ("{0}" -f $_) -foregroundColor $foregroundColor; 75: } 76: }
Tags:
Description:
Updates to my prior Code-Cat function that handles coloring comments and line breaks.
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

