Skip to content

Commit

Permalink
add display commands on enter
Browse files Browse the repository at this point in the history
  • Loading branch information
al3xbro committed Jun 14, 2024
1 parent a4e2bfd commit a5fa2e0
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TerminallyOnline(App):
}

def on_mount(self):
self.push_screen('channel_view')
self.push_screen('command_view')


if __name__ == '__main__':
Expand Down
55 changes: 52 additions & 3 deletions src/views/commandview/command_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,65 @@
from textual.widgets import Header, Footer
from views.commandview.input import CommandInput
from views.commandview.display import Display
import sys
import threading

class CommandView(Screen):

display = Display()
path = []

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def __init__(self, *command, **kwcommand):
super().__init__(*command, **kwcommand)

def parse_command(self, value):
'''Possible commands: cd, ls, quit/exit/, logout, clear, help.
Possible errors: invalid command, too many arguments, too few arguments
'''

command = value.split(' ')

match command[0]:
case 'cd':
self.command_cd(command)
case 'ls':
self.command_ls(command)
case 'quit', 'exit':
self.command_quit(command)
case 'logout':
self.command_logout(command)
case 'clear':
self.command_clear(command)
case 'help':
self.command_help(command)
case _:
self.command_not_found(command)

def command_cd(self, command):
self.display.add_command('/' + '/'.join(self.path), ' '.join(command))

def command_ls(self, command):
self.display.add_command('/' + '/'.join(self.path), ' '.join(command))

def command_quit(self, command):
self.display.add_command('/' + '/'.join(self.path), ' '.join(command))
threading.exit()
sys.exit()

def command_logout(self, command):
self.display.add_command('/' + '/'.join(self.path), ' '.join(command))

def command_clear(self, command):
self.display.add_command('/' + '/'.join(self.path), ' '.join(command))

def command_help(self, command):
self.display.add_command('/' + '/'.join(self.path), ' '.join(command))

def command_not_found(self, command):
self.display.add_command('/' + '/'.join(self.path), ' '.join(command))

def compose(self):
yield Header()
yield Display()
yield self.display
yield CommandInput()
yield Footer()
43 changes: 42 additions & 1 deletion src/views/commandview/display.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,49 @@
from textual.widgets import Label
from rich.text import Text
from models.user import User
from textual.containers import VerticalScroll
from models.guilds import Guilds

class Display(VerticalScroll):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def add_prompt(self, path: str):
renderable = Text()
renderable.append(User.get_username() + '@TerminallyOnline', style='bold green')
renderable.append(':', style='bold')
renderable.append(path, style='bold blue')
renderable.append('$ ', style='bold')
self.mount(Label(renderable))
self.scroll_end()

def remove_prompt(self):
self.remove_child(self.children[0])

def add_command(self, path: str, text: str):
renderable = Text()
renderable.append(User.get_username() + '@TerminallyOnline', style='bold green')
renderable.append(':', style='bold')
renderable.append(path, style='bold blue')
renderable.append('$ ', style='bold')
renderable.append(text)
self.mount(Label(renderable))
self.scroll_end()

def add_info(self, renderable):
self.mount(Label(renderable))
self.scroll_end()

def add_err(self, text: str):
renderable = Text()
renderable.append('TerminallyOnline: ')
renderable.append(text)
self.scroll_end()

def clear(self):
self.remove_children()

def on_mount(self):
self.add_prompt('/')
self.scroll_end()

62 changes: 3 additions & 59 deletions src/views/commandview/input.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from textual.widgets import Input
from models.guilds import Guilds

class CommandInput(Input):

Expand All @@ -9,61 +8,6 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.placeholder = 'Enter your command here'

def parse_command(self):
'''Possible commands: cd, ls, quit/exit/, logout, clear, help.
Possible errors: invalid command, too many arguments, too few arguments
'''

command = self.value.split(' ')
self.value = ''

match command[0]:
case 'cd':
if len(command) > 2:
print('cd: too many arguments')
elif len(command) == 2:
self.command_cd(command[1])
else:
print('print help here')
case 'ls':
if len(command) > 2:
print('ls: too many arguments')
elif len(command) == 2:
self.command_ls(command[1])
else:
self.command_ls(self.current_path)
case 'quit', 'exit':
if len(command) > 1:
print('quit: too many arguments')
else:
self.command_quit()
case 'logout':
if len(command) > 1:
print('logout: too many arguments')
else:
self.command_logout()
case 'clear':
if len(command) > 1:
print('clear: too many arguments')
else:
self.command_clear()
case 'help':
print('help')

def command_cd(self, path):
print('cd', path)

def command_ls(self, path):
print('ls', path)

def command_quit(self):
print('quit')

def command_logout(self):
print('logout')

def command_clear(self):
print('clear')

def command_help(self):
print('help')
def on_input_submitted(self):
self.parent.parse_command(self.value)
self.value = ''

0 comments on commit a5fa2e0

Please sign in to comment.