Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PowerLineOutput {
- [ConsoleColor]$BackgroundColor
- [ConsoleColor]$ForegroundColor
- [Object]$Content
- [int]$Length
- [bool]$Clear = $false
- PowerLineOutput([hashtable]$values) {
- foreach($key in $values.Keys) {
- if("bg" -eq $key -or "BackgroundColor" -match "^$key") {
- $this.BackgroundColor = $values.$key
- }
- elseif("fg" -eq $key -or "ForegroundColor" -match "^$key") {
- $this.ForegroundColor = $values.$key
- }
- elseif("fg" -eq $key -or "ForegroundColor" -match "^$key") {
- $this.ForegroundColor = $values.$key
- }
- elseif("text" -match "^$key" -or "Content" -match "^$key") {
- $this.Content = $values.$key
- }
- elseif("Clear" -match "^$key") {
- $this.Clear = $values.$key
- }
- else {
- throw "Unknown key '$key' in hashtable. Allowed values are BackgroundColor, ForegroundColor, Content, and Clear"
- }
- }
- }
- [string] GetText() {
- if($this.Content -is [scriptblock]) {
- return & $this.Content
- } else {
- return $this.Content
- }
- }
- [string] ToString() {
- return $(
- if($this.BackgroundColor) {
- [PowerLineOutput]::EscapeCodes.bg."$($this.BackgroundColor)"
- } else {
- [PowerLineOutput]::EscapeCodes.bg.Clear
- }
- ) + $(
- if($this.ForegroundColor) {
- [PowerLineOutput]::EscapeCodes.fg."$($this.ForegroundColor)"
- } else {
- [PowerLineOutput]::EscapeCodes.fg.Clear
- }
- ) + $this.GetText() + $(
- if($this.Clear) {
- [PowerLineOutput]::EscapeCodes.bg.Clear
- [PowerLineOutput]::EscapeCodes.fg.Clear
- }
- )
- }
- static [hashtable] $EscapeCodes = @{
- ESC = ([char]27) + "["
- CSI = [char]155
- Clear = ([char]27) + "[0m"
- fg = @{
- Clear = ([char]27) + "[39m"
- Black = ([char]27) + "[30m"; DarkGray = ([char]27) + "[90m"
- DarkRed = ([char]27) + "[31m"; Red = ([char]27) + "[91m"
- DarkGreen = ([char]27) + "[32m"; Green = ([char]27) + "[92m"
- DarkYellow = ([char]27) + "[33m"; Yellow = ([char]27) + "[93m"
- DarkBlue = ([char]27) + "[34m"; Blue = ([char]27) + "[94m"
- DarkMagenta = ([char]27) + "[35m"; Magenta = ([char]27) + "[95m"
- DarkCyan = ([char]27) + "[36m"; Cyan = ([char]27) + "[96m"
- Gray = ([char]27) + "[37m"; White = ([char]27) + "[97m"
- }
- bg = @{
- Clear = ([char]27) + "[49m"
- Black = ([char]27) + "[40m"; DarkGray = ([char]27) + "[100m"
- DarkRed = ([char]27) + "[41m"; Red = ([char]27) + "[101m"
- DarkGreen = ([char]27) + "[42m"; Green = ([char]27) + "[102m"
- DarkYellow = ([char]27) + "[43m"; Yellow = ([char]27) + "[103m"
- DarkBlue = ([char]27) + "[44m"; Blue = ([char]27) + "[104m"
- DarkMagenta = ([char]27) + "[45m"; Magenta = ([char]27) + "[105m"
- DarkCyan = ([char]27) + "[46m"; Cyan = ([char]27) + "[106m"
- Gray = ([char]27) + "[47m"; White = ([char]27) + "[107m"
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement