Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions CosmosDBShell.Tests/CommandTests/ImportCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public async Task ExecuteAsync_DryRun_UsesSelectedOutputFormat(string? format)
ColorSystem = ColorSystemSupport.NoColors,
Out = new AnsiConsoleOutput(output),
});
AnsiConsole.Console.Profile.Width = 200;

var command = new ImportCommand { File = file, DryRun = true, Format = format };
var state = await command.ExecuteAsync(shell, new CommandState(), string.Empty, TestContext.Current.CancellationToken);
Expand Down Expand Up @@ -92,6 +93,40 @@ public async Task ExecuteAsync_DryRun_UsesSelectedOutputFormat(string? format)
}
}

[Fact]
public void PrintState_JsonWithLongLine_DoesNotWrapJsonString()
{
var originalOut = Console.Out;
var originalConsole = AnsiConsole.Console;
using var output = new StringWriter();
using var shell = ShellInterpreter.CreateInstance();
try
{
Console.SetOut(output);
AnsiConsole.Console = AnsiConsole.Create(new AnsiConsoleSettings
{
Ansi = AnsiSupport.No,
ColorSystem = ColorSystemSupport.NoColors,
Out = new AnsiConsoleOutput(output),
});
AnsiConsole.Console.Profile.Width = 40;

var value = new string('x', 100);
var state = new CommandState { Result = new ShellJson(JsonSerializer.SerializeToElement(new { value })) };
state.SetFormat("json");

shell.PrintState(state);

using var document = JsonDocument.Parse(output.ToString());
Assert.Equal(value, document.RootElement.GetProperty("value").GetString());
}
finally
{
Console.SetOut(originalOut);
AnsiConsole.Console = originalConsole;
}
}

[Theory]
[InlineData("items.json", (int)ImportFormat.Json)]
[InlineData("items.jsonl", (int)ImportFormat.Json)]
Expand Down
27 changes: 26 additions & 1 deletion CosmosDBShell/Azure.Data.Cosmos.Shell.Core/ShellInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2104,7 +2104,32 @@ private CommandState PrintStateCore(CommandState state, bool markAsRendered)
var element = (JsonElement?)state.Result.ConvertShellObject(Parser.DataType.Json);
if (element.HasValue)
{
AnsiConsole.MarkupLine(JsonOutputHighlighter.BuildMarkup(element.Value));
var jsonText = state.GenerateOutputText();
var lineStart = 0;
var hasLongLine = false;
for (var index = 0; index < jsonText.Length; index++)
{
if (jsonText[index] == '\n')
{
if (index - lineStart >= AnsiConsole.Console.Profile.Width)
{
hasLongLine = true;
break;
}

lineStart = index + 1;
}
}

if (hasLongLine || jsonText.Length - lineStart >= AnsiConsole.Console.Profile.Width)
{
Console.Out.WriteLine(jsonText);
}
else
{
AnsiConsole.MarkupLine(JsonOutputHighlighter.BuildMarkup(element.Value));
}

return state;
}
}
Expand Down
Loading