Skip to content

Commit

Permalink
Add autocompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
JKamsker committed Mar 14, 2024
1 parent 31cf721 commit 5dae0ad
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
*.DotSettings.user
.idea/
.experiments/
nuget.config
nuget.config
/.vs/tone
41 changes: 22 additions & 19 deletions tone/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@
using tone.Metadata.Taggers.IdTaggers.Audible;
using ILogger = Serilog.ILogger;
using Log = Serilog.Log;

using JKToolKit.Spectre.AutoCompletion.Completion;
using JKToolKit.Spectre.AutoCompletion.Integrations;
using tone.SpectreConsoleHelpers;

try
{
var debugMode = args.Contains("--debug");

var settingsProvider = new CustomCommandSettingsProvider();

var services = new ServiceCollection();
Expand All @@ -46,14 +48,14 @@
var loggerSettings = settingsProvider.Get<ILoggerSettings>();
var configBuilder = new ConfigurationBuilder().AddEnvironmentVariables();
var config = new LoggerConfiguration()
.ReadFrom.Configuration(configBuilder.Build());
.ReadFrom.Configuration(configBuilder.Build());

// set loglevel to debug, if --debug is present
if (debugMode && loggerSettings != null)
{
loggerSettings.LogLevel = LogLevel.Debug;
}
}

if (loggerSettings == null || loggerSettings.LogLevel == LogLevel.None)
{
return config.CreateLogger();
Expand All @@ -68,7 +70,7 @@
LogLevel.Warning => config.MinimumLevel.Warning(),
_ => config.MinimumLevel.Error()
};

return config.CreateLogger();
});
services.AddLogging(loggingBuilder => loggingBuilder.SetMinimumLevel(
Expand Down Expand Up @@ -144,27 +146,28 @@
});
});

services.AddSingleton<AudibleIdTaggerSettings>(_ => new AudibleIdTaggerSettings(){
services.AddSingleton<AudibleIdTaggerSettings>(_ => new AudibleIdTaggerSettings()
{
MetadataUrlTemplate = Environment.GetEnvironmentVariable("PILABOR_AUDIBLE_METADATA_URL_TEMPLATE") ?? "",
ChaptersUrlTemplate = Environment.GetEnvironmentVariable("PILABOR_AUDIBLE_CHAPTERS_URL_TEMPLATE") ?? ""
});
services.AddHttpClient<AudibleIdTagger>();

services.AddSingleton<IdTaggerComposite>();

services.AddSingleton(sp =>
{
var fs = sp.GetRequiredService<FileSystem>();
var pathMatcher = sp.GetRequiredService<PathPatternMatcher>();
var chapterFormat = sp.GetRequiredService<ChptFmtNativeMetadataFormat>();
var ffmetadataFormat = sp.GetRequiredService<FfmetadataFormat>();
var idTagger = sp.GetRequiredService<IdTaggerComposite>();
var idTagger = sp.GetRequiredService<IdTaggerComposite>();

var taggers = new[]
{
settingsProvider.Build<IToneJsonTaggerSettings, INamedTagger>(s => new ToneJsonTagger(fs, s)),
settingsProvider.Build<IMetadata, INamedTagger>(s => new MetadataTagger(s)),

settingsProvider.Build<IIdTaggerSettings, INamedTagger>(s =>
{
idTagger.Id = s.Id;
Expand Down Expand Up @@ -208,15 +211,15 @@
return javaScriptApi;
});

// services.AddSingleton(_ => AnsiConsole.Console);
// services.AddLogging(builder => builder.AddSerilog(dispose: true));
// services.AddSingleton<ILogger>(_ => Log.Logger);

// services.AddSingleton(_ => AnsiConsole.Console);
// services.AddLogging(builder => builder.AddSerilog(dispose: true));
// services.AddSingleton<ILogger>(_ => Log.Logger);

var app = new CommandApp(new CustomTypeRegistrar(services));
var app = new CommandApp(new ToneCustomTypeRegistrar(services));

app.Configure(config =>
{
config.AddAutoCompletion(x => x.AddPowershell());
config.SetInterceptor(new CustomCommandInterceptor(settingsProvider));
config.UseStrictParsing();
config.CaseSensitivity(CaseSensitivity.None);
Expand All @@ -241,7 +244,7 @@

/*config.AddCommand<SplitCommand>("split")
.WithDescription("split audio files")
;
;
*/

if (debugMode)
Expand Down
107 changes: 107 additions & 0 deletions tone/SpectreConsoleHelpers/ToneCustomTypeRegistrar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using Microsoft.Extensions.DependencyInjection;

using Sandreas.SpectreConsoleHelpers.DependencyInjection;

using Spectre.Console.Cli;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace tone.SpectreConsoleHelpers;

public class ToneCustomTypeRegistrar : ITypeRegistrar
{
private readonly IServiceCollection _builder;

public ToneCustomTypeRegistrar(IServiceCollection builder)
{
_builder = builder;
}

public ITypeResolver Build()
{
var resolver = new ToneCustomTypeResolver();
_builder.AddSingleton<ITypeResolver>(resolver);
var sp = _builder.BuildServiceProvider();
resolver.Provider = sp;
return resolver;
}

public void Register(Type service, Type implementation)
{
_builder.AddSingleton(service, implementation);
}

public void RegisterInstance(Type service, object implementation)
{
_builder.AddSingleton(service, implementation);
}

public void RegisterLazy(Type service, Func<object> func)
{
Func<object> func2 = func;
if (func2 == null)
{
throw new ArgumentNullException("func");
}

_builder.AddSingleton(service, (IServiceProvider _) => func2());
}
}

public sealed class ToneCustomTypeResolver : ITypeResolver, IDisposable
{
private IServiceProvider _provider;

public IServiceProvider Provider
{
get
{
return _provider;
}
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}

if (_provider != null)
{
throw new InvalidOperationException("Provider has already been set.");
}

_provider = value;
}
}

public ToneCustomTypeResolver()
{
}

public object? Resolve(Type? type)
{
if (!(type == null))
{
if (type == typeof(ITypeResolver))
{
return this;
}

return _provider.GetService(type);
}

return null;
}

public void Dispose()
{
if (_provider is IDisposable disposable)
{
disposable.Dispose();
}
}
}
1 change: 1 addition & 0 deletions tone/tone.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<PackageReference Include="CSharp.OperationResult" Version="0.1.6" />
<PackageReference Include="grok.net" Version="2.0.0" />
<PackageReference Include="Jint" Version="3.0.1" />
<PackageReference Include="JKToolKit.Spectre.AutoCompletion" Version="0.0.5" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
Expand Down

0 comments on commit 5dae0ad

Please sign in to comment.