Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. class PowerLineOutput {
  2. [ConsoleColor]$BackgroundColor
  3. [ConsoleColor]$ForegroundColor
  4. [Object]$Content
  5. [int]$Length
  6. [bool]$Clear = $false
  7.  
  8. PowerLineOutput([hashtable]$values) {
  9. foreach($key in $values.Keys) {
  10. if("bg" -eq $key -or "BackgroundColor" -match "^$key") {
  11. $this.BackgroundColor = $values.$key
  12. }
  13. elseif("fg" -eq $key -or "ForegroundColor" -match "^$key") {
  14. $this.ForegroundColor = $values.$key
  15. }
  16. elseif("fg" -eq $key -or "ForegroundColor" -match "^$key") {
  17. $this.ForegroundColor = $values.$key
  18. }
  19. elseif("text" -match "^$key" -or "Content" -match "^$key") {
  20. $this.Content = $values.$key
  21. }
  22. elseif("Clear" -match "^$key") {
  23. $this.Clear = $values.$key
  24. }
  25. else {
  26. throw "Unknown key '$key' in hashtable. Allowed values are BackgroundColor, ForegroundColor, Content, and Clear"
  27. }
  28. }
  29. }
  30.  
  31. [string] GetText() {
  32. if($this.Content -is [scriptblock]) {
  33. return & $this.Content
  34. } else {
  35. return $this.Content
  36. }
  37. }
  38.  
  39. [string] ToString() {
  40. return $(
  41. if($this.BackgroundColor) {
  42. [PowerLineOutput]::EscapeCodes.bg."$($this.BackgroundColor)"
  43. } else {
  44. [PowerLineOutput]::EscapeCodes.bg.Clear
  45. }
  46. ) + $(
  47. if($this.ForegroundColor) {
  48. [PowerLineOutput]::EscapeCodes.fg."$($this.ForegroundColor)"
  49. } else {
  50. [PowerLineOutput]::EscapeCodes.fg.Clear
  51. }
  52. ) + $this.GetText() + $(
  53. if($this.Clear) {
  54. [PowerLineOutput]::EscapeCodes.bg.Clear
  55. [PowerLineOutput]::EscapeCodes.fg.Clear
  56. }
  57. )
  58. }
  59.  
  60. static [hashtable] $EscapeCodes = @{
  61. ESC = ([char]27) + "["
  62. CSI = [char]155
  63. Clear = ([char]27) + "[0m"
  64. fg = @{
  65. Clear = ([char]27) + "[39m"
  66. Black = ([char]27) + "[30m"; DarkGray = ([char]27) + "[90m"
  67. DarkRed = ([char]27) + "[31m"; Red = ([char]27) + "[91m"
  68. DarkGreen = ([char]27) + "[32m"; Green = ([char]27) + "[92m"
  69. DarkYellow = ([char]27) + "[33m"; Yellow = ([char]27) + "[93m"
  70. DarkBlue = ([char]27) + "[34m"; Blue = ([char]27) + "[94m"
  71. DarkMagenta = ([char]27) + "[35m"; Magenta = ([char]27) + "[95m"
  72. DarkCyan = ([char]27) + "[36m"; Cyan = ([char]27) + "[96m"
  73. Gray = ([char]27) + "[37m"; White = ([char]27) + "[97m"
  74. }
  75. bg = @{
  76. Clear = ([char]27) + "[49m"
  77. Black = ([char]27) + "[40m"; DarkGray = ([char]27) + "[100m"
  78. DarkRed = ([char]27) + "[41m"; Red = ([char]27) + "[101m"
  79. DarkGreen = ([char]27) + "[42m"; Green = ([char]27) + "[102m"
  80. DarkYellow = ([char]27) + "[43m"; Yellow = ([char]27) + "[103m"
  81. DarkBlue = ([char]27) + "[44m"; Blue = ([char]27) + "[104m"
  82. DarkMagenta = ([char]27) + "[45m"; Magenta = ([char]27) + "[105m"
  83. DarkCyan = ([char]27) + "[46m"; Cyan = ([char]27) + "[106m"
  84. Gray = ([char]27) + "[47m"; White = ([char]27) + "[107m"
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement