Skip to content

Commit

Permalink
Refactored GetPage to be more maintainable.
Browse files Browse the repository at this point in the history
  • Loading branch information
principis committed Jun 23, 2019
1 parent 75ac11a commit ec15cdc
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 54 deletions.
114 changes: 62 additions & 52 deletions tldr-sharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using NaturalSort.Extension;
using SharpCompress.Common;
using SharpCompress.Readers;
using Version = System.Version;

namespace tldr_sharp
{
Expand Down Expand Up @@ -234,66 +235,40 @@ private static int GetPage(string page, string language = null, string platform
language = language ?? GetLanguage();
var preferredLanguages = new List<string> {language, Language};

var langs = Environment.GetEnvironmentVariable("LANGUAGE")?.Split(':').Where(x => !x.Equals(string.Empty)).ToList();
if (langs != null)
{
preferredLanguages.AddRange(langs);
}
var langs = Environment.GetEnvironmentVariable("LANGUAGE")
?.Split(':')
.Where(x => !x.Equals(string.Empty))
.ToList();

if (langs != null) preferredLanguages.AddRange(langs);

platform = platform ?? GetPlatform();
string altPlatform = null;

var results = QueryPage(page);
if (!results.Contains((platform, language)))

if (results.Count == 0)
{
if (results.Count == 0)
{
Console.Write("Page not found. ");
Update();
results = QueryPage(page);
if (results.Count == 0)
{
Console.WriteLine("Page not found.\nFeel free to create an issue at: https://github.com/tldr-pages/tldr/issues/new?title=page%20request:%20{0}", page);
return 2;
}
}
Console.Write("Page not found. ");
Update();
results = QueryPage(page);

if (results.Contains(("common", language)))
{
platform = "common";
}
if (results.Count == 0) return PageNotFound(page);
}

if (!results.Contains((platform, language)))
{
if (results.Contains(("common", language))) platform = "common";
else
{
bool found = false;
var altLanguage = string.Empty;

foreach (var (item1, item2) in results)
{
if (!preferredLanguages.Any(preferredLanguage => item2.Contains(preferredLanguage))) continue;
if (item1 != platform && item1 != "common")
{
if (altPlatform == null)
{
altPlatform = item1;
altLanguage = item2;
}
continue;
}
platform = item1;
language = item2;
found = true;
}

if (!found)
{
if (altPlatform == string.Empty)
{
Console.WriteLine("Page not found.\nFeel free to create an issue at: https://github.com/tldr-pages/tldr/issues/new?title=page%20request:%20{0}", page);
return 2;
}
platform = altPlatform;
language = altLanguage;
}
string tmpPlatform;
(tmpPlatform, language) = FindAlternativePage(results, preferredLanguages, platform);

if (tmpPlatform == null || language == null) return PageNotFound(page);

altPlatform = tmpPlatform;
if (platform == tmpPlatform || tmpPlatform == "common") altPlatform = null;
platform = tmpPlatform;
}
}

Expand All @@ -316,6 +291,41 @@ private static int GetPage(string page, string language = null, string platform
return 0;
}

private static int PageNotFound(string page)
{
Console.WriteLine("Page not found.\nFeel free to create an issue at: https://github.com/tldr-pages/tldr/issues/new?title=page%20request:%20{0}", page);
return 2;
}

private static (string Platform, string Language) FindAlternativePage(IEnumerable<(string, string)> results,
IReadOnlyCollection<string> preferredLanguages, string platform)
{
bool found = false;
var altLanguage = string.Empty;
string altPlatform = null;
string language = null;

foreach (var (item1, item2) in results)
{
if (!preferredLanguages.Any(preferredLanguage => item2.Contains(preferredLanguage))) continue;
if (item1 != platform && item1 != "common")
{
if (altPlatform == null)
{
altPlatform = item1;
altLanguage = item2;
}
continue;
}
platform = item1;
language = item2;
found = true;
}

if (found) return (platform, language);
return altPlatform == string.Empty ? (null, null) : (altPlatform, altLanguage);
}

private static int Render(string path, string diffPlatform = null)
{
if (!File.Exists(path))
Expand Down
4 changes: 2 additions & 2 deletions tldr-sharp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.1.1")]
[assembly: AssemblyFileVersion("2.1.1")]
[assembly: AssemblyVersion("2.1.2")]
[assembly: AssemblyFileVersion("2.1.2")]

0 comments on commit ec15cdc

Please sign in to comment.