|
| 1 | +# PowerShell ANSI Color Script Guide |
| 2 | + |
| 3 | +## Critical Rules for Coloring ASCII Art in PowerShell |
| 4 | + |
| 5 | +### 1. **Variable Delimiter Issues** |
| 6 | + |
| 7 | +**Problem:** When using `${variable}` syntax followed by a letter, PowerShell tries to interpret the letter as part of the variable name. |
| 8 | + |
| 9 | +```powershell |
| 10 | +# ❌ WRONG - PowerShell thinks variable is ${gray}P |
| 11 | +"${gray}P" # Outputs nothing because ${gray}P doesn't exist |
| 12 | +
|
| 13 | +# ✅ CORRECT - Use $reset between color changes |
| 14 | +"${reset}${gray}P" # Outputs gray "P" |
| 15 | +``` |
| 16 | + |
| 17 | +**Solution:** Always insert `$reset` between any color variable and following text that starts with a letter. |
| 18 | + |
| 19 | +### 2. **Backtick Escape Sequences** |
| 20 | + |
| 21 | +**Problem:** Backticks (`` ` ``) before certain letters create escape sequences: |
| 22 | +- `` `b `` = backspace |
| 23 | +- `` `d `` = (not defined but causes issues) |
| 24 | +- `` `t `` = tab |
| 25 | +- `` `n `` = newline |
| 26 | +- `` `r `` = carriage return |
| 27 | + |
| 28 | +```powershell |
| 29 | +# ❌ WRONG - The `b gets escaped |
| 30 | +"${white}`[${brightred}`bug${white}`]" |
| 31 | +# Output: "[ug]" - the 'b' disappears! |
| 32 | +
|
| 33 | +# ✅ CORRECT - Use reset to separate |
| 34 | +"${white}[$reset${brightred}bug$reset${white}]" |
| 35 | +``` |
| 36 | + |
| 37 | +### 3. **Dollar Sign Escaping** |
| 38 | + |
| 39 | +**Problem:** Dollar signs ($) need to be escaped with backticks, but this creates alignment issues. |
| 40 | + |
| 41 | +```powershell |
| 42 | +# ❌ WRONG - Literal $ interpreted as variable |
| 43 | +"$$$$$" # PowerShell tries to evaluate variables |
| 44 | +
|
| 45 | +# ✅ CORRECT - Escape each dollar sign |
| 46 | +"`$`$`$`$`$" # Outputs: $$$$$ |
| 47 | +``` |
| 48 | + |
| 49 | +### 4. **Mixing Color Variables and Literal Text** |
| 50 | + |
| 51 | +**Problem:** When transitioning between colors and literal characters, variable expansion can break. |
| 52 | + |
| 53 | +```powershell |
| 54 | +# ❌ WRONG - ${gray}P becomes one variable name |
| 55 | +"$red`$`$`$${gray}P^*" |
| 56 | +
|
| 57 | +# ✅ CORRECT - Insert reset between transitions |
| 58 | +"$red`$`$`$$reset${gray}P^*" |
| 59 | +``` |
| 60 | + |
| 61 | +### 5. **Here-String vs Write-Host Lines** |
| 62 | + |
| 63 | +**Here-String Issues:** |
| 64 | +- Variable interpolation happens all at once |
| 65 | +- Harder to debug specific lines |
| 66 | +- Can have unexpected escape sequence interactions |
| 67 | + |
| 68 | +**Write-Host Line-by-Line:** |
| 69 | +- Each line is independent |
| 70 | +- Easier to debug |
| 71 | +- Better control over color transitions |
| 72 | +- **RECOMMENDED for complex ASCII art** |
| 73 | + |
| 74 | +```powershell |
| 75 | +# ❌ Problematic Here-String |
| 76 | +Write-Host @" |
| 77 | +${gray}Text${red}`$`$`$${gray}More |
| 78 | +"@ |
| 79 | +
|
| 80 | +# ✅ Better approach - Individual lines |
| 81 | +Write-Host "${gray}Text$reset$red`$`$`$$reset${gray}More$reset" |
| 82 | +``` |
| 83 | + |
| 84 | +### 6. **The Reset Pattern** |
| 85 | + |
| 86 | +**Always follow this pattern for color transitions:** |
| 87 | + |
| 88 | +```powershell |
| 89 | +# Pattern: [color][content]$reset[next-color][content]$reset |
| 90 | +
|
| 91 | +Write-Host "$red`$`$`$$reset${gray}P^*^T$reset$red`$`$$reset" |
| 92 | +``` |
| 93 | + |
| 94 | +This ensures: |
| 95 | +- No variable name collisions |
| 96 | +- Clean color boundaries |
| 97 | +- Predictable output |
| 98 | + |
| 99 | +### 7. **Special Characters That Need Backticks** |
| 100 | + |
| 101 | +Characters that need escaping in double-quoted strings: |
| 102 | +- `` ` `` (backtick itself) - ``` `` ``` |
| 103 | +- `$` (dollar) - `` `$ `` |
| 104 | +- `"` (quote) - `` `" `` |
| 105 | +- Newlines/tabs if you DON'T want them - `` `n ``, `` `t `` |
| 106 | + |
| 107 | +**IMPORTANT - Backslash is NOT an escape character in PowerShell:** |
| 108 | + |
| 109 | +Unlike many other languages, backslash (`\`) is a literal character in PowerShell, NOT an escape character. The backtick (`` ` ``) is PowerShell's escape character. |
| 110 | + |
| 111 | +```powershell |
| 112 | +# ✅ CORRECT - Backslash is literal, no escaping needed |
| 113 | +Write-Host "C:\Users\Name" # Outputs: C:\Users\Name |
| 114 | +Write-Host "\.period" # Outputs: \.period |
| 115 | +Write-Host "\-dash" # Outputs: \-dash |
| 116 | +
|
| 117 | +# ❌ WRONG - Don't escape backslash with backtick |
| 118 | +Write-Host "C:\`Users\`Name" # Unnecessary escaping |
| 119 | +Write-Host "\`.period" # Outputs backtick, not what you want |
| 120 | +Write-Host "\`-dash" # Outputs backtick, not what you want |
| 121 | +
|
| 122 | +# ✅ CORRECT - Only escape backticks and dollars |
| 123 | +Write-Host "``backtick" # Outputs: `backtick |
| 124 | +Write-Host "`$dollar" # Outputs: $dollar |
| 125 | +``` |
| 126 | + |
| 127 | +**Common Pattern Mistakes:** |
| 128 | +- `\`` - This is backslash + escaped-backtick (outputs: \`) |
| 129 | +- `\.` - This is just backslash + period (outputs: \.) |
| 130 | +- `\-` - This is just backslash + dash (outputs: \-) |
| 131 | + |
| 132 | +**When working with ASCII art containing backslashes:** |
| 133 | +- Use `\\` to output a single backslash (PowerShell string literal, not escaping) |
| 134 | +- Use `\` followed by any character that's not special |
| 135 | +- Never use `` \` `` unless you specifically want backslash-backtick output |
| 136 | + |
| 137 | +**CRITICAL - Backtick Escaping in Strings:** |
| 138 | + |
| 139 | +When the original ASCII art contains a literal backtick character (`` ` ``), you MUST escape it with another backtick in PowerShell double-quoted strings: |
| 140 | + |
| 141 | +```powershell |
| 142 | +# Original ASCII has: `*TP* |
| 143 | +# ❌ WRONG - Creates escape sequence issues |
| 144 | +Write-Host "\`*TP*" # May cause parser errors |
| 145 | +
|
| 146 | +# ❌ WRONG - Backslash-backtick is NOT the same as double-backtick |
| 147 | +Write-Host "\``*TP*" # This is backslash + escaped-backtick - WRONG! |
| 148 | +
|
| 149 | +# ✅ CORRECT - Double backtick for literal backtick |
| 150 | +Write-Host "``*TP*" # Outputs: `*TP* |
| 151 | +``` |
| 152 | + |
| 153 | +**Common Mistakes with Backslash-Backtick (`\```):** |
| 154 | + |
| 155 | +The pattern `\``` appears to work but causes subtle parser errors: |
| 156 | +- `\``` means: backslash + backtick-escape-for-backtick |
| 157 | +- This creates ambiguous parsing situations |
| 158 | +- Can cause "Unexpected token" errors at seemingly random positions |
| 159 | + |
| 160 | +**Always use double-backtick (``` `` ```) for literal backticks, never `\```** |
| 161 | + |
| 162 | +```powershell |
| 163 | +# ❌ WRONG - All of these use incorrect \` pattern |
| 164 | +Write-Host "${gray}P* ${white}\`*'${gray} _._ \`T$reset" |
| 165 | +
|
| 166 | +# ✅ CORRECT - Use `` for literal backticks |
| 167 | +Write-Host "${gray}P* ${white}``*'${gray} _._ ``T$reset" |
| 168 | +``` |
| 169 | + |
| 170 | +### 8. **Color Variable Definition** |
| 171 | + |
| 172 | +Use RGB ANSI codes for best compatibility: |
| 173 | + |
| 174 | +```powershell |
| 175 | +$esc = [char]27 |
| 176 | +
|
| 177 | +# Basic format: ESC[38;2;R;G;Bm |
| 178 | +$red = "$esc[38;2;238;0;0m" |
| 179 | +$gray = "$esc[38;2;140;140;140m" |
| 180 | +$white = "$esc[38;2;220;220;220m" |
| 181 | +
|
| 182 | +# Bold |
| 183 | +$boldon = "$esc[1m" |
| 184 | +
|
| 185 | +# Always include reset |
| 186 | +$reset = "$esc[0m" |
| 187 | +``` |
| 188 | + |
| 189 | +### 9. **Debugging Alignment Issues** |
| 190 | + |
| 191 | +If output is misaligned: |
| 192 | + |
| 193 | +1. **Check for escape sequence bugs** - Look for backtick before b, d, t, n, r |
| 194 | +2. **Verify dollar escaping** - Each $ should be `` `$ `` |
| 195 | +3. **Test color boundaries** - Add $reset between color changes |
| 196 | +4. **Count visible characters** - ANSI codes don't count toward width |
| 197 | +5. **Test line by line** - Comment out lines to isolate the problem |
| 198 | +6. **Compare character-by-character** - Count $ symbols, spaces, and special characters against original |
| 199 | +7. **Watch for incorrect backslash escaping** - Don't use `` \` `` when you mean `\.` or `\-` |
| 200 | + |
| 201 | +**Character Counting Tips:** |
| 202 | +- Count each literal `$` in the original ASCII (each becomes `` `$ `` in code) |
| 203 | +- Verify spaces match exactly |
| 204 | +- Check that backslashes are literal `\`, not escaped with backtick |
| 205 | +- Ensure color codes don't accidentally consume following characters |
| 206 | + |
| 207 | +### 10. **Best Practices Template** |
| 208 | + |
| 209 | +```powershell |
| 210 | +# Check cache first for instant output |
| 211 | +if (. "$PSScriptRoot\..\ColorScriptCache.ps1") { return } |
| 212 | +
|
| 213 | +$esc = [char]27 |
| 214 | +
|
| 215 | +# Define all colors |
| 216 | +$red = "$esc[38;2;238;0;0m" |
| 217 | +$gray = "$esc[38;2;140;140;140m" |
| 218 | +$white = "$esc[38;2;220;220;220m" |
| 219 | +$boldon = "$esc[1m" |
| 220 | +$reset = "$esc[0m" |
| 221 | +
|
| 222 | +# Use Write-Host per line |
| 223 | +Write-Host "" |
| 224 | +Write-Host "$boldon${red}Title$reset" |
| 225 | +Write-Host "${gray}Text $reset$red`$`$`$$reset${gray} More$reset" |
| 226 | +Write-Host "" |
| 227 | +``` |
| 228 | + |
| 229 | +## Complete Example |
| 230 | + |
| 231 | +```powershell |
| 232 | +# RedHat Bug - Proper Implementation |
| 233 | +$esc = [char]27 |
| 234 | +$red = "$esc[38;2;238;0;0m" |
| 235 | +$gray = "$esc[38;2;140;140;140m" |
| 236 | +$white = "$esc[38;2;220;220;220m" |
| 237 | +$reset = "$esc[0m" |
| 238 | +
|
| 239 | +# Notice the $reset between each color transition |
| 240 | +Write-Host "${gray} .sd$red`$`$`$`$`$`$`$$reset${gray}P^*^T${reset}$red`$`$`$$reset${gray}P^*"*^T${reset}$red`$`$`$`$$reset${gray}bs.$reset" |
| 241 | +``` |
| 242 | + |
| 243 | +## Quick Reference |
| 244 | + |
| 245 | +| Issue | Wrong | Right | |
| 246 | +|-------|-------|-------| |
| 247 | +| Letter after color | `${gray}P` | `$reset${gray}P` | |
| 248 | +| Backtick + letter | ``` ${white}`bug ``` | `$reset${white}bug` | |
| 249 | +| Dollar signs | `$$$` | `` `$`$`$ `` | |
| 250 | +| Color transitions | `$red$$$${gray}P` | `$red`$`$`$$reset${gray}P` | |
| 251 | +| Complex strings | Here-string | Write-Host lines | |
| 252 | +| Backslash escaping | `` \`. `` or `` \`- `` | `\.` or `\-` | |
| 253 | +| Missing dollars | Count mismatch | Count each $ in original | |
| 254 | + |
| 255 | +## Testing Checklist |
| 256 | + |
| 257 | +- [ ] Run script and verify alignment matches original |
| 258 | +- [ ] Check for missing characters (especially b, d, t after backticks) |
| 259 | +- [ ] Verify all dollar signs display correctly |
| 260 | +- [ ] Confirm colors appear as intended |
| 261 | +- [ ] Test with cache disabled |
| 262 | +- [ ] Compare output character-by-character with original ASCII |
| 263 | +- [ ] Count $ symbols in each line matches original exactly |
| 264 | +- [ ] Verify backslashes are literal (not escaped with backtick) |
| 265 | +- [ ] Check that color transitions use $reset properly |
| 266 | +- [ ] Ensure no accidental escape sequences (`` `b ``, `` `d ``, `` `n ``, `` `r ``, `` `t ``) |
| 267 | + |
| 268 | +--- |
| 269 | + |
| 270 | +**Remember:** When in doubt, add more `$reset` markers. They don't add visible characters but ensure clean color transitions. |
0 commit comments