Skip to content

Commit

Permalink
improved page path handling
Browse files Browse the repository at this point in the history
  • Loading branch information
principis committed Jan 5, 2019
1 parent 4027bc3 commit 3d6ad71
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A C# based command-line client for [tldr](https://github.com/tldr-pages/tldr).
![tldr screenshot](screenshot.png)

## Requirements
Because of TLS 1.2 support Mono >= 4.8 is required. If your distro comes with an older version, please install the [latest stable](https://www.mono-project.com/download/stable/).
Because of TLS 1.2 support, Mono >= 4.8, built with TLS 1.2 support, is required. If your distro comes with an older version, please install the [latest stable](https://www.mono-project.com/download/stable/).
* Mono >= 4.8 because of TLS 1.2 support

## Installing
Expand Down
43 changes: 30 additions & 13 deletions tldr-sharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,17 @@ private static int GetPage(string page, string language = null, string os = null
{
page = page.TrimStart();
CheckDb();
language = language ?? GetLanguage();
os = os ?? GetOs();

SqliteConnection conn = new SqliteConnection("Data Source=" + DbPath + ";");
conn.Open();
SqliteCommand command = new SqliteCommand(
"SELECT path FROM pages WHERE name = @name AND lang = @lang AND (os = @os OR os = 'common')",
"SELECT os FROM pages WHERE name = @name AND lang = @lang AND (os = @os OR os = 'common') ORDER BY os DESC LIMIT 1",
conn);
command.Parameters.Add(new SqliteParameter("@name", page));
command.Parameters.Add(new SqliteParameter("@os", os ?? GetOs()));
command.Parameters.Add(new SqliteParameter("@lang", language ?? GetLanguage()));
command.Parameters.Add(new SqliteParameter("@os", os));
command.Parameters.Add(new SqliteParameter("@lang", language));

var reader = command.ExecuteReader();

Expand All @@ -223,10 +225,19 @@ private static int GetPage(string page, string language = null, string os = null
return 2;
}
}

reader.Read();
string path = reader.GetString(0);
os = reader.GetString(0);

string path = Path.Combine(Path.GetDirectoryName(DbPath),
"pages" + (language != "en" ? $".{language}" : string.Empty), os, $"{page}.md");

if (!File.Exists(path))
{
Console.WriteLine("[ERROR] File \"{0}\" not found.", path);
return 1;
}

if (markdown)
Console.WriteLine(File.ReadAllText(path));
else
Expand Down Expand Up @@ -273,7 +284,6 @@ private static int Render(string path)
break;
}
}

return 0;
}

Expand Down Expand Up @@ -311,7 +321,6 @@ private static void ListAll(bool ignorePlatform, bool singleColumn, string langu
: string.Join(", ", results.OrderBy(x => x, StringComparer.Ordinal.WithNaturalSort())));
}


private static SortedSet<string> ListLanguages()
{
CheckDb();
Expand Down Expand Up @@ -385,29 +394,37 @@ private static void UpdateIndex()
SqliteConnection conn = new SqliteConnection("Data Source=" + DbPath + ";");
conn.Open();
SqliteCommand command = new SqliteCommand(
"CREATE TABLE pages (name VARCHAR(100), path TEXT, os VARCHAR(10), lang VARCHAR(2))",
"CREATE TABLE pages (name VARCHAR(100), os VARCHAR(10), lang VARCHAR(2))",
conn);

command.ExecuteNonQuery();
command.Dispose();

SqliteTransaction transaction = conn.BeginTransaction();
command = conn.CreateCommand();
command.Transaction = transaction;
command.CommandType = CommandType.Text;
command.CommandText = "INSERT INTO pages (name, path, os, lang) VALUES(@name, @path, @os, @lang)";

// Create indexes
command.CommandText = "CREATE INDEX os_names ON pages (os, name)";
command.ExecuteNonQuery();
command.CommandText = "CREATE INDEX lang_names ON pages (lang, name)";
command.ExecuteNonQuery();
command.CommandText = "CREATE INDEX names_index ON pages (lang, os, name)";
command.ExecuteNonQuery();

// Add pages
command.CommandText = "INSERT INTO pages (name, os, lang) VALUES(@name, @os, @lang)";

foreach (var dir in cacheDir.EnumerateDirectories("*pages*"))
{
string lang = "en";
if (dir.Name.Contains(".")) lang = dir.Name.Split('.')[1];
foreach (var osDir in dir.EnumerateDirectories())
{
string os = osDir.Name;
foreach (var file in osDir.EnumerateFiles("*.md", SearchOption.AllDirectories))
{
command.Parameters.AddWithValue("@name", Path.GetFileNameWithoutExtension(file.Name));
command.Parameters.AddWithValue("@path", file.FullName);
command.Parameters.AddWithValue("@os", os);
command.Parameters.AddWithValue("@os", osDir.Name);
command.Parameters.AddWithValue("@lang", lang);
command.ExecuteNonQuery();
}
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("1.4.0")]
[assembly: AssemblyFileVersion("1.4.0")]
[assembly: AssemblyVersion("1.4.1")]
[assembly: AssemblyFileVersion("1.4.1")]

0 comments on commit 3d6ad71

Please sign in to comment.