mirror of
				https://github.com/nsnail/spectre.console.git
				synced 2025-11-04 18:40:50 +08:00 
			
		
		
		
	
				
					committed by
					
						
						Phil Scott
					
				
			
			
				
	
			
			
			
						parent
						
							2e5d18fa78
						
					
				
				
					commit
					fd4b96944e
				
			@@ -29,7 +29,7 @@ namespace Spectre.Console
 | 
			
		||||
        /// <returns>The result of the function.</returns>
 | 
			
		||||
        public static Task<T> RunExclusive<T>(this IAnsiConsole console, Func<Task<T>> func)
 | 
			
		||||
        {
 | 
			
		||||
            return console.ExclusivityMode.Run(func);
 | 
			
		||||
            return console.ExclusivityMode.RunAsync(func);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,48 @@
 | 
			
		||||
using System;
 | 
			
		||||
 | 
			
		||||
namespace Spectre.Console
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Contains extension methods for <see cref="IAnsiConsole"/>.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public static partial class AnsiConsoleExtensions
 | 
			
		||||
    {
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Switches to an alternate screen buffer if the terminal supports it.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="console">The console.</param>
 | 
			
		||||
        /// <param name="action">The action to execute within the alternate screen buffer.</param>
 | 
			
		||||
        public static void AlternateScreen(this IAnsiConsole console, Action action)
 | 
			
		||||
        {
 | 
			
		||||
            if (console is null)
 | 
			
		||||
            {
 | 
			
		||||
                throw new ArgumentNullException(nameof(console));
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (!console.Profile.Capabilities.Ansi)
 | 
			
		||||
            {
 | 
			
		||||
                throw new NotSupportedException("Alternate buffers are not supported since your terminal does not support ANSI.");
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (!console.Profile.Capabilities.AlternateBuffer)
 | 
			
		||||
            {
 | 
			
		||||
                throw new NotSupportedException("Alternate buffers are not supported by your terminal.");
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            console.ExclusivityMode.Run<object?>(() =>
 | 
			
		||||
            {
 | 
			
		||||
                // Switch to alternate screen
 | 
			
		||||
                console.Write(new ControlCode("\u001b[?1049h\u001b[H"));
 | 
			
		||||
 | 
			
		||||
                // Execute custom action
 | 
			
		||||
                action();
 | 
			
		||||
 | 
			
		||||
                // Switch back to primary screen
 | 
			
		||||
                console.Write(new ControlCode("\u001b[?1049l"));
 | 
			
		||||
 | 
			
		||||
                // Dummy result
 | 
			
		||||
                return null;
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user