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
4 changes: 3 additions & 1 deletion src/SeqCli/Cli/Commands/PrintCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using SeqCli.Cli.Features;
using SeqCli.Config;
using SeqCli.Ingestion;
using SeqCli.Output;
using SeqCli.Util;
using Serilog;
using Serilog.Events;
Expand Down Expand Up @@ -72,7 +73,8 @@ protected override async Task<int> Run()
filter = evt => ExpressionResult.IsTrue(compiled(evt));
}

var output = _output.GetOutputFormat(config, _template);
var template = _template == null ? null : PrintTemplate.InterpretEscapeChars(_template);
var output = _output.GetOutputFormat(config, template);

foreach (var input in _fileInputFeature.OpenInputs())
{
Expand Down
59 changes: 59 additions & 0 deletions src/SeqCli/Output/PrintTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright © Datalust Pty Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Text;

namespace SeqCli.Output;

static class PrintTemplate
{
/// <summary>
/// Windows disallows passing literal newlines in process arguments (and it's inconvenient to figure out
/// how to do it with varied shells, anyway). This function provides JSON-like (C#-like) backslash-escaped
/// substitutes for newlines and a few other nonprintable characters.
/// </summary>
public static string InterpretEscapeChars(string template)
{
var result = new StringBuilder();
for (var i = 0; i < template.Length; ++i)
{
var ch = template[i];
if (ch != '\\')
{
result.Append(ch);
continue;
}

if (i == template.Length - 1)
throw new ArgumentException(@"Trailing literal `\` is invalid in templates and must be escaped as `\\`.");

i += 1;
var next = template[i];
result.Append(next switch
{
'\\' => '\\',
't' => '\t',
'r' => '\r',
'n' => '\n',
'e' => '\e',
'b' => '\b',
'f' => '\f',
_ => throw new ArgumentException($@"Sequence `\{next}` is invalid in templates; escape literal `\` as `\\`.")
});
}

return result.ToString();
}
}
30 changes: 30 additions & 0 deletions test/SeqCli.Tests/Output/PrintTemplateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using SeqCli.Output;
using Xunit;

namespace SeqCli.Tests.Output;

public class PrintTemplateTests
{
[Theory]
[InlineData("", "")]
[InlineData("abc", "abc")]
[InlineData(@"{@m}\\{@x}", @"{@m}\{@x}")]
[InlineData(@"{@m}\n{@x}", "{@m}\n{@x}")]
[InlineData(@"{@m}\r\n{@x}", "{@m}\r\n{@x}")]
[InlineData(@"\\\n", "\\\n")]
public void PrintTemplatesAreProcessedCorrectly(string template, string expected)
{
var actual = PrintTemplate.InterpretEscapeChars(template);
Assert.Equal(expected, actual);
}

[Theory]
[InlineData(@"\")]
[InlineData(@"test\")]
[InlineData(@"\q")]
public void InvalidTemplatesAreRejected(string template)
{
Assert.Throws<ArgumentException>(() => PrintTemplate.InterpretEscapeChars(template));
}
}