Skip to content

Commit 49e2f0e

Browse files
Code refactoring
1 parent 9a62cd7 commit 49e2f0e

File tree

13 files changed

+64
-72
lines changed

13 files changed

+64
-72
lines changed
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using vv.Core;
1+
using vv.CLI;
2+
using vv.CLI.Rendering;
23

34
internal abstract class BaseAsyncCommand<TSettings> : AsyncCommand<TSettings> where TSettings : CommandSettings
45
{
@@ -11,23 +12,15 @@ public sealed override async Task<int> ExecuteAsync(CommandContext context, TSet
1112
}
1213
catch (UserException e)
1314
{
14-
WriteError(e.Message);
15+
DefaultRendering.WriteError(e.Message);
1516
return 1;
1617
}
1718
catch (Exception e)
1819
{
19-
WriteError(e.Message);
20+
DefaultRendering.WriteError(e.Message);
2021
return 2;
2122
}
2223
}
2324

2425
protected abstract Task<int> ExecuteImpl(TSettings settings, CancellationToken token);
25-
26-
protected static void WriteError(Exception exception) =>
27-
AnsiConsole.WriteException(exception);
28-
29-
protected static void WriteError(string message) =>
30-
AnsiConsole.MarkupLine($"[red]Error: {Markup.Escape(message)}[/]");
31-
32-
protected static void WriteSuccess(string message) => AnsiConsole.MarkupLine($"[green]{Markup.Escape(message)}[/]");
3326
}

src/vv/CLI/Commands/BaseCommand.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using vv.Core;
1+
using vv.CLI;
2+
using vv.CLI.Rendering;
23

34
internal abstract class BaseCommand<TSettings> : Command<TSettings> where TSettings : CommandSettings
45
{
@@ -11,23 +12,15 @@ public sealed override int Execute(CommandContext context, TSettings settings,
1112
}
1213
catch (UserException e)
1314
{
14-
WriteError(e.Message);
15+
DefaultRendering.WriteError(e.Message);
1516
return 1;
1617
}
1718
catch (Exception e)
1819
{
19-
WriteError(e.Message);
20+
DefaultRendering.WriteError(e.Message);
2021
return 2;
2122
}
2223
}
2324

2425
protected abstract int ExecuteImpl(TSettings settings);
25-
26-
protected static void WriteError(Exception exception) =>
27-
AnsiConsole.WriteException(exception);
28-
29-
protected static void WriteError(string message) =>
30-
AnsiConsole.MarkupLine($"[red]Error: {Markup.Escape(message)}[/]");
31-
32-
protected static void WriteSuccess(string message) => AnsiConsole.MarkupLine($"[green]{Markup.Escape(message)}[/]");
3326
}

src/vv/CLI/Commands/BaseSettings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace vv.CLI.Settings;
55
internal class BaseSettings : CommandSettings
66
{
77
[CommandOption("--path")]
8-
[Description("Path to repository. Use when you don't need to use selection prompt")]
8+
[Description("Path to repository. Use when you don't need selection prompt")]
99
public string RepoPath { get; init; }
1010

1111
[CommandOption("--disrespect-gitignore")]
12-
[Description("Ignore all .gitignore files")]
12+
[Description("Ignore .gitignore")]
1313
public bool DisrespectGitIgnore { get; init; }
1414
}

src/vv/CLI/Commands/Git/GitSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace vv.CLI.Settings;
55
internal class GitSettings : BaseSettings
66
{
77
[CommandOption("--branch")]
8-
[Description("Display basic repository and specified branch info")]
8+
[Description("Also display specified branch info")]
99
public string BranchName { get; init; }
1010
}

src/vv/CLI/Commands/Languages/LanguagesCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ await DefaultRendering.Spinner("Starting processing...", async ctx =>
2525
ctx.Status("Parsing tokei output...");
2626
var langsData = Tokei.ParseLanguageData(jsonOutput);
2727

28-
ctx.Status("Running founded languages from languages.yml ...");
29-
fullLangsData = LanguagesYml.GetLangsColors(langsData, settings.IgnoreDocsLangs);
28+
ctx.Status("Getting founded languages colors from languages.yml ...");
29+
fullLangsData = LanguagesYml.GetLangsColors(langsData, settings.IgnoreExtraLangs);
3030
});
3131

3232
AnsiConsole.WriteLine();

src/vv/CLI/Commands/Languages/LanguagesSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ internal class LanguagesSettings : BaseSettings
66

77
[CommandOption("--ignore-extra-langs")]
88
[Description("Ignore extra languages like Markdown, JSON, MSBuild, etc.")]
9-
public bool IgnoreDocsLangs { get; init; }
9+
public bool IgnoreExtraLangs { get; init; }
1010

1111
[CommandOption("--fetch-latest")]
1212
[Description("Delete old \"languages.yml\" and download new from official repository")]

src/vv/CLI/Commands/Setup/SetupCommand.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using vv.CLI.Settings;
1+
using vv.CLI.Rendering;
2+
using vv.CLI.Settings;
23
using vv.Core;
34

45
namespace vv.CLI.Commands;
@@ -12,9 +13,9 @@ protected override async Task<int> ExecuteImpl(SetupSettings settings, Cancellat
1213
{
1314
var installed = await Tokei.CheckIfTokeiInstalled();
1415
if (installed)
15-
AnsiConsole.MarkupLine("[green]All required dependencies are satisfied![/]");
16+
DefaultRendering.WriteSuccess("All required dependencies are satisfied!");
1617
else
17-
WriteError("Install tokei and add it to PATH");
18+
DefaultRendering.WriteError("Install Tokei and add it to PATH");
1819
}
1920

2021
if (AnsiConsole.Confirm("Do you want to locate folder for repositories?"))
@@ -23,11 +24,11 @@ protected override async Task<int> ExecuteImpl(SetupSettings settings, Cancellat
2324
.Replace("\"", "");
2425

2526
if (string.IsNullOrEmpty(path))
26-
WriteError("Path cannot be empty");
27+
DefaultRendering.WriteError("Path cannot be empty");
2728

2829
SetupHandle.WriteSetupToJson(new(path));
2930

30-
AnsiConsole.MarkupLine($"[dim white]Settings were written to {SetupHandle.SetupFilePath}![/]");
31+
DefaultRendering.WriteSuccess($"Settings were written to {SetupHandle.SetupFilePath}!");
3132
}
3233

3334
return 0;

src/vv/CLI/Rendering/DefaultRendering.cs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,25 @@
22

33
internal static class DefaultRendering
44
{
5-
public static void Progress(Action<ProgressContext> ctx)
6-
{
5+
public static void Progress(Action<ProgressContext> ctx) =>
76
AnsiConsole.Progress()
8-
.AutoClear(true)
9-
.Columns(
10-
new SpinnerColumn()
11-
{
12-
Spinner = Spectre.Console.Spinner.Known.Dots,
13-
Style = Style.Parse("bold blue")
14-
},
15-
new TaskDescriptionColumn(),
16-
new ProgressBarColumn()
17-
{
18-
CompletedStyle = new Style(Color.Green),
19-
FinishedStyle = new Style(Color.Lime),
20-
RemainingStyle = new Style(Color.Grey)
21-
},
22-
new ElapsedTimeColumn())
23-
.Start(ctx);
24-
}
25-
7+
.AutoClear(true)
8+
.Columns(
9+
new SpinnerColumn()
10+
{
11+
Spinner = Spectre.Console.Spinner.Known.Dots,
12+
Style = Style.Parse("bold blue")
13+
},
14+
new TaskDescriptionColumn(),
15+
new ProgressBarColumn()
16+
{
17+
CompletedStyle = new Style(Color.Green),
18+
FinishedStyle = new Style(Color.Lime),
19+
RemainingStyle = new Style(Color.Grey)
20+
},
21+
new ElapsedTimeColumn())
22+
.Start(ctx);
23+
2624
public static async Task Spinner(string status, Func<StatusContext, Task> action)
2725
{
2826
await AnsiConsole.Status()
@@ -31,12 +29,10 @@ await AnsiConsole.Status()
3129
.StartAsync(status, action);
3230
}
3331

34-
public static void Rule(string title)
35-
{
32+
public static void Rule(string title) =>
3633
AnsiConsole.Write(new Rule(title)
3734
.RuleStyle(new Style(Color.Grey)));
38-
}
39-
35+
4036
public static TResult Prompt<TResult>(string title, IEnumerable<TResult> options)
4137
{
4238
return AnsiConsole.Prompt(new SelectionPrompt<TResult>()
@@ -57,4 +53,10 @@ public static Table Table(params string[] columns)
5753

5854
return resultTable;
5955
}
56+
57+
public static void WriteError(string message) =>
58+
AnsiConsole.MarkupLine($"[red]Error: {Markup.Escape(message)}[/]");
59+
60+
public static void WriteSuccess(string message) =>
61+
AnsiConsole.MarkupLine($"[green]{Markup.Escape(message)}[/]");
6062
}

src/vv/CLI/UserException.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace vv.CLI;
2+
3+
internal sealed class UserException(string message) : Exception(message) {}

src/vv/Core/LanguagesYml.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
using vv.Utils;
1+
using vv.CLI;
2+
using vv.Utils;
23
using YamlDotNet.RepresentationModel;
34

45
namespace vv.Core;
56

67
internal static class LanguagesYml
78
{
8-
public static string LangsFilePath =>
9-
Path.Combine(Path.GetDirectoryName(Environment.ProcessPath), "languages.yml");
9+
private const string DOWNLOAD_URL =
10+
"https://raw.githubusercontent.com/github-linguist/linguist/refs/heads/main/lib/linguist/languages.yml";
1011
private static Color DefaultColorIfNotFound => Color.FromHex("#383c42");
12+
1113
private static readonly HashSet<string> IgnoredExtraLangs = new()
1214
{
13-
"Markdown", "MSBuild", "YAML", "Visual Studio Solution",
15+
"Markdown", "MSBuild", "YAML", "Visual Studio Solution",
1416
"Plain Text", "TOML", "XAML", "JSON", "XML", "SVG"
1517
};
1618

19+
public static string LangsFilePath =>
20+
Path.Combine(Path.GetDirectoryName(Environment.ProcessPath), "languages.yml");
21+
1722
public static Dictionary<LangData, string> GetLangsColors(List<LangData> langsDatas, bool ignoreExtra)
1823
{
1924
var yamlString = File.ReadAllText(LangsFilePath);
@@ -50,7 +55,7 @@ public static async Task UpdateLanguagesYmlFile()
5055
{
5156
if (File.Exists(LangsFilePath)) File.Delete(LangsFilePath);
5257

53-
var newFileBytes = await HttpUtils.DownloadFileBytes("https://raw.githubusercontent.com/github-linguist/linguist/refs/heads/main/lib/linguist/languages.yml");
58+
var newFileBytes = await HttpUtils.DownloadFileBytes(DOWNLOAD_URL);
5459

5560
File.WriteAllBytes(LangsFilePath, newFileBytes);
5661
}

0 commit comments

Comments
 (0)