Filter ANSI colors when not writing to a terminal
Fixes https://github.com/NixOS/nixpkgs/issues/37114.
This commit is contained in:
		
							parent
							
								
									7b8914825a
								
							
						
					
					
						commit
						4868721506
					
				
					 4 changed files with 10 additions and 8 deletions
				
			
		|  | @ -53,7 +53,7 @@ public: | ||||||
|             prefix = std::string("<") + c + ">"; |             prefix = std::string("<") + c + ">"; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         writeToStderr(prefix + filterANSIEscapes(fs.s) + "\n"); |         writeToStderr(prefix + filterANSIEscapes(fs.s, !tty) + "\n"); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     void startActivity(ActivityId act, Verbosity lvl, ActivityType type, |     void startActivity(ActivityId act, Verbosity lvl, ActivityType type, | ||||||
|  |  | ||||||
|  | @ -1185,7 +1185,7 @@ void ignoreException() | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| std::string filterANSIEscapes(const std::string & s, unsigned int width) | std::string filterANSIEscapes(const std::string & s, bool filterAll, unsigned int width) | ||||||
| { | { | ||||||
|     std::string t, e; |     std::string t, e; | ||||||
|     size_t w = 0; |     size_t w = 0; | ||||||
|  | @ -1210,7 +1210,7 @@ std::string filterANSIEscapes(const std::string & s, unsigned int width) | ||||||
|                 if (i != s.end() && *i >= 0x40 && *i <= 0x5f) e += *i++; |                 if (i != s.end() && *i >= 0x40 && *i <= 0x5f) e += *i++; | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             if (last == 'm') |             if (!filterAll && last == 'm') | ||||||
|                 t += e; |                 t += e; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -391,11 +391,13 @@ void ignoreException(); | ||||||
| #define ANSI_BLUE "\e[34;1m" | #define ANSI_BLUE "\e[34;1m" | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| /* Truncate a string to 'width' printable characters. Certain ANSI
 | /* Truncate a string to 'width' printable characters. If 'filterAll'
 | ||||||
|    escape sequences (such as colour setting) are copied but not |    is true, all ANSI escape sequences are filtered out. Otherwise, | ||||||
|    included in the character count. Other ANSI escape sequences are |    some escape sequences (such as colour setting) are copied but not | ||||||
|    filtered. Also, tabs are expanded to spaces. */ |    included in the character count. Also, tabs are expanded to | ||||||
|  |    spaces. */ | ||||||
| std::string filterANSIEscapes(const std::string & s, | std::string filterANSIEscapes(const std::string & s, | ||||||
|  |     bool filterAll = false, | ||||||
|     unsigned int width = std::numeric_limits<unsigned int>::max()); |     unsigned int width = std::numeric_limits<unsigned int>::max()); | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -308,7 +308,7 @@ public: | ||||||
|         auto width = getWindowSize().second; |         auto width = getWindowSize().second; | ||||||
|         if (width <= 0) std::numeric_limits<decltype(width)>::max(); |         if (width <= 0) std::numeric_limits<decltype(width)>::max(); | ||||||
| 
 | 
 | ||||||
|         writeToStderr("\r" + filterANSIEscapes(line, width) + "\e[K"); |         writeToStderr("\r" + filterANSIEscapes(line, false, width) + "\e[K"); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     std::string getStatus(State & state) |     std::string getStatus(State & state) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue