Skip to content

Commit

Permalink
Quotes mode
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenXie committed Dec 31, 2021
1 parent 77b7aeb commit d0ae13a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 12 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Project exclude paths
*.pyc
**/__pycache__/
__pycache__/
.idea/
.vscode/
typertype/src/__pycache__
typertype/__pycache__
7 changes: 7 additions & 0 deletions settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"WORDS_PER_SET": 20,
"CORRECTIONS": "None",
"TEST_TIMER": 60,
"SPACE_STOP": true,
"WORD_MODIFICATION": "Normal"
}
11 changes: 0 additions & 11 deletions typertype/quotes.py

This file was deleted.

3 changes: 2 additions & 1 deletion typertype/src/general.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .words_mode import Typer_words
from .time_mode import Typer_time
from .quotes_mode import Typer_quotes


class Typer(Typer_words,Typer_time):
class Typer(Typer_words,Typer_time, Typer_quotes):
pass
2 changes: 2 additions & 0 deletions typertype/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ def home(self, stdscr):
self.practice_set(stdscr)
elif arrow_y == 2:
self.test_set(stdscr)
elif arrow_y == 3:
self.quotes_set(stdscr)
elif arrow_y == 4:
self.Settings(stdscr)
stdscr.clear()
Expand Down
79 changes: 79 additions & 0 deletions typertype/src/quotes_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import requests
import random
import curses
import time
from . import main

quotes = requests.get('https://type.fit/api/quotes').json()

def get_quote():
return quotes[random.randint(0, 1600)]['text']

class Typer_quotes(main.Typer_base):
def quotes_set(self,stdscr):
started = False
l, r = 0, self.WIN_X-self.X-1
accuracy = {"correct": 0, "incorrect": 0, "extra": 0}
stdscr.clear()
raw_set = get_quote().split()
chr_set = list(" ".join(raw_set))
cur_set = [[chr, curses.color_pair(1)] for chr in chr_set]
self.draw(stdscr, self.Y, self.X, cur_set[l:r+1])
i = 0
while i < len(cur_set):
cur_key = stdscr.getch()
if not started:
started = True
t0 = time.time()
if cur_key == ord(chr_set[i]):
if self.CORRECTIONS == "Stop":
if cur_set[i][1] == curses.color_pair(2) | curses.A_BLINK:
cur_set[i][1] = curses.color_pair(2)
else:
cur_set[i][1] = curses.color_pair(3)
else:
cur_set[i][1] = curses.color_pair(3)

if i < len(cur_set)-1:
cur_set[i+1][1] = curses.color_pair(1) | curses.A_BLINK

i += 1
if i >= self.WIN_X//2:
l += 1
r += 1
accuracy["correct"] += 1
elif cur_key == 27 or cur_key == curses.KEY_COPY or cur_key == curses.KEY_END or cur_key == curses.KEY_BREAK:
return
# backspace:
elif cur_key == 8 or cur_key == curses.KEY_BACKSPACE:
if cur_set[i-1][1] == curses.color_pair(2) and self.CORRECTIONS == "Backspace":
cur_set[i][1] = curses.color_pair(1)
cur_set[i-1][1] = curses.color_pair(2) | curses.A_BLINK
i -= 1
if i >= self.WIN_X//2:
l -= 1
r -= 1
elif not(self.SPACE_STOP and cur_key == ord(" ")):
cur_set[i][1] = curses.color_pair(2)
if self.CORRECTIONS == "Stop":
cur_set[i][1] = curses.color_pair(2) | curses.A_BLINK
if i < len(cur_set)-1 and self.CORRECTIONS != "Stop":
cur_set[i+1][1] = curses.color_pair(1) | curses.A_BLINK
if chr_set[i] == " ":
accuracy["extra"] += 1
else:
accuracy["incorrect"] += 1
if self.CORRECTIONS == "None" or self.CORRECTIONS == "Backspace":
i += 1
if i >= self.WIN_X//2:
l += 1
r += 1
self.draw(stdscr, self.Y, self.X, cur_set[l:r+1])
t1 = time.time()
my_time = t1-t0
cpm = accuracy["correct"]*(60/my_time)
wpm = self.WORDS_PER_SET*(60/my_time)
if self.score(stdscr, wpm, cpm, accuracy):
self.practice_set(stdscr)
else:
return

0 comments on commit d0ae13a

Please sign in to comment.