Skip to content

Commit

Permalink
Clear the terminal via the LSP (#1108)
Browse files Browse the repository at this point in the history
* Clear the terminal via the LSP

* use actual Clear-Host impl

* add clear for only Windows
  • Loading branch information
TylerLeonhardt authored Nov 27, 2019
1 parent 3591ee1 commit e813fbd
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ FunctionsToExport = @('Register-EditorCommand',
'Join-ScriptExtent',
'Test-ScriptExtent',
'Open-EditorFile',
'New-EditorFile')
'New-EditorFile',
'Clear-Host')

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
Expand All @@ -87,7 +88,7 @@ CmdletsToExport = @()
VariablesToExport = @()

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = @('psedit')
AliasesToExport = @('psedit', 'cls', 'clear')

# DSC resources to export from this module
# DscResourcesToExport = @()
Expand Down
18 changes: 18 additions & 0 deletions module/PowerShellEditorServices/Commands/Public/Clear-Host.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
#

Microsoft.PowerShell.Management\Get-Item function:Clear-Host | Microsoft.PowerShell.Management\Set-Item function:__clearhost

function Clear-Host {
[Alias('cls')]
param()

__clearhost
$psEditor.Window.Terminal.Clear()
}

if (!$IsMacOS -or $IsLinux) {
Set-Alias -Name clear -Value Clear-Host
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,10 @@ public async Task SetStatusBarMessageAsync(string message, int? timeout)
Timeout = timeout
});
}

public void ClearTerminal()
{
_languageServer.SendNotification("editor/clearTerminal");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

namespace Microsoft.PowerShell.EditorServices.Services.PowerShellContext
{
/// <summary>
/// Provides a PowerShell-facing API which allows scripts to
/// interact with the editor's terminal.
/// </summary>
public class EditorTerminal
{
#region Private Fields

private readonly IEditorOperations editorOperations;

#endregion

#region Constructors

/// <summary>
/// Creates a new instance of the EditorTerminal class.
/// </summary>
/// <param name="editorOperations">An IEditorOperations implementation which handles operations in the host editor.</param>
internal EditorTerminal(IEditorOperations editorOperations)
{
this.editorOperations = editorOperations;
}

#endregion

#region Public Methods

/// <summary>
/// Triggers to the editor to clear the terminal.
/// </summary>
public void Clear()
{
this.editorOperations.ClearTerminal();
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ public class EditorWindow
{
#region Private Fields

private IEditorOperations editorOperations;
private readonly IEditorOperations editorOperations;

#endregion

#region Public Properties

/// <summary>
/// Gets the terminal interface for the editor API.
/// </summary>
public EditorTerminal Terminal { get; private set; }

#endregion

Expand All @@ -26,6 +35,7 @@ public class EditorWindow
internal EditorWindow(IEditorOperations editorOperations)
{
this.editorOperations = editorOperations;
this.Terminal = new EditorTerminal(editorOperations);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,10 @@ public interface IEditorOperations
/// <param name="timeout">If non-null, a timeout in milliseconds for how long the message should remain visible.</param>
/// <returns>A Task that can be tracked for completion.</returns>
Task SetStatusBarMessageAsync(string message, int? timeout);

/// <summary>
/// Triggers to the editor to clear the terminal.
/// </summary>
void ClearTerminal();
}
}

0 comments on commit e813fbd

Please sign in to comment.