Velocity/modes/command.py

90 lines
2.6 KiB
Python
Raw Normal View History

2022-03-14 07:21:55 +00:00
from core import statusbar, utils
import curses
2022-03-15 15:37:38 +00:00
def execute(stdscr, data, commands):
2022-03-14 07:21:55 +00:00
if not commands:
# Quit if there are no commands, don't check anything
return
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-15 15:37:38 +00:00
utils.goodbye(stdscr, data)
elif command == "t":
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:
utils.error(stdscr, 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
def activate(stdscr, data):
2022-03-14 07:21:55 +00:00
# Initialise variables
commands = []
# Visibly switch to command mode
2022-03-15 15:37:38 +00:00
statusbar.refresh(stdscr, data)
stdscr.addstr(data["height"]-1, 0, ":")
stdscr.move(data["height"]-1, 1)
2022-03-14 07:21:55 +00:00
# Main loop
while True:
# Get a key inputted by the user
key = stdscr.getch()
# Handle subtracting a key (backspace)
if key == curses.KEY_BACKSPACE:
# Write whitespace over characters to refresh it
2022-03-15 15:37:38 +00:00
stdscr.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-15 15:37:38 +00:00
data = execute(stdscr, data, commands)
2022-03-14 07:21:55 +00:00
# Clear the bottom bar
2022-03-15 15:37:38 +00:00
stdscr.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-15 15:37:38 +00:00
stdscr.addstr(data["height"]-1, 1, friendly_command)
2022-03-14 07:21:55 +00:00
# Move the cursor the end of the commands
2022-03-15 15:37:38 +00:00
stdscr.move(data["height"]-1, len(commands)+1)