Velocity/modes/command.py

91 lines
2.7 KiB
Python
Raw Normal View History

2022-03-14 07:21:55 +00:00
from core import statusbar, utils
import curses
2022-03-17 21:47:19 +00:00
def execute(screen, data, commands):
2022-03-14 07:21:55 +00:00
if not commands:
# Quit if there are no commands, don't check anything
2022-03-17 21:47:19 +00:00
return data
2022-03-14 07:21:55 +00:00
for command in commands:
if command == "w":
# Write to the file
pass
2022-03-15 15:37:38 +00:00
2022-03-14 07:21:55 +00:00
elif command == "q":
# Goodbye prompt
2022-03-17 21:47:19 +00:00
utils.goodbye(screen, data)
2022-03-15 15:37:38 +00:00
elif command == "t":
2022-03-17 21:47:19 +00:00
# Theme switcher
2022-03-15 15:37:38 +00:00
if data["statusbar_theme"] == "filled":
data["statusbar_theme"] = "bare"
2022-03-14 07:21:55 +00:00
2022-03-15 15:37:38 +00:00
else:
data["statusbar_theme"] = "filled"
else:
2022-03-17 21:47:19 +00:00
utils.error(screen, data, f"Not an editor command: '{command}'")
2022-03-14 07:21:55 +00:00
2022-03-15 15:37:38 +00:00
return data
2022-03-17 21:47:19 +00:00
def activate(screen, data):
2022-03-14 07:21:55 +00:00
# Initialise variables
commands = []
# Visibly switch to command mode
2022-03-17 21:47:19 +00:00
statusbar.refresh(screen, data)
screen.addstr(data["height"]-1, 0, ":")
screen.move(data["height"]-1, 1)
2022-03-14 07:21:55 +00:00
# Main loop
while True:
# Get a key inputted by the user
2022-03-17 21:47:19 +00:00
key = screen.getch()
2022-03-14 07:21:55 +00:00
# Handle subtracting a key (backspace)
if key == curses.KEY_BACKSPACE:
# Write whitespace over characters to refresh it
2022-03-17 21:47:19 +00:00
screen.addstr(data["height"]-1, 1, " " * len(commands))
2022-03-14 07:21:55 +00:00
if commands:
# Subtract a character
commands.pop()
else:
2022-03-15 15:37:38 +00:00
# Exit command mode and enter normal mode if there is nothing left
data["mode"] = "normal"
2022-03-14 07:21:55 +00:00
return data
elif key == 27:
2022-03-15 15:37:38 +00:00
# Exit command mode and enter normal mode if "esc" is pressed
data["mode"] = "normal"
2022-03-14 07:21:55 +00:00
return data
2022-03-15 15:37:38 +00:00
elif key in (curses.KEY_ENTER, ord('\n'), ord('\r'), ord(":"), ord(";")):
2022-03-14 07:21:55 +00:00
# Execute commands
2022-03-17 21:47:19 +00:00
data = execute(screen, data, commands)
2022-03-14 07:21:55 +00:00
# Clear the bottom bar
2022-03-17 21:47:19 +00:00
screen.addstr(data["height"] - 1, 0, " " * (data["width"] - 1))
2022-03-14 07:21:55 +00:00
2022-03-15 15:37:38 +00:00
# Return to normal mode after executing a command
data["mode"] = "normal"
2022-03-14 07:21:55 +00:00
return data
else:
# If any other key is typed, append it
# As long as the key is in the valid list
valid = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ!"
2022-03-15 15:37:38 +00:00
if chr(key) in valid and len(commands) < (data["width"] - 2):
2022-03-14 07:21:55 +00:00
commands.append(chr(key))
# Join the commands together for visibility on the screen
friendly_command = "".join(commands)
2022-03-15 15:37:38 +00:00
2022-03-14 07:21:55 +00:00
# Write the commands to the screen
2022-03-17 21:47:19 +00:00
screen.addstr(data["height"]-1, 1, friendly_command)
2022-03-15 15:37:38 +00:00
2022-03-14 07:21:55 +00:00
# Move the cursor the end of the commands
2022-03-17 21:47:19 +00:00
screen.move(data["height"]-1, len(commands)+1)