repl.it
@PYer/

PYsel

Python

No description

fork
loading
Files
  • main.py
  • Packager files
  • poetry.lock
  • pyproject.toml
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
import readchar
import colorama

def pysel(prompt, opts, default=0, asterix=False, align=True):
  curr = default
  if asterix:
    maxlen = 0
    for opt in opts:
      if len(opt) > maxlen:
        maxlen = len(opt)
  while True:
    print(prompt + '\n\n')
    for i in range(len(opts)):
      if i == curr:
        if not asterix:
          print(colorama.Fore.RED + opts[i] + colorama.Fore.RESET)
        else:
          if not align:
            print('\t'+ colorama.Fore.YELLOW + '*' + colorama.Fore.RESET + opts[i] + colorama.Fore.YELLOW + '*' + colorama.Fore.RESET)
          else:
            print('\t'+ colorama.Fore.YELLOW + '*' + colorama.Fore.RESET + opts[i] + colorama.Fore.YELLOW + (' '*(maxlen-len(opts[i]))) + '*' + colorama.Fore.RESET)
      else: 
        if not asterix: print(opts[i])
        else: print('\t ' + opts[i])
    char = repr(readchar.readkey())
    if char in [repr('\x1b[A'), repr('w'), repr('\x1b[D'), repr('a')]:
      if curr-1 >= 0:
        curr -= 1
    elif char in [repr('\x1b[B'), repr('s'), repr('\x1b[C'), repr('d')]:
      if curr+1 < len(opts):
        curr += 1
    elif char == repr('\r'):
      return opts[curr]
    if os.name == 'nt':
      os.system('cls')
    else: os.system('clear')

print(pysel("Select character...", ['Warrior', 'Mage', 'Elf']))