-
Notifications
You must be signed in to change notification settings - Fork 368
/
OutDisplayCommand.cs
66 lines (59 loc) · 2.3 KB
/
OutDisplayCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.DotNet.Interactive.Events;
using System.Management.Automation;
using static Microsoft.DotNet.Interactive.Kernel;
namespace Microsoft.DotNet.Interactive.PowerShell.Commands
{
/// <summary>
/// Takes the the input and displays it on the client using .NET Interactive's formatters.
/// This returns a DisplayedValue that can then be updated by calling `Update` on the object.
/// </summary>
[Cmdlet(VerbsData.Out, "Display", DefaultParameterSetName = "MimeType")]
[OutputType("Interactive.Events.DisplayedValue")]
[Alias("od")]
public sealed class OutDisplayCommand : PSCmdlet
{
/// <summary>
/// The object from pipeline.
/// </summary>
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
[AllowNull]
public object InputObject { get; set; }
/// <summary>
/// The MimeType to use.
/// </summary>
[Parameter(Position = 1, ParameterSetName = "MimeType")]
[ValidateSet(
"application/javascript",
"application/json",
"text/html",
"text/markdown",
"text/plain"
)]
public string MimeType { get; set; } = "text/html";
/// <summary>
/// If the user wants to send back a MimeType that isn't in the MimeType parameter,
/// they can use this.
/// </summary>
[Parameter(Position = 1, ParameterSetName = "CustomMimeType")]
public string CustomMimeType { get; set; }
/// <summary>
/// Determines whether the DisplayedValue should get written to the pipeline.
/// </summary>
[Parameter()]
public SwitchParameter PassThru { get; set; }
/// <summary>
/// ProcessRecord override.
/// </summary>
protected override void ProcessRecord()
{
object obj = InputObject is PSObject psObj ? psObj.Unwrap() : InputObject;
DisplayedValue displayedValue = display(obj, MimeType);
if (PassThru.IsPresent)
{
WriteObject(displayedValue);
}
}
}
}