From 83ff9d58c30c30002f42ad13dd754dbb83e9d006 Mon Sep 17 00:00:00 2001 From: Doman Date: Sat, 12 Feb 2022 22:45:26 +0100 Subject: [PATCH] added executable --- README.md | 30 ++++++++++++++++ fasttyper/__main__.py | 78 ++-------------------------------------- fasttyper/application.py | 4 +-- fasttyper/cli.py | 77 +++++++++++++++++++++++++++++++++++++++ fasttyper/listener.py | 2 +- fasttyper/runner.py | 55 ++++++++++++++++++++++++++++ setup.cfg | 8 ----- setup.py | 5 +++ 8 files changed, 172 insertions(+), 87 deletions(-) create mode 100644 fasttyper/cli.py create mode 100644 fasttyper/runner.py diff --git a/README.md b/README.md index f9df22c..a980875 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,36 @@ _Fasttyper_ is currently maintained on [PyPi](https://pypi.org/) Python Package # Usage +With installation of fasttyper you should have new executable - `fasttyper`. It takes two optional positional arguments, amount of words and language (like in monkeytype: english, english_1k etc). + +Avalibe options: + +``` +usage: fasttyper [-h] [--config FILE] [--unclutter-backspace] [--no-cursor] [amount] [language] + +positional arguments: + amount Amount of words + language Language + +options: + -h, --help show this help message and exit + --config FILE, -c FILE + configuration file + --unclutter-backspace, -b + unclutter backspace, when it raises ctrl+backspace instead + --no-cursor, -n disable cursos +``` + +I personally use alias: + +``` +alias ff='fasttyper -n` +``` + +because i don't like having cursor. + +# Usage as module, piping stuff, custom scripts etc + _Fasttyper_ is ran as an python module, so to execute it simply type: `python3 -m fasttyper` diff --git a/fasttyper/__main__.py b/fasttyper/__main__.py index 67f99a5..0c9e21e 100644 --- a/fasttyper/__main__.py +++ b/fasttyper/__main__.py @@ -1,81 +1,15 @@ -from .application import Application -from .interface import Interface -from .components import ( - CursorComponent, - StatsComponent, - TextBox, - TopMargin, -) -from .listener import Listener -from .buffer import UserBuffer, Buffer -from .config import Config -from curses import wrapper +from .cli import initialize, get_parser import sys -import io import os -import argparse -import json - - -def initialize(configmap, rbuffer, backspace_debug, no_cursor): - config = Config(configmap) - - reference_buffer = Buffer(rbuffer) - user_buffer = UserBuffer() - - top_margin = TopMargin(config) - cursor_component = CursorComponent(config) - text_box = TextBox(config, cursor_component) - stats_component = StatsComponent(config) - - listener = Listener(backspace_debug) - application = Application(listener, user_buffer, reference_buffer, config) - - interface = Interface( - application, - [ - top_margin, - text_box, - stats_component, - cursor_component, - ], - no_cursor, - ) - wrapper(interface) - - application.summarize() - application.exit() def main(): is_tty = sys.stdin.isatty() - - parser = argparse.ArgumentParser() + parser = get_parser() if is_tty: parser.add_argument("file", metavar="FILE", help="file to type") - parser.add_argument( - "--config", - "-c", - metavar="FILE", - help="configuration file", - default="~/.config/fasttyper/config.json", - ) - parser.add_argument( - "--unclutter-backspace", - "-b", - action="store_true", - help="unclutter backspace, when it raises ctrl+backspace instead", - default=False, - ) - parser.add_argument( - "--no-cursor", - "-n", - action="store_true", - help="disable cursos", - default=False, - ) args = parser.parse_args() if is_tty: @@ -89,13 +23,7 @@ def main(): rbuffer = "".join(input_lines) - try: - with open(os.path.expanduser(args.config)) as f: - configmap = json.load(f) - except FileNotFoundError: - configmap = {} - - initialize(configmap, rbuffer, args.unclutter_backspace, args.no_cursor) + initialize(args.config, rbuffer, args.unclutter_backspace, args.no_cursor) if __name__ == "__main__": diff --git a/fasttyper/application.py b/fasttyper/application.py index bb8a7bf..258d78d 100644 --- a/fasttyper/application.py +++ b/fasttyper/application.py @@ -58,9 +58,7 @@ class Application: input() def exit(self): - if self.finished or self.silent_exit: - sys.exit(0) - else: + if not self.finished and not self.silent_exit: sys.exit(1) diff --git a/fasttyper/cli.py b/fasttyper/cli.py new file mode 100644 index 0000000..fd73d3b --- /dev/null +++ b/fasttyper/cli.py @@ -0,0 +1,77 @@ +from .application import Application +from .interface import Interface +from .components import ( + CursorComponent, + StatsComponent, + TextBox, + TopMargin, +) +from .listener import Listener +from .buffer import UserBuffer, Buffer +from .config import Config +from curses import wrapper +import os +import argparse +import json + + +def initialize(config_path, rbuffer, backspace_debug, no_cursor): + try: + with open(os.path.expanduser(config_path)) as f: + configmap = json.load(f) + except FileNotFoundError: + configmap = {} + + config = Config(configmap) + + reference_buffer = Buffer(rbuffer) + user_buffer = UserBuffer() + + top_margin = TopMargin(config) + cursor_component = CursorComponent(config) + text_box = TextBox(config, cursor_component) + stats_component = StatsComponent(config) + + listener = Listener(backspace_debug) + application = Application(listener, user_buffer, reference_buffer, config) + + interface = Interface( + application, + [ + top_margin, + text_box, + stats_component, + cursor_component, + ], + no_cursor, + ) + wrapper(interface) + + application.summarize() + application.exit() + + +def get_parser(): + parser = argparse.ArgumentParser() + parser.add_argument( + "--config", + "-c", + metavar="FILE", + help="configuration file", + default="~/.config/fasttyper/config.json", + ) + parser.add_argument( + "--unclutter-backspace", + "-b", + action="store_true", + help="unclutter backspace, when it raises ctrl+backspace instead", + default=False, + ) + parser.add_argument( + "--no-cursor", + "-n", + action="store_true", + help="disable cursos", + default=False, + ) + return parser diff --git a/fasttyper/listener.py b/fasttyper/listener.py index 323b979..8013ec4 100644 --- a/fasttyper/listener.py +++ b/fasttyper/listener.py @@ -14,7 +14,7 @@ class Action(enum.Enum): class Listener: def __init__(self, backspace_debug=False): self.tabbed = False - self.backspace_debug = backspace_debug + self.backspace_debug = not backspace_debug def handle_key(self, key): action = Action.invalid diff --git a/fasttyper/runner.py b/fasttyper/runner.py new file mode 100644 index 0000000..eb89030 --- /dev/null +++ b/fasttyper/runner.py @@ -0,0 +1,55 @@ +from .cli import initialize, get_parser +from random import choice +import os +import requests +import pathlib + + +MONKEYTYPE_WORDS_PATT = "https://raw.githubusercontent.com/Miodec/monkeytype/master/frontend/static/languages/{language}.json" + + +def get_words(language): + home = os.environ.get("HOME", "") + sfile = os.path.join(home, ".cache", "fasttyper", language) + words = None + + try: + with open(sfile) as f: + words = f.read().splitlines() + except FileNotFoundError: + pass + + if words is None: + source_path = MONKEYTYPE_WORDS_PATT.format(language=language) + + words = requests.get(source_path).json()["words"] + + p = pathlib.Path(os.path.join(home, ".cache", "fasttyper")) + p.mkdir(parents=True, exist_ok=True) + + with open(sfile, "w") as f: + f.write("\n".join(words)) + + return words + + +def runner(): + parser = get_parser() + + parser.add_argument( + "amount", type=int, default=25, help="Amount of words", nargs="?" + ) + parser.add_argument( + "language", type=str, default="english", help="Language", nargs="?" + ) + + args = parser.parse_args() + words = get_words(args.language) + + while True: + rbuffer = " ".join([choice(words) for _ in range(args.amount)]) + initialize(args.config, rbuffer, args.unclutter_backspace, args.no_cursor) + + +if __name__ == "__main__": + runner() diff --git a/setup.cfg b/setup.cfg index c15cde7..e69de29 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,8 +0,0 @@ -[bumpversion] -current_version = 1.5.2 - -[wheel] -universal = 1 - -[metadata] -description-file = README.md diff --git a/setup.py b/setup.py index 775f408..bf6abc7 100644 --- a/setup.py +++ b/setup.py @@ -19,4 +19,9 @@ setup( ], packages=find_packages(), python_requires=">=3.6", + entry_points={ + "console_scripts": [ + "fasttyper=fasttyper.runner:runner", + ] + }, )