diff --git a/README.md b/README.md index 8b39318..e63fced 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/tldr-sharp/Program.cs b/tldr-sharp/Program.cs index 658ead1..4ea7d94 100644 --- a/tldr-sharp/Program.cs +++ b/tldr-sharp/Program.cs @@ -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(); @@ -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 @@ -273,7 +284,6 @@ private static int Render(string path) break; } } - return 0; } @@ -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 ListLanguages() { CheckDb(); @@ -385,16 +394,26 @@ 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*")) { @@ -402,12 +421,10 @@ private static void UpdateIndex() 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(); } diff --git a/tldr-sharp/Properties/AssemblyInfo.cs b/tldr-sharp/Properties/AssemblyInfo.cs index dfc7baa..5e98721 100644 --- a/tldr-sharp/Properties/AssemblyInfo.cs +++ b/tldr-sharp/Properties/AssemblyInfo.cs @@ -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")] \ No newline at end of file +[assembly: AssemblyVersion("1.4.1")] +[assembly: AssemblyFileVersion("1.4.1")] \ No newline at end of file