Skip to content

feat (3.4.0): add platform option, Android and BSD platforms #421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ npm install -g tldr
To see tldr pages:

- `tldr <command>` show examples for this command
- `tldr <command> --os=<platform>` show command page for the given platform (`linux`, `osx`, `sunos`, `windows`)
- `tldr <command> --platform=<android|linux|osx|sunos|windows>` show command page for the given platform
- `tldr --search "<query>"` search all pages for the query
- `tldr --android <command>` show command page for Android
- `tldr --linux <command>` show command page for Linux
- `tldr --osx <command>` show command page for OSX
- `tldr --sunos <command>` show command page for SunOS
Expand Down Expand Up @@ -91,7 +92,7 @@ you can put it in the config file:
The default platform value can be overwritten with command-line option:

```shell
tldr du --os=osx
tldr du --platform=<osx>
```

As a contributor, you can also point to your own fork containing the `tldr.zip` file. The file is just a zipped version of the entire tldr repo:
Expand Down
9 changes: 5 additions & 4 deletions bin/completion/bash/tldr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

BUILTIN_THEMES="single base16 ocean"

OS_TYPES="linux osx sunos windows"
PLATFORM_TYPES="android linux osx sunos windows"

OPTIONS='-v
--version
Expand All @@ -20,7 +20,8 @@ OPTIONS='-v
--render
-m
--markdown
-o
-p
--android
--linux
--osx
--sunos
Expand Down Expand Up @@ -56,8 +57,8 @@ function _tldr_autocomplete {
COMPREPLY=(`compgen -f $cur`)
;;

-o|--os)
COMPREPLY=(`compgen -W "$OS_TYPES" $cur`)
-p|--platform)
COMPREPLY=(`compgen -W "$PLATFORM_TYPES" $cur`)
;;

-t|--theme)
Expand Down
15 changes: 8 additions & 7 deletions bin/completion/zsh/_tldr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#compdef tldr

local -a pages oses
local -a pages platforms
pages=$(tldr -a1)
oses='( linux osx sunos windows )'
platforms='( android linux osx sunos windows )'

_arguments \
'(- *)'{-h,--help}'[show help]' \
Expand All @@ -15,11 +15,12 @@ _arguments \
'(- *)'{-e,--random-example}'[show a random example]' \
'(- *)'{-m,--markdown}'[show the original markdown format page]' \
'(-f --render)'{-f,--render}'[render a specific markdown file]:markdown file:_files -/' \
'(-o --os)'{-o,--os}"[override operating system]:os:${oses}" \
'--linux[override operating system with Linux]' \
'--osx[override operating system with OSX]' \
'--sunos[override operating system with SunOS]' \
'--windows[override operating system with Windows]' \
'(-p --platform)'{-p,--platform}"[override platform]:os:${oses}" \
'--android[override platform with Android]' \
'--linux[override platform with Linux]' \
'--osx[override platform with OSX]' \
'--sunos[override platform with SunOS]' \
'--windows[override platform with Windows]' \
'(- *)'{-u,--update}'[update local cache]' \
'(- *)'{-c,--clear-cache}'[clear local cache]' \
"*:page:(${(b)pages})" && return 0
35 changes: 21 additions & 14 deletions bin/tldr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const program = require('commander');
const pkg = require('../package');
const Tldr = require('../lib/tldr');
const config = require('../lib/config');
const platform = require('../lib/platform');
const platforms = require('../lib/platforms');
const { TldrError } = require('../lib/errors');

program
Expand All @@ -22,11 +22,14 @@ program
.option('-e, --random-example', 'Show a random example')
.option('-f, --render [file]', 'Render a specific markdown [file]')
.option('-m, --markdown', 'Output in markdown format')
.option('-o, --os [type]', 'Override the operating system [linux, osx, sunos, windows]')
.option('--linux', 'Override the operating system with Linux')
.option('--osx', 'Override the operating system with OSX')
.option('--sunos', 'Override the operating system with SunOS')
.option('--windows', 'Override the operating system with Windows')
.option('-p, --platform [type]', 'Override the current platform [android, linux, osx, sunos, windows]')
.option('--android', 'Override the platform with Android')
.option('--linux', 'Override the platform with Linux')
.option('--osx', 'Override the platform with OSX')
.option('--sunos', 'Override the platform with SunOS')
.option('--windows', 'Override the platform with Windows')
.alias('-o', '-p') // Alias -o to -p
.alias('--os', '--platform') // Alias --os to --platform
.option('-t, --theme [theme]', 'Color theme (simple, base16, ocean)')
.option('-s, --search [keywords]', 'Search pages using keywords')
//
Expand All @@ -39,7 +42,7 @@ const help = `
Examples:

$ tldr tar
$ tldr du --os=linux
$ tldr du --platform=linux
$ tldr --search "create symbolic link to file"
$ tldr --list
$ tldr --list-all
Expand All @@ -62,26 +65,30 @@ program.on('--help', () => {

program.parse(process.argv);

if(program.android) {
program.platform = 'android';
}

if (program.linux) {
program.os = 'linux';
program.platform = 'linux';
}

if (program.osx) {
program.os = 'osx';
program.platform = 'osx';
}

if (program.sunos) {
program.os = 'sunos';
program.platform = 'sunos';
}

if (program.windows) {
program.os = 'windows';
program.platform = 'windows';
}

let cfg = config.get();
if (program.os) {
if (platform.isSupported(program.os)) {
cfg.platform = program.os;
if (program.platform) {
if (platforms.isSupported(program.platform)) {
cfg.platform = program.platform;
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require('fs-extra');
const os = require('os');
const path = require('path');
const remote = require('./remote');
const platform = require('./platform');
const platforms = require('./platforms');
const index = require('./index');
const utils = require('./utils');

Expand All @@ -21,7 +21,7 @@ class Cache {
}

getPage(page) {
let preferredPlatform = platform.getPreferredPlatformFolder(this.config);
let preferredPlatform = platforms.getPreferredPlatformFolder(this.config);
const preferredLanguage = process.env.LANG || 'en';
return index.findPage(page, preferredPlatform, preferredLanguage)
.then((folder) => {
Expand Down
10 changes: 5 additions & 5 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const defaults = require('lodash/defaults');
const fs = require('fs');
const path = require('path');
const osHomedir = require('os').homedir;
const platforms = require('./platforms');

exports.get = () => {
const DEFAULT = path.join(__dirname, '..', 'config.json');
Expand Down Expand Up @@ -38,10 +39,9 @@ exports.get = () => {
return merged;
};

function validatePlatform(os) {
let platform = require('./platform');
if (os && !platform.isSupported(os)) {
return 'Unsupported platform : ' + os;
function validatePlatform(platform) {
if (platform && !platforms.isSupported(platform)) {
return 'Unsupported platform : ' + platform;
}
return null;
}
Expand Down Expand Up @@ -101,4 +101,4 @@ function validateThemeItem(field, key) {
return null;
}
return errMsg.join('\n');
}
}
20 changes: 10 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ function findPage(page, preferredPlatform, preferredLanguage) {
targetPlatform = 'common';
} else if (targets.length > 0 && hasLang(targets, preferredLanguage)) {
targetLanguage = preferredLanguage;
targetPlatform = targets[0].os;
console.log(`Command ${page} does not exist for the host platform. Displaying the page from ${targets[0].os} platform`);
targetPlatform = targets[0].platform;
console.log(`Command ${page} does not exist for the host platform. Displaying the page from ${targetPlatform} platform`);
} else if (targets.length > 0 && hasLang(targets, 'en')) {
targetLanguage = 'en';
targetPlatform = targets[0].os;
console.log(`Command ${page} does not exist for the host platform. Displaying the page from ${targets[0].os} platform`);
targetPlatform = targets[0].platform;
console.log(`Command ${page} does not exist for the host platform. Displaying the page from ${targetPlatform} platform`);
}

if (!targetLanguage && !targetPlatform) {
Expand All @@ -81,7 +81,7 @@ function findPage(page, preferredPlatform, preferredLanguage) {

function hasPlatformLang(targets, preferredPlatform, preferredLanguage) {
return targets.some((t) => {
return t.os === preferredPlatform && t.language === preferredLanguage;
return t.platform === preferredPlatform && t.language === preferredLanguage;
});
}

Expand Down Expand Up @@ -116,7 +116,7 @@ function commandsFor(platform) {
let commands = Object.keys(idx)
.filter((cmd) => {
let targets = idx[cmd].targets;
let platforms = targets.map((t) => {return t.os;});
let platforms = targets.map((t) => {return t.platform;});
return platforms.indexOf(platform) !== -1 || platforms.indexOf('common') !== -1;
})
.sort();
Expand Down Expand Up @@ -187,24 +187,24 @@ function buildShortPagesIndex() {
.then((files) => {
files = files.filter(utils.isPage);
let reducer = (index, file) => {
let os = utils.parsePlatform(file);
let platform = utils.parsePlatform(file);
let page = utils.parsePagename(file);
let language = utils.parseLanguage(file);
if (index[page]) {
let targets = index[page].targets;
let needsPush = true;
for (const target of targets) {
if (target.platform === os && target.language === language) {
if (target.platform === platform && target.language === language) {
needsPush = false;
continue;
}
}
if (needsPush) {
targets.push({'os': os, 'language': language});
targets.push({ platform, language });
index[page].targets = targets;
}
} else {
index[page] = {targets: [{'os': os, 'language': language}]};
index[page] = {targets: [{ platform, language }]};
}

return index;
Expand Down
1 change: 1 addition & 0 deletions lib/platform.js → lib/platforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const folders = {
'darwin': 'osx',
'linux': 'linux',
'sunos': 'sunos',
'android': 'android',
'windows': 'windows',
'win32': 'windows'
};
Expand Down
8 changes: 4 additions & 4 deletions lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const natural = require('natural');
const config = require('./config');
const utils = require('./utils');
const index = require('./index');
const platform = require('./platform');
const platforms = require('./platforms');

const CACHE_FOLDER = path.join(config.get().cache, 'cache');

Expand Down Expand Up @@ -189,16 +189,16 @@ exports.printResults = (results, config) => {
// Prints the passed results to the console.
// If the command is not available for the current platform,
// it lists the available platforms instead.
// Example: tldr --search print directory tree --os sunos prints:
// Example: tldr --search print directory tree --platform sunos prints:
// $ tree (Available on: linux, osx)
index.getShortIndex().then((shortIndex) => {
let outputs = new Set();
let preferredPlatform = platform.getPreferredPlatform(config);
let preferredPlatform = platforms.getPreferredPlatform(config);
results.forEach((elem) => {
let cmdname = utils.parsePagename(elem.file);
let output = ' $ ' + cmdname;
let targets = shortIndex[cmdname]['targets'];
let platforms = targets.map((t) => {return t.os;});
let platforms = targets.map((t) => {return t.platform;});
if (platforms.indexOf('common') === -1 && platforms.indexOf(preferredPlatform) === -1) {
output += ' (Available on: ' + platforms.join(', ') + ')';
}
Expand Down
14 changes: 7 additions & 7 deletions lib/tldr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const ora = require('ora');
const { EmptyCacheError, MissingPageError, MissingRenderPathError } = require('./errors');
const Cache = require('./cache');
const search = require('./search');
const platform = require('./platform');
const platforms = require('./platforms');
const parser = require('./parser');
const render = require('./render');
const index = require('./index');
Expand All @@ -22,8 +22,8 @@ class Tldr {
}

list(singleColumn) {
let os = platform.getPreferredPlatformFolder(this.config);
return index.commandsFor(os)
let platform = platforms.getPreferredPlatformFolder(this.config);
return index.commandsFor(platform)
.then((commands) => {
return this.printPages(commands, singleColumn);
});
Expand All @@ -41,8 +41,8 @@ class Tldr {
}

random(options) {
let os = platform.getPreferredPlatformFolder(this.config);
return index.commandsFor(os)
let platform = platforms.getPreferredPlatformFolder(this.config);
return index.commandsFor(platform)
.then((pages) => {
if (pages.length === 0) {
throw new EmptyCacheError();
Expand All @@ -57,8 +57,8 @@ class Tldr {
}

randomExample() {
let os = platform.getPreferredPlatformFolder(this.config);
return index.commandsFor(os)
let platform = platforms.getPreferredPlatformFolder(this.config);
return index.commandsFor(platform)
.then((pages) => {
if (pages.length === 0) {
throw new EmptyCacheError();
Expand Down
Loading