def test_clear(): console = Console(file=io.StringIO(), force_terminal=True) console.clear() console.clear(home=False) assert console.file.getvalue() == "\033[2J\033[H" + "\033[2J"
from discord import Client from rich.console import Console from rich.table import Table from rich.prompt import IntPrompt, Prompt, Confirm from time import sleep import sys console = Console() client = Client() console.clear() console.print( '[bold yellow]___________ __.__ _________ __ .__ v1.0 ' ) console.print( '[bold yellow]\_ _____/ _____ ____ |__|__|/ _____// |_ ____ _____ | | ' ) console.print( '[bold yellow] | __)_ / \ / _ \ | | |\_____ \\ __\/ __ \\__ \ | | ' ) console.print( '[bold yellow] | \ Y Y ( <_> ) | | |/ \| | \ ___/ / __ \| |__' ) console.print( '[bold yellow]/_______ /__|_| /\____/\__| |__/_______ /|__| \___ >____ /____/' ) console.print( '[bold yellow] \/ \/ \______| \/ \/ \/ ' ) console.print()
class Game: def __init__(self): self.started = False self.phase = TurnSequence.hero self.units: list[Unit] = [chainrasp_horde(game=self)] self.enemy_units: list[Unit] = [chainrasp_horde(game=self, x=76, y=15)] self.console = Console() self.current_roll = 0 self.messages = [] self.board = Board() def start(self): self.write( '[bold red]Welcome to [yellow]Warhammer: Age of Sigmar[/yellow].[/]' ) self.write('[bold red]This is a Work-In-Progress Project.[/]') self.units.append(chainrasp_horde(game=self)) self.fill_enemy_units() self.placement() self.shooting() self.command() self.charge() sleep(5) def display(self): self.console.clear() # Game statuses table = Table(show_header=True) table.add_column("Roll") table.add_column("Units") table.add_column("Record Log") table.add_row( int_to_die(self.current_roll).replace("●", "[red]●[/red]"), "\n".join( f'[yellow]{i}[/yellow]. [bold {unit.colour}]{unit.name} ({unit.symbol})[/]' for i, unit in enumerate(self.units, start=1)), "\n".join(self.messages[-5:])) self.console.print(table) # Battlefield table = Table(show_header=False, show_footer=False, box=box.ROUNDED) table.add_column(width=100) table.add_row(str(self.board)) self.console.print(table) def write(self, content: str): self.messages.append(content) self.display() sleep(1) def fill_enemy_units(self): for unit in self.enemy_units: self.board.board[unit.y][unit.x] = unit def prompt_command(self, unit: Unit): self.write(f'[bold red]Moving [yellow]{unit.name}[/yellow].[/]') self.write( '[bold red]Make your move ([blue]stationary[/blue]|[blue]normal[/blue]|[blue]run[/blue]).[/]' ) validating_move = True while validating_move: move = input('\u001b[32m> ') # the ANSI escape code makes the input green as well. print('\u001b[0m', end='') # At the moment, rich cannot do this. move.lower() try: mt = MoveType(move) except ValueError: self.write( '[bold italic magenta]That was not a valid move.[/]') else: old_x, old_y = unit.x, unit.y unit.make_move(move_type=mt) self.board.board[old_y][old_x] = None self.board.board[unit.y][unit.x] = unit self.display() validating_move = False def placement(self): self.write('[bold red]Preparing to place units.[/]') for unit in self.units: self.place_unit(unit) def place_unit(self, unit: Unit): self.write( f'[bold red]Place your [yellow]{unit.name}[/yellow] (Press ESC to finish).[/]' ) x, y = 0, 0 self.board.board[y][x] = unit self.display() def on_press(key): try: # Alphanumeric character. key.char except AttributeError: nonlocal x, y self.board.board[y][x] = None if key == keyboard.Key.right: x += 1 if x > 25: x = 0 elif key == keyboard.Key.left: x -= 1 if x < 0: x = 25 elif key == keyboard.Key.up: y -= 1 if y < 0: y = 24 elif key == keyboard.Key.down: y += 1 if y > 24: y = 0 self.board.board[y][x] = unit self.display() def on_release(key): if key == keyboard.Key.esc: return False # Collect events until released with keyboard.Listener(on_press=on_press, on_release=on_release) as listener: listener.join() unit.x, unit.y = x, y def charge(self): self.write('[green]Entering Charge Phase.[/green]') self.phase = TurnSequence.charge can_charge = False for unit in self.units: for enemy_unit in self.enemy_units: if enemy_unit.x - unit.x <= 12 or unit.x - enemy_unit.x <= 12: can_charge = True unit.charge(enemy_unit) if not can_charge: self.write('[bold red]No units are eligible to charge.[/]') self.write('[bold red]Skipping...[/]') def shooting(self): self.write('[green]Entering Shooting Phase.[/green]') self.phase = TurnSequence.shooting have_missile_weapon = False for unit in self.units: for weapon in unit.weapons: if weapon.type == WeaponType.missile: have_missile_weapon = True # currently, none of the units have a missile weapon. if not have_missile_weapon: self.write( '[bold red]No units are equipped with a missile weapon.[/]') self.write('[bold red]Skipping...[/]') def command(self): self.write('[green]Entering Command Phase.[/green]') self.phase = TurnSequence.movement for unit in self.units: self.prompt_command(unit) def roll(self): self.write(f'[bold red]Rolling...[/]') value = roll() self.current_roll = value self.display() self.write(f'[bold red]Rolled a [yellow]{value}[/yellow].[/]') self.reset_roll() return value def reset_roll(self): self.current_roll = 0 self.display() return
def prompt(): MARKDOWN = """ ## Random Password Generator # Siga estes passos para criar sua senha: * Escolha a quantidade de caracteres. * Escolha se aceita caracteres especiais. * A senha será gerada e copiada automaticamente para sua área de transferência. """ console = Console() md = Markdown(MARKDOWN) console.print(md) qnt = int( console.input("\n\n[blue]Quantidade de caracteres :point_right: ")) esp = str( console.input( "[blue]Aceita caracteres especiais? (sim/nao) :point_right: ")) if qnt > 50: console.print( "\n[red underline]:x: A senha não pode ter mais de 50 caracteres. :x:" ) time.sleep(2) console.clear() prompt() elif esp == "sim": possibilities = "12345!@" + string.ascii_letters + "#*67890" password = pwd_creation(qnt, possibilities) pyperclip.copy(password) toaster.show_toast( "Random Password Generator", "Sua senha foi copiada para a área de transferência!", duration=3) console.print( "[yellow]\nSua senha aparecerá e sumirá em 4 segundos após aparecer." ) time.sleep(2) with alive_bar(4) as bar: for i in range(4): time.sleep(1) bar() console.print(f"[red]\n:point_right: {password} :point_left:") time.sleep(4) console.clear() prompt() elif esp == "nao": possibilities = "67890" + string.ascii_letters + "12345" password = pwd_creation(qnt, possibilities) pyperclip.copy(password) toaster.show_toast( "Random Password Generator", "Sua senha foi copiada para a área de transferência!", duration=3) console.print( "[yellow]\nSua senha aparecerá e sumirá em 4 segundos após aparecer." ) time.sleep(2) with alive_bar(4) as bar: for i in range(4): time.sleep(1) bar() console.print(f"[red]\n:point_right: {password} :point_left:") time.sleep(4) console.clear() prompt() else: console.print( "\n[red underline]:x: A resposta deve sem apenas 'sim' ou 'nao'. :x:" ) time.sleep(2) console.clear() prompt()
def pretty_print_syntax(syntax: Syntax) -> None: console = Console() class _RevealingSyntax: def __init__(self, result: RenderResult): self.result = [i for i in result] self.progress = 0 self.length = sum( len(i.text) for i in self.result if not i.text.isspace()) def __rich_console__(self, a, b): self.progress += 1 current_partial_segment = None current_char_index = 0 # A typo ~ 1 in 20 characters seems about right to me typo = random.random() < 0.05 i: Segment for index, i in enumerate(self.result): if i.text.isspace(): continue if current_char_index + len(i.text) < self.progress: current_char_index += len(i.text) elif current_char_index + len(i.text) == self.progress: if typo: yield from self.result[:index + 1] + [ Segment( random.choice(string.printable), self.result[index].style, False, ) ] else: yield from self.result[:index + 1] return else: partial_length = self.progress - current_char_index if typo: current_partial_segment = Segment( i.text[:partial_length] + random.choice(string.printable), i.style, i.is_control, ) else: current_partial_segment = Segment( i.text[:partial_length], i.style, i.is_control) yield from self.result[:index] + [current_partial_segment] return return def __len__(self): return self.length code = _RevealingSyntax(syntax.__rich_console__(console, console.options)) for _ in range(len(code)): console.clear() console.print(code) sleep(CHAR_RATE)