Skip to content

Commit

Permalink
Improved windows compatibility
Browse files Browse the repository at this point in the history
- Added ansi support for windows
- Refactored ansi colors
- Added sqlite3.dll to deploy
  • Loading branch information
principis committed Nov 23, 2019
1 parent 5fff206 commit 0d81e7b
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 52 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ sudo chmod +x tldr
```

### Windows
Extract the latest [release](https://github.com/principis/tldr-sharp/releases) and download the latest [sqlite3 dll](https://www.sqlite.org/download.html).
Extract the dll in the same folder as tldr_sharp.exe.
Extract the latest [release](https://github.com/principis/tldr-sharp/releases) to a folder of choice, add it to the Path and reboot.

## Usage
```
Expand Down Expand Up @@ -65,4 +64,4 @@ Simplified and community-driven man pages
This project is licensed under the GPL license - see the [LICENSE](LICENSE) file for details.

## Contributing
Contributions are always welcome! Please open an issue first.
Contributions are always welcome! Please open an issue first.
72 changes: 37 additions & 35 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

set -e

if ([ ! -z "$TRAVIS_TAG" ]) &&
[ "$TRAVIS_PULL_REQUEST" == "false" ]; then

create_archives() {

SCRIPT=$(realpath $0)
SCRIPTPATH=$(dirname $SCRIPT)

if ([ ! -z "$TRAVIS_TAG" ]) &&
[ "$TRAVIS_PULL_REQUEST" == "false" ]; then

create_archives() {

cd $TARGET

# Windows archives
cp "${SCRIPTPATH}/windows/sqlite3${PLATFORM}.dll" sqlite3.dll
zip -r "../tldr-sharp_${TRAVIS_TAG#v}_windows${PLATFORM}.zip" *

# Linux archives
Expand All @@ -19,43 +23,41 @@ if ([ ! -z "$TRAVIS_TAG" ]) &&
cd ..

# Linux install scripts
sed -e "s/VERSION_PLACEHOLDER/$TRAVIS_TAG/" -e "s/FILE_PLACEHOLDER/tldr-sharp_${TRAVIS_TAG#v}_linux${PLATFORM}/" ../../scripts/linux_install.sh > tldr-sharp_${TRAVIS_TAG#v}_linux${PLATFORM}.sh
}
sed -e "s/VERSION_PLACEHOLDER/$TRAVIS_TAG/" -e "s/FILE_PLACEHOLDER/tldr-sharp_${TRAVIS_TAG#v}_linux${PLATFORM}/" ${SCRIPTPATH}/linux_install.sh >tldr-sharp_${TRAVIS_TAG#v}_linux${PLATFORM}.sh
}

build_deb() {
local tempdir
build_deb() {
local tempdir

tempdir=$(mktemp -d 2>/dev/null || mktemp -d -t tmp)
tempdir=$(mktemp -d 2>/dev/null || mktemp -d -t tmp)

mkdir -p "$tempdir/usr/lib/tldr-sharp"
cp $TARGET/* "$tempdir/usr/lib/tldr-sharp"
chmod 755 "$tempdir/usr/lib/tldr-sharp/"*
mkdir -p "$tempdir/usr/lib/tldr-sharp"
cp $TARGET/* "$tempdir/usr/lib/tldr-sharp"
chmod 755 "$tempdir/usr/lib/tldr-sharp/"*

mkdir -p "$tempdir/usr/bin"
install -Dm755 "../../scripts/debian/tldr-sharp" "$tempdir/usr/bin/tldr-sharp"
mkdir -p "$tempdir/usr/bin"
install -Dm755 "${SCRIPTPATH}/debian/tldr-sharp" "$tempdir/usr/bin/tldr-sharp"

mkdir "$tempdir/DEBIAN"
install -Dm644 "../../scripts/debian/control" "$tempdir/DEBIAN/control"
install -Dm755 "../../scripts/debian/postinst" "$tempdir/DEBIAN/postinst"
install -Dm755 "../../scripts/debian/prerm" "$tempdir/DEBIAN/prerm"
mkdir "$tempdir/DEBIAN"
install -Dm644 "${SCRIPTPATH}/debian/control" "$tempdir/DEBIAN/control"
install -Dm755 "${SCRIPTPATH}/debian/postinst" "$tempdir/DEBIAN/postinst"
install -Dm755 "${SCRIPTPATH}/debian/prerm" "$tempdir/DEBIAN/prerm"

sed -i "s/VERSION_PLACEHOLDER/${TRAVIS_TAG#v}/g" "$tempdir/DEBIAN/control"
sed -i "s/VERSION_PLACEHOLDER/${TRAVIS_TAG#v}/g" "$tempdir/DEBIAN/control"

fakeroot dpkg-deb --build "$tempdir" "tldr-sharp_${TRAVIS_TAG#v}${PLATFORM}.deb"
}
fakeroot dpkg-deb --build "$tempdir" "tldr-sharp_${TRAVIS_TAG#v}${PLATFORM}.deb"
}


cd tldr-sharp/bin

TARGET="Release"
PLATFORM="_x64"
create_archives
build_deb
cd tldr-sharp/bin

TARGET="Release32"
PLATFORM=""
create_archives
build_deb
TARGET="Release"
PLATFORM="_x64"
create_archives
build_deb

fi
TARGET="Release32"
PLATFORM=""
create_archives
build_deb

fi
Binary file added scripts/windows/sqlite3.dll
Binary file not shown.
Binary file added scripts/windows/sqlite3_x64.dll
Binary file not shown.
14 changes: 14 additions & 0 deletions tldr-sharp/Ansi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace tldr_sharp
{
internal struct Ansi
{
internal const string Default = "\x1b[39m";
internal const string Off = "\x1b[0m";
internal const string Bold = "\x1b[1m";
internal const string BoldOff = "\x1b[21m";
internal const string Green = "\x1b[32m";
internal const string Magenta = "\x1b[39m";
internal const string Red = "\x1b[31m";
internal const string Underline = "\x1b[4m";
}
}
34 changes: 23 additions & 11 deletions tldr-sharp/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,15 @@ internal static int Render(string path, string diffPlatform = null)
}

if (diffPlatform != null) {
Console.WriteLine("\x1B[31m\x1b[1m[WARNING] THIS PAGE IS FOR THE " + diffPlatform.ToUpper() +
" PLATFORM!\x1b[0m\n");
if (Program.AnsiSupport) {
Console.WriteLine(Ansi.Red + Ansi.Bold + "[WARNING] THIS PAGE IS FOR THE " + diffPlatform.ToUpper() +
" PLATFORM!" + Ansi.Off + "\n");
} else {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[WARNING] THIS PAGE IS FOR THE " + diffPlatform.ToUpper() +
" PLATFORM!\n");
Console.ForegroundColor = Program.DefaultColor;
}
}

foreach (string line in File.ReadLines(path)) {
Expand All @@ -135,31 +142,31 @@ internal static int Render(string path, string diffPlatform = null)
internal static string ParseLine(string line, bool formatted = false)
{
if (line.Contains("{{")) {
line = line.Replace("{{", "\x1b[32m").Replace("}}", "\x1b[31m");
line = line.Replace("{{", Ansi.Green).Replace("}}", Ansi.Red);
}

int urlStart = line.IndexOf("<", StringComparison.Ordinal);
if (urlStart != -1) {
int urlEnd = line.Substring(urlStart).IndexOf(">", StringComparison.Ordinal);
if (urlEnd != -1) {
line = line.Substring(0, urlStart) + "\x1b[21m\x1b[4m" +
line.Substring(urlStart + 1, urlEnd - 1) + "\x1b[0m" +
line = line.Substring(0, urlStart) + Ansi.BoldOff + Ansi.Underline +
line.Substring(urlStart + 1, urlEnd - 1) + Ansi.Off +
line.Substring(urlStart + urlEnd + 1);
}
}

switch (line[0]) {
case '#':
line = "\x1B[4m\x1b[1m" + line.Substring(2) + "\x1b[0m" + (formatted ? "\n" : "");
line = (Program.AnsiSupport ? Ansi.Underline + Ansi.Bold : "") + line.Substring(2) + (Program.AnsiSupport ? Ansi.Off : "") + (formatted ? "\n" : "");
break;
case '>':
line = "\x1b[1m" + line.Substring(2) + "\x1b[0m";
line = (Program.AnsiSupport ? Ansi.Bold : "") + line.Substring(2) + (Program.AnsiSupport ? Ansi.Off : "");
break;
case '-':
line = "\x1b[39m" + (formatted ? "\n" : "") + line + "\x1b[0m";
line = (Program.AnsiSupport ? Ansi.Default : "") + (formatted ? "\n" : "") + line + (Program.AnsiSupport ? Ansi.Off : "");
break;
case '`':
line = (formatted ? " " : "") + "\x1b[31m" + line.Trim('`') + "\x1b[0m";
line = (formatted ? " " : "") + (Program.AnsiSupport ? Ansi.Red : "") + line.Trim('`') + (Program.AnsiSupport ? Ansi.Off : "");
break;
}

Expand Down Expand Up @@ -253,8 +260,13 @@ internal static int Search(string searchString, string language, string platform

foreach ((string page, var matches) in results) {
foreach (string line in matches) {
Console.WriteLine("\x1b[35m{0}\x1b[39m:\t{1}", page,
ParseLine(line).Replace(searchString, "\x1b[4m" + searchString + "\x1b[24m"));

if (Program.AnsiSupport) {
Console.WriteLine("{0}{1}{2}:\t{3}", Ansi.Magenta, page, Ansi.Default,
ParseLine(line).Replace(searchString, "\x1b[4m" + searchString + "\x1b[24m"));
} else {
Console.WriteLine("{0}:\t{1}", page, ParseLine(line));
}
}
}

Expand Down
34 changes: 33 additions & 1 deletion tldr-sharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,32 @@
using Mono.Options;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using NaturalSort.Extension;
using static tldr_sharp.Index;

namespace tldr_sharp
{
internal static class Program
{
private const int STD_OUTPUT_HANDLE = -11;
private const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
private const uint DISABLE_NEWLINE_AUTO_RETURN = 0x0008;

[DllImport("kernel32.dll")]
private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);

[DllImport("kernel32.dll")]
private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetStdHandle(int nStdHandle);


private const string ClientSpecVersion = "1.2";
internal const string DefaultLanguage = "en_US";
internal static bool AnsiSupport = true;
internal static readonly ConsoleColor DefaultColor = Console.ForegroundColor;

internal static readonly string Language = CultureInfo.CurrentCulture.Name.Replace('-', '_');

Expand All @@ -28,7 +45,22 @@ internal static class Program

public static int Main(string[] args)
{

if (Environment.OSVersion.Platform == PlatformID.Win32NT) {

IntPtr iStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

if (!GetConsoleMode(iStdOut, out uint outConsoleMode)) {
AnsiSupport = false;
} else {
outConsoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN;

if (!SetConsoleMode(iStdOut, outConsoleMode)) {
AnsiSupport = false;
}
}
}


bool showHelp = false;

bool list = false;
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.3.2")]
[assembly: AssemblyFileVersion("2.3.2")]
[assembly: AssemblyVersion("2.3.3")]
[assembly: AssemblyFileVersion("2.3.3")]
Binary file added tldr-sharp/sqlite3.dll
Binary file not shown.
1 change: 1 addition & 0 deletions tldr-sharp/tldr-sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Ansi.cs" />
<Compile Include="Cache.cs" />
<Compile Include="Index.cs" />
<Compile Include="Page.cs" />
Expand Down

0 comments on commit 0d81e7b

Please sign in to comment.