1.3.2 - exit silently
This commit is contained in:
		
							parent
							
								
									c1886b96fb
								
							
						
					
					
						commit
						ff18f0581c
					
				
					 6 changed files with 26 additions and 7 deletions
				
			
		| 
						 | 
				
			
			@ -35,7 +35,7 @@ You can use another similar projects set of words as well, for example to create
 | 
			
		|||
 | 
			
		||||
`curl -s https://raw.githubusercontent.com/Miodec/monkeytype/master/static/languages/english.json | python3 -c "import sys, json; print('\n'.join(json.load(sys.stdin)['words']))" | shuf -n20 | python3 -m fasttyper`
 | 
			
		||||
 | 
			
		||||
To exit you can either finish test, use `KeyboardInterrupt` (CTRL+C) or tap **tab** then **enter**.
 | 
			
		||||
To exit you can either finish test, use `KeyboardInterrupt` (CTRL+C) or tap **tab** then **enter**. After you finish test, there will be summary printed, use enter to exit from it.
 | 
			
		||||
 | 
			
		||||
## Example scripts
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -40,6 +40,7 @@ def initialize(configmap, rbuffer):
 | 
			
		|||
    wrapper(interface)
 | 
			
		||||
 | 
			
		||||
    application.summarize()
 | 
			
		||||
    application.exit()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,9 +1,14 @@
 | 
			
		|||
import sys
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Application:
 | 
			
		||||
    def __init__(self, listener, user_buffer, reference_buffer, config):
 | 
			
		||||
        self.listener = listener
 | 
			
		||||
        self.user_buffer = user_buffer
 | 
			
		||||
        self.reference_buffer = reference_buffer
 | 
			
		||||
        self.config = config
 | 
			
		||||
        self.finished = False
 | 
			
		||||
        self.silent_exit = False
 | 
			
		||||
 | 
			
		||||
        from .state import StateMachine
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -38,14 +43,26 @@ class Application:
 | 
			
		|||
            self.user_buffer.handle_action(action, key)
 | 
			
		||||
            self.state.update(action, key)
 | 
			
		||||
            self.stats.update(action, self.state.valid(), self.state.running())
 | 
			
		||||
        except StoppingSignal:
 | 
			
		||||
        except StoppingSignal as e:
 | 
			
		||||
            self.state.signal_stop()
 | 
			
		||||
            self.stats.signal_stop()
 | 
			
		||||
            if e.silent:
 | 
			
		||||
                self.silent_exit = True
 | 
			
		||||
 | 
			
		||||
    def summarize(self):
 | 
			
		||||
        self.stats.summarize(self.config.get("summary_template"))
 | 
			
		||||
        self.stats.export_to_datafile(self.config.get("summary_datafile"))
 | 
			
		||||
        if self.finished:
 | 
			
		||||
            self.stats.summarize(self.config.get("summary_template"))
 | 
			
		||||
            self.stats.export_to_datafile(self.config.get("summary_datafile"))
 | 
			
		||||
            input()
 | 
			
		||||
 | 
			
		||||
    def exit(self):
 | 
			
		||||
        if self.finished or self.silent_exit:
 | 
			
		||||
            sys.exit(0)
 | 
			
		||||
        else:
 | 
			
		||||
            sys.exit(1)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class StoppingSignal(Exception):
 | 
			
		||||
    pass
 | 
			
		||||
    def __init__(self, *args, silent=False, **kwargs):
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
        self.silent = silent
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -21,7 +21,7 @@ class Listener:
 | 
			
		|||
        if key == "\t":
 | 
			
		||||
            self.tabbed = True
 | 
			
		||||
        elif key == "\n" and self.tabbed:
 | 
			
		||||
            raise StoppingSignal()
 | 
			
		||||
            raise StoppingSignal(silent=True)
 | 
			
		||||
        elif key == 263:
 | 
			
		||||
            action = Action.del_word
 | 
			
		||||
            self.tabbed = False
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -39,6 +39,7 @@ class StateMachine:
 | 
			
		|||
                    self.state = State.invalid
 | 
			
		||||
                elif user_position == self.application.reference_buffer.get_lenght():
 | 
			
		||||
                    self.state = State.finished
 | 
			
		||||
                    self.application.finished = True
 | 
			
		||||
 | 
			
		||||
    def valid(self):
 | 
			
		||||
        return self.state in (State.valid, State.finished)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										2
									
								
								setup.py
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								setup.py
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
 | 
			
		|||
 | 
			
		||||
setup(
 | 
			
		||||
    name="fasttyper",
 | 
			
		||||
    version="1.3.1",
 | 
			
		||||
    version="1.3.2",
 | 
			
		||||
    author="Piotr Domanski",
 | 
			
		||||
    author_email="pi.domanski@gmail.com",
 | 
			
		||||
    description="Minimalistic typing exercise",
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue