Skip to content
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

Improve githunt #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion Contributors.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Kyle (https://github.com/kc8)
[Kyle](https://github.com/kc8)
[supremestdoggo](https://github.com/kc8)
23 changes: 16 additions & 7 deletions githunt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Standard library imports
from datetime import datetime, timedelta
from sys import stdout

# Third party imports
import click
Expand All @@ -14,23 +15,31 @@


@click.command()
@click.option("--language", "-l", default="", help="language filter (eg: python)")
@click.option("--language", "-l", default="", help="Language filter (e.g. Python)")
@click.option(
"--date",
"-d",
default="",
help="date in the ISO8601 format which is YYYY-MM-DD (year-month-day)",
help="ISO-8601-formatted date (YYYY-MM-DD)",
)
@click.option(
"--fmt",
"-f",
default="colored",
help="output format, it can be either table or colored",
help="Output format (table or colored)",
)
def search(language, date, fmt):
""" Returns repositories based on the language.
repositories are sorted by stars
@click.option(
"--output",
"-o",
default="",
help="File to pipe output to."
)
def search(language, date, fmt, output):
"""
Returns repositories based on the language. Repositories are sorted by stars
"""
if output == "":
output = stdout

if not date:
start_date = datetime.fromisoformat(
Expand All @@ -49,7 +58,7 @@ def search(language, date, fmt):
query += f"+language:{language}" if language else ""
url = f"{API_URL}?q={query}&sort=stars&order=desc"
repositories = requests.get(url).json()
beautify(repositories["items"], fmt)
beautify(repositories["items"], fmt, output)


if __name__ == "__main__":
Expand Down
24 changes: 15 additions & 9 deletions githunt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@

# Standard library imports
import textwrap
from sys import stderr

# Third party imports
from colorama import init, Fore, Style
from tabulate import tabulate



def eprint(string):
""" Prints a string to stderr. """
print(string, file=stderr)

def make_hyperlink(text, target):
""" Makes hyperlink out of text and target and retuns it
https://stackoverflow.com/questions/44078888/clickable-html-links-in-python-3-6-shell
"""
return f"\u001b]8;;{target}\u001b\\{text}\u001b]8;;\u001b\\"


def colored_output(repos):
def colored_output(repos, output):
""" Displays repositories using colorama """

init() # initialize coloroma
Expand All @@ -33,7 +39,7 @@ def colored_output(repos):
"\n ".join(textwrap.wrap(f"{repo['description']}", len(seperator))),
end="\n\n",
)
print(Fore.LIGHTCYAN_EX, Style.BRIGHT, f"{repo['language']}", end="\t")
print(Fore.LIGHTCYAN_EX, Style.BRIGHT, repo['language'], end="\t")
print(
Fore.LIGHTCYAN_EX,
Style.BRIGHT,
Expand All @@ -47,10 +53,10 @@ def colored_output(repos):
f"{repo['watchers_count']} Watchers",
end="\n\n",
)
print(Fore.WHITE, Style.BRIGHT, seperator, end="\n\n")
print(Fore.WHITE, Style.BRIGHT, seperator, end="\n\n", file=output)


def tabular_output(repos):
def tabular_output(repos, output):
""" Displays repositories as tables using tabulate """
table_headers = ["URL", "Language", "Stars", "Forks", "Watches"]
repositories = [
Expand All @@ -63,14 +69,14 @@ def tabular_output(repos):
]
for repo in repos
]
print(tabulate(repositories, headers=table_headers, tablefmt="fancy_grid"))
print(tabulate(repositories, headers=table_headers, tablefmt="fancy_grid"), file=output)


def beautify(repos, fmt):
""" Beautfies the output based on display format given """
def beautify(repos, fmt, output):
""" Beautfies the output based on the display format given """
if fmt == "colored":
colored_output(repos)
colored_output(repos, output)
elif fmt == "table":
tabular_output(repos)
else:
print("Can't output anything. Invalid display format!")
eprint("Can't output anything. Invalid display format!")
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import io
import pathlib
import re

from setuptools import setup
Expand All @@ -16,7 +15,7 @@
version=version,
description="Browse most stared Github repositories by date from your command line.",
long_description=readme,
python_requires='>3.7',
python_requires='=>3.7',
long_description_content_type="text/markdown",
url="https://github.com/SriNandan33/githunt",
author="Srinivasa Rao",
Expand Down