Skip to content

Instantly share code, notes, and snippets.

@iTrooz
Created January 26, 2024 21:24
Show Gist options
  • Save iTrooz/8a164ea2821fb8b1f017c207ee3627dc to your computer and use it in GitHub Desktop.
Save iTrooz/8a164ea2821fb8b1f017c207ee3627dc to your computer and use it in GitHub Desktop.
Check commands that you can add to tldr
#!/usr/bin/env python3
# This script will check for commands that you could most easily contribute to tldr (https://tldr.sh/)
# Run it in the root of the https://github.com/tldr-pages/tldr repository
import os
import sys
def get_used_commands():
used_cmds = {}
for filepath, complex in {"~/.zsh_history": True, "~/.bash_history": False}.items():
with open(os.path.expanduser(filepath), "rb") as file:
line_n = 0
for line in file:
line_n += 1
try:
line = line.decode()
except UnicodeDecodeError as e:
print(f"Failed to parse line {line_n} of {filepath}: {e}", file=sys.stderr)
continue
if complex:
full_cmd = line[line.find(";")+1:]
else:
full_cmd = line
cmd = full_cmd[:full_cmd.find(" ")]
cmd = os.path.basename(cmd)
if cmd in used_cmds:
used_cmds[cmd] += 1
else:
used_cmds[cmd] = 1
return used_cmds
tldr_cmds = []
for subdir in os.listdir("./pages"):
for file in os.listdir(os.path.join("./pages", subdir)):
tldr_cmds.append(os.path.splitext(file)[0])
existing_cmds = []
for path in os.getenv("PATH").split(":"):
if not os.path.exists(path):
continue
for cmd in os.listdir(path):
existing_cmds.append(cmd)
used_cmds = get_used_commands()
todo_cmds = {}
for cmd, count in used_cmds.items():
if cmd in existing_cmds and cmd not in tldr_cmds:
todo_cmds[cmd] = count
for cmd, count in sorted(todo_cmds.items(), key=lambda item: item[1]):
print(f"{cmd:<20} count={count}")
print("\n--- Stats:")
print(f"Commands in tldr db: {len(tldr_cmds)}")
print(f"Commands in PATH: {len(existing_cmds)}")
print(f"Unique commands used: {len(used_cmds)}")
print(f"Commands to put in tldr db that are used (shown above): {len(todo_cmds)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment