Skip to content

Commit

Permalink
First release v0.1.150
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianMorozov committed Dec 10, 2022
1 parent aebd7da commit 2953849
Show file tree
Hide file tree
Showing 14 changed files with 414 additions and 67 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.1.130] - 2022-12-10
## [0.1.150] - 2022-12-10
### Added
- MenuHelper into TgDownloaderConsole
- LogHelper into TgDownloaderCore
- TgClientHelper into TgDownloaderCore
- TgSettingsHelper into TgDownloaderCore
- FileUtils into TgDownloaderCore
- First release

## [0.1.100] - 2022-12-08
### Added
Expand Down
12 changes: 12 additions & 0 deletions TgDownloaderConsole/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com

global using System.Diagnostics;
global using System.Reflection;
global using System.Text;
global using Spectre.Console;
global using TgDownloaderConsole.Helpers;
global using TgDownloaderCore.Enums;
global using TgDownloaderCore.Helpers;
global using TgDownloaderCore.Locales;
global using TL;
200 changes: 200 additions & 0 deletions TgDownloaderConsole/Helpers/MenuHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com

namespace TgDownloaderConsole.Helpers;

internal class MenuHelper
{
#region Design pattern "Lazy Singleton"

private static MenuHelper _instance;
public static MenuHelper Instance => LazyInitializer.EnsureInitialized(ref _instance);

#endregion

#region Public and internal fields, properties, constructor

internal static LocaleHelper Locale => LocaleHelper.Instance;
internal static TgClientHelper TgClient => TgClientHelper.Instance;
internal static Style StyleMain => new(Color.White, null, Decoration.Bold | Decoration.Conceal | Decoration.Italic);

internal long SourceFileSize { get; set; }
internal long SourceFileRead { get; set; }
internal double SourceFileProgress
{
get
{
if (SourceFileSize == 0)
return 0;
return (double)(SourceFileRead * 100) / SourceFileSize;
}
}
internal TgMenu MenuItem { get; set; } = TgMenu.Exit;

#endregion

#region Public and internal methods

internal static string GetAppVersion()
{
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion;
return version ?? string.Empty;
}

internal void ShowTable(string title)
{
AnsiConsole.Clear();
AnsiConsole.Write(new FigletText(Locale.Info.AppTitle).Alignment(Justify.Center).Color(Color.Yellow));
Table table = new()
{
ShowHeaders = true,
Border = TableBorder.Rounded,
Title = new(title, Style.Plain),
};

FillTableColumns(table);
FillTableRows(table);

table.Expand();
AnsiConsole.Write(table);
}

internal void FillTableColumns(Table table)
{
if (table.Columns.Count > 0) return;

table.AddColumn(new TableColumn(
new Markup(Locale.Info.AppName, StyleMain)) { Width = 20 }.LeftAligned());
table.AddColumn(new TableColumn(
new Markup(Locale.Info.AppValue, StyleMain)) { Width = 80 }.LeftAligned());
}

internal void FillTableRows(Table table)
{
if (table.Rows.Count > 0) table.Rows.Clear();

table.AddRow(new Markup(Locale.InfoMessage(Locale.Info.AppVersion)), new Markup($@"v{GetAppVersion()}"));

// TG client info.
if (!TgClient.IsConnected)
{
table.AddRow(new Markup(Locale.WarningMessage(Locale.Info.TgClientUserName)),
new Markup(Locale.Warning.TgSettingsNotSet));
table.AddRow(new Markup(Locale.WarningMessage(Locale.Info.TgClientUserId)),
new Markup(Locale.Warning.TgSettingsNotSet));
table.AddRow(new Markup(Locale.WarningMessage(Locale.Info.TgClientUserIsActive)),
new Markup(Locale.Warning.TgSettingsNotSet));
table.AddRow(new Markup(Locale.WarningMessage(Locale.Info.TgClientUserStatus)),
new Markup(Locale.Warning.TgSettingsNotSet));
}
else
{
User user = TgClient.MySelfUser;
table.AddRow(new Markup(Locale.InfoMessage(Locale.Info.TgClientUserName)),
new Markup(user.username));
table.AddRow(new Markup(Locale.InfoMessage(Locale.Info.TgClientUserId)),
new Markup(user.id.ToString()));
table.AddRow(new Markup(Locale.InfoMessage(Locale.Info.TgClientUserIsActive)),
new Markup(user.IsActive.ToString()));
table.AddRow(new Markup(Locale.InfoMessage(Locale.Info.TgClientUserStatus)),
new Markup(user.status.ToString() ?? ""));
}

// TG source user name.
if (string.IsNullOrEmpty(TgClient.TgSettings.SourceUserName))
table.AddRow(new Markup(Locale.WarningMessage(Locale.Info.TgSettingsSourceUserName)),
new Markup(Locale.Warning.TgSettingsNotSet));
else
table.AddRow(new Markup(Locale.InfoMessage(Locale.Info.TgSettingsSourceUserName)),
new Markup(TgClient.TgSettings.SourceUserName));

// Dest dir.
if (string.IsNullOrEmpty(TgClient.TgSettings.DestDirectory))
table.AddRow(new Markup(Locale.WarningMessage(Locale.Info.TgSettingsDestDirectory)),
new Markup(Locale.Warning.TgSettingsNotSet));
else
{
if (!Directory.Exists(TgClient.TgSettings.DestDirectory))
table.AddRow(new Markup(Locale.WarningMessage(Locale.Info.TgSettingsDestDirectory)),
new Markup(Locale.Warning.DirIsNotExists));
else
table.AddRow(new Markup(Locale.InfoMessage(Locale.Info.TgSettingsDestDirectory)),
new Markup(TgClient.TgSettings.DestDirectory));
}

// TG start ID.
if (TgClient.TgSettings.MessageStartId < 0)
table.AddRow(new Markup(Locale.WarningMessage(Locale.Info.TgSettingsMessageStartId)),
new Markup(Locale.Warning.TgSettingsNotSet));
else
table.AddRow(new Markup(Locale.InfoMessage(Locale.Info.TgSettingsMessageStartId)),
new Markup(TgClient.TgSettings.MessageStartId.ToString()));

// TG messages count.
if (TgClient.TgSettings.MessageCount < 0)
table.AddRow(new Markup(Locale.WarningMessage(Locale.Info.TgSettingsMessageCount)),
new Markup(Locale.Warning.TgSettingsNotSet));
else
table.AddRow(new Markup(Locale.InfoMessage(Locale.Info.TgSettingsMessageCount)),
new Markup(TgClient.TgSettings.MessageCount.ToString()));

// TG messages max count.
if (TgClient.TgSettings.MessageMaxCount < 0)
table.AddRow(new Markup(Locale.WarningMessage(Locale.Info.TgSettingsMessageMaxCount)),
new Markup(Locale.Warning.TgSettingsNotSet));
else
table.AddRow(new Markup(Locale.InfoMessage(Locale.Info.TgSettingsMessageMaxCount)),
new Markup(TgClient.TgSettings.MessageMaxCount.ToString()));
}

internal string GetTableRowsSize(long value)
{
if (value > 0)
{
if (value < 1024)
return $"{value:##0.000} B";
if (value < 1024 * 1024)
return $"{(double)value / 1024:##0.000} KB";
return value < 1024 * 1024 * 1024
? $"{(double)value / 1024L / 1024:##0.000} MB"
: $"{(double)value / 1024L / 1024L / 1024:##0.000} GB";
}
return "0 B";
}

internal void SetOptionsSetupTg()
{
string subMenu = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title(Locale.Info.MenuSwitchNumber)
.PageSize(6)
.MoreChoicesText(Locale.Info.MoveUpDown)
.AddChoices(
"Return",
"Setup user name",
"Setup download folder",
"Setup message start ID",
"Setup messages count"));
MenuItem = subMenu switch
{
"Return" => TgMenu.Return,
"Setup user name" => TgMenu.SetTgSourceUsername,
"Setup download folder" => TgMenu.SetTgDestDirectory,
"Setup message start ID" => TgMenu.SetTgMessageStartId,
"Setup messages count" => TgMenu.SetTgMessageCount,
_ => MenuItem
};
}

internal string GetStatusInfo(Stopwatch sw) =>
$"{sw.Elapsed} | {SourceFileProgress:##0.000} % | {GetTableRowsSize(SourceFileRead)} |";

public bool CheckTgSettings() =>
TgClient.IsConnected &&
!string.IsNullOrEmpty(TgClient.TgSettings.SourceUserName) &&
!string.IsNullOrEmpty(TgClient.TgSettings.DestDirectory);

#endregion
}
119 changes: 118 additions & 1 deletion TgDownloaderConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,121 @@
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
// See https://aka.ms/new-console-template for more information

Console.WriteLine("TgDownloaderConsole project");
LocaleHelper locale = LocaleHelper.Instance;
LogHelper log = LogHelper.Instance;
MenuHelper menu = MenuHelper.Instance;
TgClientHelper tgClient = TgClientHelper.Instance;

SetLog();

do
{
try
{
menu.ShowTable("");
menu.MenuItem = TgMenu.Return;
string mainMenu = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title(locale.Info.MenuSwitchNumber)
.PageSize(6)
.MoreChoicesText(locale.Info.MoveUpDown)
.AddChoices(locale.Info.MenuMain));
switch (mainMenu)
{
case "Exit":
menu.MenuItem = TgMenu.Exit;
break;
case "Connect":
ConnectTgClient();
break;
case "Settings":
SetupTgSettings();
break;
case "Download files":
menu.MenuItem = TgMenu.DownloadFiles;
RunMenuJob(DownloadFiles);
break;
}
}
catch (Exception ex)
{
log.MarkupLineStamp(locale.Info.StatusException + log.GetMarkupString(ex.Message));
if (ex.InnerException is not null)
log.MarkupLineStamp(locale.Info.StatusInnerException + log.GetMarkupString(ex.InnerException.Message));
menu.MenuItem = TgMenu.Exit;
}
} while (menu.MenuItem is not TgMenu.Exit);

void SetLog()
{
Console.OutputEncoding = Encoding.UTF8;
log.SetMarkupLineStamp(AnsiConsole.MarkupLine);
log.SetAskString(AnsiConsole.Ask<string>);
log.SetAskInt(AnsiConsole.Ask<int>);
log.SetAskBool(AnsiConsole.Ask<bool>);
}

void SetupTgSettings()
{
menu.ShowTable(locale.Info.MenuSetupTg);
menu.SetOptionsSetupTg();

switch (menu.MenuItem)
{
case TgMenu.SetTgSourceUsername:
tgClient.TgSettings.SetSourceUserName();
break;
case TgMenu.SetTgDestDirectory:
tgClient.TgSettings.SetDestDirectory();
break;
case TgMenu.SetTgMessageStartId:
tgClient.TgSettings.SetMessageStartId();
break;
case TgMenu.SetTgMessageCount:
tgClient.TgSettings.SetMessageCount();
break;
}
}

void RunMenuJob(Action action)
{
menu.ShowTable(locale.Info.MenuDownloadFiles);
log.MarkupLineStamp(locale.Info.StatusStart);

AnsiConsole.Status()
.AutoRefresh(true)
.Spinner(Spinner.Known.Aesthetic)
.SpinnerStyle(Style.Parse("green"))
.Start("Thinking...", statusContext =>
{
Stopwatch sw = new();
sw.Start();
statusContext.Status($"{menu.GetStatusInfo(sw)} | Process job.");
statusContext.Refresh();
action();
sw.Stop();
statusContext.Status($"{menu.GetStatusInfo(sw)} | Job is complete.");
statusContext.Refresh();
});
log.MarkupLineStamp(locale.Info.MenuReturn);
Console.ReadKey();
}

void ConnectTgClient()
{
tgClient.Connect();
tgClient.CollectAllChats().GetAwaiter().GetResult();
tgClient.PrintChatsInfo(tgClient.ListSmallGroups, "small groups", true);
tgClient.PrintChannelsInfo(tgClient.ListGroups, "groups", true);
tgClient.PrintChannelsInfo(tgClient.ListChannels, "channels", true);
log.MarkupLineStamp(locale.Info.AnyKey);
Console.ReadKey();
}

void DownloadFiles()
{
if (!menu.CheckTgSettings()) return;
Channel channel = tgClient.PrepareCollectMessages();
menu.ShowTable(locale.Info.MenuDownloadFiles);
tgClient.CollectMessages(channel).GetAwaiter().GetResult();
}
5 changes: 4 additions & 1 deletion TgDownloaderConsole/TgDownloaderConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<LangVersion>11.0</LangVersion>
<Version>0.1.100.0</Version>
<Version>0.1.150.0</Version>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Locale\**" />
Expand All @@ -19,4 +19,7 @@
<ItemGroup>
<ProjectReference Include="..\TgDownloaderCore\TgDownloaderCore.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Helpers\" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion TgDownloaderConsoleTest/TgDownloaderConsoleTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<IsPackable>false</IsPackable>
<Version>0.1.100.0</Version>
<Version>0.1.150.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
Expand Down
3 changes: 0 additions & 3 deletions TgDownloaderCore/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@

global using System.Diagnostics;
global using System.IO.Enumeration;
global using System.Reflection;
global using System.Text;
global using System.Text.RegularExpressions;
global using TL;
global using WTelegram;
Loading

0 comments on commit 2953849

Please sign in to comment.