From 2deb0488f04a0d198d5b55745b3244cf1ac1897d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Doma=C5=84ski?= Date: Sat, 3 Dec 2022 15:53:47 +0100 Subject: [PATCH] more options --- fasttyper/cli.py | 25 +++++++-- fasttyper/config.py | 9 ++++ fasttyper/runner.py | 128 +++++++++++++++++++++++++++++++++++++++++--- setup.cfg | 2 +- setup.py | 2 +- 5 files changed, 154 insertions(+), 12 deletions(-) diff --git a/fasttyper/cli.py b/fasttyper/cli.py index 5a6b600..33ae62f 100644 --- a/fasttyper/cli.py +++ b/fasttyper/cli.py @@ -24,7 +24,7 @@ class RuntimeConfig: self.mode = mode -def initialize(config_path, rbuffer, backspace_debug, no_cursor, runtime_config): +def get_config(config_path): try: with open(os.path.expanduser(config_path)) as f: configmap = json.load(f) @@ -32,6 +32,17 @@ def initialize(config_path, rbuffer, backspace_debug, no_cursor, runtime_config) configmap = {} config = Config(configmap) + return config + + +def initialize(config, rbuffer, backspace_debug, no_cursor, runtime_config): + if isinstance(config, str): + config = get_config(config) + + backspace_debug = ( + backspace_debug(config) if callable(backspace_debug) else backspace_debug + ) + no_cursor = no_cursor(config) if callable(no_cursor) else no_cursor text_box = TextBox(config) summary = Summary(config) @@ -71,13 +82,21 @@ def get_parser(): "-b", action="store_true", help="unclutter backspace, when it raises ctrl+backspace instead", - default=False, + default=ValueFromConfig("unclutter_backspace"), ) parser.add_argument( "--no-cursor", "-n", action="store_true", help="disable cursos", - default=False, + default=ValueFromConfig("no_cursor"), ) return parser + + +class ValueFromConfig: + def __init__(self, key): + self.key = key + + def __call__(self, config): + return config.get(self.key) diff --git a/fasttyper/config.py b/fasttyper/config.py index d82532c..9f4649b 100644 --- a/fasttyper/config.py +++ b/fasttyper/config.py @@ -26,6 +26,15 @@ class Config: "summary_color": 9, "summary_lines": 4, "summary_border": 0, + "random_capitalization": 0, + "random_punctuation": 0, + "sentence_mode": False, + "punctuation": ",.?!;:", + "sentence_ending": ".?!", + "amount": 25, + "language": "english", + "no_cursor": False, + "unclutter_backspace": False, } def __init__(self, configmap): diff --git a/fasttyper/runner.py b/fasttyper/runner.py index 6a270e1..fa3dac4 100644 --- a/fasttyper/runner.py +++ b/fasttyper/runner.py @@ -1,11 +1,13 @@ -from .cli import initialize, get_parser, RuntimeConfig -from random import choice +from .cli import initialize, get_parser, RuntimeConfig, get_config, ValueFromConfig +from random import choice, random import os import requests import pathlib MONKEYTYPE_WORDS_PATT = "https://raw.githubusercontent.com/Miodec/monkeytype/master/frontend/static/languages/{language}.json" +PUNCTUATION = ",.?!;:" +PUNCTUATION_ENDING_SENTENCE = ".?!" def get_words(language): @@ -33,23 +35,135 @@ def get_words(language): return words +def generate_text( + words, + amount, + capitalization_factor, + punctuation_factor, + sentence_mode, + punctuation, + sentence_ending, +): + chosen_words = [] + + new_sentence = True + for _ in range(amount): + word = choice(words) + + if sentence_mode and new_sentence: + word = word.capitalize() + new_sentence = False + elif capitalization_factor and capitalization_factor / 100 > random(): + word = word.capitalize() + + if punctuation_factor and punctuation_factor / 100 > random(): + punctuation = choice(punctuation) + word = "".join((word, punctuation)) + + new_sentence = word[-1] in sentence_ending + chosen_words.append(word) + + return " ".join(chosen_words) + + def runner(): parser = get_parser() parser.add_argument( - "amount", type=int, default=25, help="Amount of words", nargs="?" + "amount", + type=int, + default=ValueFromConfig("amount"), + help="Amount of words", + nargs="?", ) parser.add_argument( - "language", type=str, default="english", help="Language", nargs="?" + "language", + type=str, + default=ValueFromConfig("language"), + help="Language", + nargs="?", + ) + parser.add_argument( + "-rc", + "--random-capitalization", + type=int, + default=ValueFromConfig("random_capitalization"), + help="Percent of randomally capitalized words", + action="store", + ) + parser.add_argument( + "-rp", + "--random-punctuation", + type=int, + default=ValueFromConfig("random_capitalization"), + help="Percent of words that will have randomally appender punctuation at the end", + action="store", + ) + parser.add_argument( + "-sm", + "--sentence-mode", + default=ValueFromConfig("sentence_mode"), + help="Forces uppercase after sentence ending characters like dot or question mark and at the beggining of text", + action="store_true", + ) + parser.add_argument( + "--punctuation", + type=str, + default=ValueFromConfig("punctuation"), + help="List of all punctuation characters", + action="store", + ) + parser.add_argument( + "--sentence-ending", + type=str, + default=ValueFromConfig("sentence_ending"), + help="List of all punctuation characters that end sentence", + action="store", ) args = parser.parse_args() - words = get_words(args.language) + config = get_config(args.config) + + random_punctuation = ( + args.random_punctuation(config) + if callable(args.random_punctuation) + else args.random_punctuation + ) + random_capitalization = ( + args.random_capitalization(config) + if callable(args.random_capitalization) + else args.random_capitalization + ) + sentence_mode = ( + args.sentence_mode(config) + if callable(args.sentence_mode) + else args.sentence_mode + ) + punctuation = ( + args.punctuation(config) if callable(args.punctuation) else args.punctuation + ) + sentence_ending = ( + args.sentence_ending(config) + if callable(args.sentence_ending) + else args.sentence_ending + ) + amount = args.amount(config) if callable(args.amount) else args.amount + language = args.language(config) if callable(args.language) else args.language + + words = get_words(language) while True: - rbuffer = " ".join([choice(words) for _ in range(args.amount)]) + rbuffer = generate_text( + words, + amount, + random_capitalization, + random_punctuation, + sentence_mode, + punctuation, + sentence_ending, + ) initialize( - args.config, + config, rbuffer, args.unclutter_backspace, args.no_cursor, diff --git a/setup.cfg b/setup.cfg index b1e9891..ae3898f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 2.3.2 +current_version = 2.4.0 [wheel] universal = 1 diff --git a/setup.py b/setup.py index fd718c8..8ba73ed 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ with open("requirements.txt", "r", encoding="utf-8") as fh: setup( name="fasttyper", - version="2.3.2", + version="2.4.0", author="Piotr Domanski", author_email="pi.domanski@gmail.com", description="Minimalistic typing exercise",