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

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

Open
wants to merge 7 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
61 changes: 61 additions & 0 deletions TagCloud.ConsoleApp/CommandLine/CommandService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using TagCloud.ConsoleApp.CommandLine.Commands.Interfaces;
using TagCloud.ConsoleApp.CommandLine.Interfaces;

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)
this.commands[command.Trigger] = command;
}

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

while (true)
{
if (Execute(input))
return;

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 bool Execute(string input)
{
var parameters = input.Split(' ');
try
{
if (parameters.Length == 0 || !commands.TryGetValue(parameters[0], out var command))
return commands["help"].Execute(parameters);

if (parameters.Length == 2 && parameters[1] == "--help")
return commands["help"].Execute(parameters);

return command.Execute(parameters.Skip(1).ToArray());
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using TagCloud.ConsoleApp.CommandLine.Commands.Interfaces;
using TagCloud.Domain.Settings;

namespace TagCloud.ConsoleApp.CommandLine.Commands.Entities;

public class BigToCenterCommand : ICommand
{
private readonly LayoutSettings layoutSettings;

public BigToCenterCommand(LayoutSettings layoutSettings)
{
this.layoutSettings = layoutSettings;
}

public string Trigger => "bigcenter";

public bool Execute(string[] parameters)
{
if (parameters.Length < 1
|| !int.TryParse(parameters[0], out var parsed)
|| parsed != 0 && parsed != 1)
throw new ArgumentException(GetHelp());

layoutSettings.BigToCenter = parsed == 1;
return false;
}

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

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

namespace TagCloud.ConsoleApp.CommandLine.Commands.Entities;

public class ColorCommand : ICommand
{
private readonly VisualizerSettings visualizerSettings;

public ColorCommand(VisualizerSettings visualizerSettings)
{
this.visualizerSettings = visualizerSettings;
}

public string Trigger => "color";

public bool Execute(string[] parameters)
{
if (parameters.Length < 3
|| !int.TryParse(parameters[0], out var red)
|| !int.TryParse(parameters[1], out var green)
|| !int.TryParse(parameters[2], out var blue)
|| !(red, green, blue).TryParseColor(out var color))
throw new ArgumentException(GetHelp());

visualizerSettings.Color = color;
return 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 + " позволяет настраивать цвет шрифта";
}
}
50 changes: 50 additions & 0 deletions TagCloud.ConsoleApp/CommandLine/Commands/Entities/DrawCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using TagCloud.ConsoleApp.CommandLine.Commands.Interfaces;
using TagCloud.Domain.Settings;
using TagCloud.Domain.Visualizer.Interfaces;
using TagCloud.Utils.Files.Interfaces;
using TagCloud.Utils.Images.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)
{
this.visualizer = visualizer;
this.imageWorker = imageWorker;
this.fileSettings = fileSettings;
this.wordsService = wordsService;
}

public string Trigger => "draw";

public bool Execute(string[] parameters)
{
using var image = visualizer.Visualize(wordsService.GetWords(fileSettings.FileFromWithPath));
imageWorker.SaveImage(image, fileSettings.OutPathToFile, fileSettings.ImageFormat, fileSettings.OutFileName);

Console.WriteLine($"Изображение было сохранено по пути {Path.GetFullPath(Path.Combine(fileSettings.OutPathToFile, fileSettings.OutFileName))}");

return 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,38 @@
using TagCloud.ConsoleApp.CommandLine.Commands.Interfaces;
using TagCloud.Domain.Settings;

namespace TagCloud.ConsoleApp.CommandLine.Commands.Entities;

public class ExcludeCommand : ICommand
{
private readonly WordSettings wordSettings;

public ExcludeCommand(WordSettings wordSettings)
{
this.wordSettings = wordSettings;
}

public string Trigger => "exclude";
public bool Execute(string[] parameters)
{
if (parameters.Length < 1)
throw new ArgumentException(GetHelp());

wordSettings.Excluded.AddRange(parameters);

return false;
}

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

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

namespace TagCloud.ConsoleApp.CommandLine.Commands.Entities;

public class ExitCommand : ICommand
{
public string Trigger => "exit";
public 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;

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 bool Execute(string[] parameters)
{
if (parameters.Length < 1)
throw new ArgumentException(GetHelp());

fileSettings.OutFileName = parameters[0];

return 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,45 @@
using Aspose.Drawing;
using TagCloud.ConsoleApp.CommandLine.Commands.Interfaces;
using TagCloud.Domain.Settings;
using TagCloud.Utils.Extensions;

namespace TagCloud.ConsoleApp.CommandLine.Commands.Entities;

public class FontFamilyCommand : ICommand
{
private readonly VisualizerSettings visualizerSettings;

public FontFamilyCommand(VisualizerSettings visualizerSettings)
{
this.visualizerSettings = visualizerSettings;
}

public string Trigger => "fontfamily";

public bool Execute(string[] parameters)
{
if (parameters.Length == 0)
throw new ArgumentException("Введите название шрифта");

if (!parameters[0].TryParseFontFamily(out var fontFamily))
throw new ArgumentException("Данный шрифт не поддерживается в системе\n" + GetHelp());

visualizerSettings.Font = new Font(fontFamily, visualizerSettings.Font.Size);

return 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 + " позволяет настраивать начертание шрифта";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Aspose.Drawing;
using TagCloud.ConsoleApp.CommandLine.Commands.Interfaces;
using TagCloud.Domain.Settings;

namespace TagCloud.ConsoleApp.CommandLine.Commands.Entities;

public class FontSizeCommand : ICommand
{
private readonly VisualizerSettings visualizerSettings;

public FontSizeCommand(VisualizerSettings visualizerSettings)
{
this.visualizerSettings = visualizerSettings;
}

public string Trigger => "fontsize";

public bool Execute(string[] parameters)
{
if (parameters.Length == 0 || !float.TryParse(parameters[0], out var parsed))
throw new ArgumentException(GetHelp());

visualizerSettings.Font = new Font(visualizerSettings.Font.FontFamily, parsed);

return false;
}

public string GetHelp()
{
return GetShortHelp() + Environment.NewLine +
"Параметры:\n" +
"float - размер шрифта\n" +
$"Актуальное значение {visualizerSettings.Font.Size}";
}

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