mirror of
				https://github.com/nsnail/spectre.console.git
				synced 2025-11-04 18:40:50 +08:00 
			
		
		
		
	Add support for rendering exceptions
This commit is contained in:
		
				
					committed by
					
						
						Patrik Svensson
					
				
			
			
				
	
			
			
			
						parent
						
							971f9032ba
						
					
				
				
					commit
					3c3afe7439
				
			@@ -0,0 +1,21 @@
 | 
			
		||||
using System;
 | 
			
		||||
 | 
			
		||||
namespace Spectre.Console
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Contains extension methods for <see cref="IAnsiConsole"/>.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public static partial class AnsiConsoleExtensions
 | 
			
		||||
    {
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Writes an exception to the console.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="console">The console.</param>
 | 
			
		||||
        /// <param name="exception">The exception to write to the console.</param>
 | 
			
		||||
        /// <param name="format">The exception format options.</param>
 | 
			
		||||
        public static void WriteException(this IAnsiConsole console, Exception exception, ExceptionFormats format = ExceptionFormats.None)
 | 
			
		||||
        {
 | 
			
		||||
            Render(console, exception.GetRenderable(format));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -27,10 +27,9 @@ namespace Spectre.Console
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var options = new RenderContext(console.Encoding, console.Capabilities.LegacyConsole);
 | 
			
		||||
            var segments = renderable.Render(options, console.Width).Where(x => !(x.Text.Length == 0 && !x.IsLineBreak)).ToArray();
 | 
			
		||||
            var segments = renderable.Render(options, console.Width).ToArray();
 | 
			
		||||
            segments = Segment.Merge(segments).ToArray();
 | 
			
		||||
 | 
			
		||||
            var current = Style.Plain;
 | 
			
		||||
            foreach (var segment in segments)
 | 
			
		||||
            {
 | 
			
		||||
                if (string.IsNullOrEmpty(segment.Text))
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										22
									
								
								src/Spectre.Console/Extensions/ExceptionExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								src/Spectre.Console/Extensions/ExceptionExtensions.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
			
		||||
using System;
 | 
			
		||||
using Spectre.Console.Rendering;
 | 
			
		||||
 | 
			
		||||
namespace Spectre.Console
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Contains extension methods for <see cref="Exception"/>.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public static class ExceptionExtensions
 | 
			
		||||
    {
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Gets a <see cref="IRenderable"/> representation of the exception.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="exception">The exception to format.</param>
 | 
			
		||||
        /// <param name="format">The exception format options.</param>
 | 
			
		||||
        /// <returns>A <see cref="IRenderable"/> representing the exception.</returns>
 | 
			
		||||
        public static IRenderable GetRenderable(this Exception exception, ExceptionFormats format = ExceptionFormats.None)
 | 
			
		||||
        {
 | 
			
		||||
            return ExceptionFormatter.Format(exception, format);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								src/Spectre.Console/Extensions/StringExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								src/Spectre.Console/Extensions/StringExtensions.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
namespace Spectre.Console
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Contains extension methods for <see cref="string"/>.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public static class StringExtensions
 | 
			
		||||
    {
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Converts the string to something that is safe to
 | 
			
		||||
        /// use in a markup string.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="text">The text to convert.</param>
 | 
			
		||||
        /// <returns>A string that is safe to use in a markup string.</returns>
 | 
			
		||||
        public static string SafeMarkup(this string text)
 | 
			
		||||
        {
 | 
			
		||||
            if (text == null)
 | 
			
		||||
            {
 | 
			
		||||
                return string.Empty;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return text
 | 
			
		||||
                .Replace("[", "[[")
 | 
			
		||||
                .Replace("]", "]]");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user