more options
This commit is contained in:
parent
772c66e132
commit
2deb0488f0
5 changed files with 154 additions and 12 deletions
|
@ -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)
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
[bumpversion]
|
||||
current_version = 2.3.2
|
||||
current_version = 2.4.0
|
||||
|
||||
[wheel]
|
||||
universal = 1
|
||||
|
|
2
setup.py
2
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",
|
||||
|
|
Loading…
Reference in a new issue