Skip to content

Instantly share code, notes, and snippets.

@NonlinearFruit
Last active November 19, 2019 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NonlinearFruit/07cca3536e321ec1b92045548ed192bd to your computer and use it in GitHub Desktop.
Save NonlinearFruit/07cca3536e321ec1b92045548ed192bd to your computer and use it in GitHub Desktop.
Plugin for termsaver to allow displaying quotes from local file
# git clone https://github.com/brunobraga/termsaver.git
# curl https://gist.githubusercontent.com/NonlinearFruit/07cca3536e321ec1b92045548ed192bd/raw -o termsaver/termsaverlib/screen/quotes.py
# curl https://gist.githubusercontent.com/NonlinearFruit/5ff69f8291b4d241a60ca445c06508bf/raw -o wcf.json
# curl https://gist.githubusercontent.com/NonlinearFruit/dda52038c29f26b83d337c82f25bdef8/raw -o wlc.json
# jq -r '.[] | . as $parent | .Sections[] | { title: $parent.Title, chapter: $parent.Chapter, section: .Section, content: .Content } | "\(.title) (WCF \(.chapter).\(.section))\n\(.content)\n\n"' wcf.json > wcf.saver
# jq -r '.[] | "\(.Question) (WLC \(.Number))\n\(.Answer)\n\n"' wlc.json >> wcf.saver
# python termsaver/termsaver quotes -p wcf.saver
import time
import sys
from termsaverlib.screen.base import ScreenBase
from termsaverlib.screen.helper.typing import TypingHelperBase
from termsaverlib.screen.helper.position import PositionHelperBase
from termsaverlib import common, exception, constants
from termsaverlib.i18n import _
class QuotesScreen(ScreenBase, TypingHelperBase, PositionHelperBase):
sleep_between_items = 10
sleep_between_characters = 0.05
sleep_between_lines = 0.5
data = None
def __init__(self):
ScreenBase.__init__(self, "quotes", "display quotes from local file", {
'opts': ':p:',
'long_opts': ['path='],
})
def _usage_options_example(self):
print _(""" """)
def _parse_args(self, prepared_args):
for option, arg in prepared_args[0]:
if option in ("-p", "--path"):
self._read_file_into_data(arg)
def _read_file_into_data(self, path):
with open(path, 'r') as content_file:
self.data = content_file.read().split("\n\n")
def _run_cycle(self):
self.clear_screen()
for item in self.data:
self.get_terminal_size()
item = self.center_text_vertically(item)
item = self.center_text_horizontally(item)
##
lines = item.split('\n')
for index, line in enumerate(lines):
for char in line:
sys.stdout.write(char)
if char != ' ':
time.sleep(self.sleep_between_characters)
sys.stdout.flush()
sys.stdout.write('\n')
if not line.isspace():
time.sleep(self.sleep_between_lines)
##
time.sleep(self.sleep_between_items)
self.clear_screen()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment