Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Волков Кирилл #165

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
64 changes: 64 additions & 0 deletions TagCloud.ConsoleApp/CommandLine/CommandService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using TagCloud.ConsoleApp.CommandLine.Commands.Interfaces;
using TagCloud.ConsoleApp.CommandLine.Interfaces;
using TagCloud.Utils.ResultPattern;

namespace TagCloud.ConsoleApp.CommandLine;

public class CommandService : ICommandService
{
private readonly Dictionary<string, ICommand> _commands = new();

public CommandService(IEnumerable<ICommand> commands)
{
foreach (var command in commands)
_commands[command.Trigger] = command;
}

public void Run()
{
Console.WriteLine("Введите команду");
var input = Console.ReadLine();

while (true)
{
var result = Execute(input);

switch (result.IsSuccess)
{
case true when result.Value:
return;
case false:
Console.WriteLine(result.Error);
break;
}

Console.WriteLine(Environment.NewLine + "Введите команду");
input = Console.ReadLine();
}
}

public IEnumerable<ICommand> GetCommands()
{
return _commands.Values;
}

public bool TryGetCommand(string name, out ICommand command)
{
return _commands.TryGetValue(name, out command);
}

private Result<bool> Execute(string input)
{
var parameters = input.Split(' ');

if (parameters.Length == 0
|| !_commands.TryGetValue(parameters[0], out var command)
|| parameters.Length == 2 && parameters[1] == "--help")
return _commands["help"].Execute(parameters);

return command.Execute(parameters
.Skip(1)
.Where(e => e != "")
.ToArray());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using TagCloud.ConsoleApp.CommandLine.Commands.Interfaces;
using TagCloud.Domain.Settings;
using TagCloud.Utils.ResultPattern;

namespace TagCloud.ConsoleApp.CommandLine.Commands.Entities;

public class BigToCenterCommand : ICommand
{
private readonly LayoutSettings _layoutSettings;

public BigToCenterCommand(LayoutSettings layoutSettings)
{
_layoutSettings = layoutSettings;
}

public string Trigger => "bigcenter";

public Result<bool> Execute(string[] parameters)
{
return Result
.Of(() => int.Parse(parameters[0]))
.Then(parsed => parsed == 0 || parsed == 1 ? Result.Fail<int>("") : parsed.AsResult())
.ReplaceError(_ => GetHelp())
.Then(parsed => _layoutSettings.BigToCenter = parsed == 1)
.Then(() => false);
}

public string GetHelp()
{
return GetShortHelp() + Environment.NewLine +
"Параметры:\n" +
"int - 1(ближе к центру) или 0(в случайном порядке)\n" +
$"Актуальное значение {_layoutSettings.BigToCenter}";
}

public string GetShortHelp()
{
return Trigger + " позволяет настраивать положение более частых слов";
}
}
43 changes: 43 additions & 0 deletions TagCloud.ConsoleApp/CommandLine/Commands/Entities/ColorCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using TagCloud.ConsoleApp.CommandLine.Commands.Interfaces;
using TagCloud.Domain.Settings;
using TagCloud.Utils.Extensions;
using TagCloud.Utils.ResultPattern;

namespace TagCloud.ConsoleApp.CommandLine.Commands.Entities;

public class ColorCommand : ICommand
{
private readonly VisualizerSettings _visualizerSettings;

public ColorCommand(VisualizerSettings visualizerSettings)
{
_visualizerSettings = visualizerSettings;
}

public string Trigger => "color";

public Result<bool> Execute(string[] parameters)
{
return Result
.Of(() => (int.Parse(parameters[0]), int.Parse(parameters[1]), int.Parse(parameters[2])))
.ReplaceError(_ => GetHelp())
.Then(parsed => parsed.ParseColor())
.Then(color => _visualizerSettings.Color = color)
.Then(() => false);
}

public string GetHelp()
{
return GetShortHelp() + Environment.NewLine +
"Параметры:\n" +
"int - red channel\n" +
"int - green channel\n" +
"int - blue channel\n" +
$"Актуальное значение {_visualizerSettings.Color}";
}

public string GetShortHelp()
{
return Trigger + " позволяет настраивать цвет шрифта";
}
}
57 changes: 57 additions & 0 deletions TagCloud.ConsoleApp/CommandLine/Commands/Entities/DrawCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using TagCloud.ConsoleApp.CommandLine.Commands.Interfaces;
using TagCloud.Domain.Settings;
using TagCloud.Domain.Visualizer.Interfaces;
using TagCloud.Utils.Images.Interfaces;
using TagCloud.Utils.ResultPattern;
using TagCloud.Utils.Words.Interfaces;

namespace TagCloud.ConsoleApp.CommandLine.Commands.Entities;

public class DrawCommand : ICommand
{
private readonly IVisualizer _visualizer;
private readonly IImageWorker _imageWorker;
private readonly FileSettings _fileSettings;
private readonly IWordsService _wordsService;

public DrawCommand(
IVisualizer visualizer,
IImageWorker imageWorker,
FileSettings fileSettings,
IWordsService wordsService)
{
_visualizer = visualizer;
_imageWorker = imageWorker;
_fileSettings = fileSettings;
_wordsService = wordsService;
}

public string Trigger => "draw";

public Result<bool> Execute(string[] parameters)
{
return _wordsService.GetWords(_fileSettings.FileFromWithPath)
.Then(words => _visualizer.Visualize(words))
.Then(image => _imageWorker.SaveImage(
image,
_fileSettings.OutPathToFile,
_fileSettings.ImageFormat,
_fileSettings.OutFileName))
.Then(_ => Console.WriteLine(
$"Изображение было сохранено по пути {
Path.GetFullPath(Path.Combine(_fileSettings.OutPathToFile, _fileSettings.OutFileName))
}"))
.Then(() => true);
}

public string GetHelp()
{
return GetShortHelp() + Environment.NewLine +
"Не имеет параметров";
}

public string GetShortHelp()
{
return Trigger + " позволяет нарисовать облако тегов";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using TagCloud.ConsoleApp.CommandLine.Commands.Interfaces;
using TagCloud.Domain.Settings;
using TagCloud.Utils.ResultPattern;

namespace TagCloud.ConsoleApp.CommandLine.Commands.Entities;

public class ExcludeCommand : ICommand
{
private readonly WordSettings _wordSettings;

public ExcludeCommand(WordSettings wordSettings)
{
_wordSettings = wordSettings;
}

public string Trigger => "exclude";

public Result<bool> Execute(string[] parameters)
{
return parameters.Length < 1
? Result.Fail<bool>(GetHelp())
: Result
.OfAction(() => _wordSettings.Excluded.AddRange(parameters))
.Then(() => false);
}

public string GetHelp()
{
return GetShortHelp() + Environment.NewLine +
"Параметры:\n" +
"string[] - список слов, которые надо исключить через пробел\n" +
"Сейчас исключено " + string.Join(", ", _wordSettings.Excluded);
}

public string GetShortHelp()
{
return Trigger + " позволяет исключить слова из облака тегов";
}
}
23 changes: 23 additions & 0 deletions TagCloud.ConsoleApp/CommandLine/Commands/Entities/ExitCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using TagCloud.ConsoleApp.CommandLine.Commands.Interfaces;
using TagCloud.Utils.ResultPattern;

namespace TagCloud.ConsoleApp.CommandLine.Commands.Entities;

public class ExitCommand : ICommand
{
public string Trigger => "exit";
public Result<bool> Execute(string[] parameters)
{
return true;
}

public string GetHelp()
{
return GetShortHelp();
}

public string GetShortHelp()
{
return Trigger + " завершить выполнение программы";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using TagCloud.ConsoleApp.CommandLine.Commands.Interfaces;
using TagCloud.Domain.Settings;
using TagCloud.Utils.ResultPattern;

namespace TagCloud.ConsoleApp.CommandLine.Commands.Entities;

public class FileNameCommand : ICommand
{
private readonly FileSettings fileSettings;

public FileNameCommand(FileSettings fileSettings)
{
this.fileSettings = fileSettings;
}

public string Trigger => "filename";

public Result<bool> Execute(string[] parameters)
{
return Result
.Of(() => parameters[0])
.ReplaceError(_ => GetHelp())
.Then(name => fileSettings.OutFileName = name)
.Then(() => false);
}

public string GetHelp()
{
return GetShortHelp() + Environment.NewLine +
"Параметры:\n" +
"stirng - filename\n" +
$"Актуальное значение {fileSettings.OutFileName}";
}

public string GetShortHelp()
{
return Trigger + " позволяет настраивать имя файла при сохранении облака тегов";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Aspose.Drawing;
using TagCloud.ConsoleApp.CommandLine.Commands.Interfaces;
using TagCloud.Domain.Settings;
using TagCloud.Utils.Extensions;
using TagCloud.Utils.ResultPattern;

namespace TagCloud.ConsoleApp.CommandLine.Commands.Entities;

public class FontFamilyCommand : ICommand
{
private readonly VisualizerSettings _visualizerSettings;

public FontFamilyCommand(VisualizerSettings visualizerSettings)
{
_visualizerSettings = visualizerSettings;
}

public string Trigger => "fontfamily";

public Result<bool> Execute(string[] parameters)
{
return Result
.OfAction(() =>
{
var a = parameters[0];
Console.WriteLine(a.Length);
})
.ReplaceError(_ => GetHelp())
.Then(_ => string.Join(" ", parameters).ParseFontFamily())
.Then(ff => _visualizerSettings.Font = new Font(ff, _visualizerSettings.Font.Size))
.Then(() => false);
}

public string GetHelp()
{
return GetShortHelp() + Environment.NewLine +
"Параметры:\n" +
"string - название шрифта\n" +
$"Актуальное значение: {_visualizerSettings.Font.FontFamily.Name}\n" +
"Доступные шрифты в системе: " + string.Join(", ", FontFamily.Families.Select(f => f.Name));
}

public string GetShortHelp()
{
return Trigger + " позволяет настраивать начертание шрифта";
}
}
Loading