Skip to content

Commit

Permalink
Reimplementing the input field
Browse files Browse the repository at this point in the history
This addresses the issue #116
  • Loading branch information
anufrievroman committed Feb 10, 2025
1 parent a8d212c commit 399fb0e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
43 changes: 42 additions & 1 deletion calcure/dialogues.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,55 @@ def clear_line(stdscr, y, x=0):
stdscr.addstr(y, x, " " * (x_max - x - 1), curses.color_pair(Color.EMPTY.value))


def input_field(stdscr, y, x, field_length):
"""Input field that gets characters entered by user"""
curses.curs_set(1)
input_str = ""
cursor_pos = 0

while True:
stdscr.move(y, x + cursor_pos)
key = stdscr.getch()

# Esc, Return, and Backspace keys:
if key == 27:
return ""
if key in (curses.KEY_ENTER, 10, 13):
return input_str
if key in (curses.KEY_BACKSPACE, 127):
if cursor_pos > 0:
input_str = input_str[:cursor_pos-1] + input_str[cursor_pos:]
cursor_pos -= 1

# Left and Right arrows:
elif key == curses.KEY_LEFT:
if cursor_pos > 0:
cursor_pos -= 1
elif key == curses.KEY_RIGHT:
if cursor_pos < len(input_str):
cursor_pos += 1

# Regular ASCII characters:
elif 32 <= key <= 126 and cursor_pos < field_length:
input_str = input_str[:cursor_pos] + chr(key) + input_str[cursor_pos:]
cursor_pos += 1
else:
pass

# Redraw input field:
stdscr.addstr(y, x, input_str + " ")
stdscr.refresh()


def input_string(stdscr, y, x, question, answer_length):
"""Ask user to input something and return it as a string"""
curses.echo()
curses.curs_set(True)
display_question(stdscr, y, x, question, Color.PROMPTS)
stdscr.refresh()
try:
answer = stdscr.getstr(y, len(question) + x, answer_length).decode(encoding="utf-8")
# answer = stdscr.getstr(y, len(question) + x, answer_length).decode(encoding="utf-8")
answer = input_field(stdscr, y, len(question) + x, answer_length)
except (UnicodeDecodeError, KeyboardInterrupt):
answer = ""
logging.warning("Incorrect characters in user input.")
Expand Down
56 changes: 56 additions & 0 deletions calcure/input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import curses

def input_field(stdscr, prompt="Enter text: "):
curses.curs_set(1) # Show cursor
stdscr.clear()
stdscr.addstr(0, 0, prompt)
stdscr.refresh()

input_str = ""
cursor_pos = 0
start_x = len(prompt) # Fixed starting position of input

while True:
stdscr.move(0, start_x + cursor_pos) # Move cursor
key = stdscr.getch()

if key == 27: # Escape key
return None
elif key in (curses.KEY_ENTER, 10, 13): # Enter key
return input_str
elif key in (curses.KEY_BACKSPACE, 127): # Backspace key
if cursor_pos > 0:
input_str = input_str[:cursor_pos-1] + input_str[cursor_pos:]
cursor_pos -= 1
elif key == curses.KEY_LEFT: # Move cursor left
if cursor_pos > 0:
cursor_pos -= 1
elif key == curses.KEY_RIGHT: # Move cursor right
if cursor_pos < len(input_str):
cursor_pos += 1
elif 32 <= key <= 126: # Printable ASCII characters
input_str = input_str[:cursor_pos] + chr(key) + input_str[cursor_pos:]
cursor_pos += 1

# Redraw input field
stdscr.addstr(0, start_x, input_str + " ") # Extra space clears deleted characters
stdscr.refresh()

def main(stdscr):
stdscr.clear()
stdscr.addstr("Press Enter after typing, or Esc to exit.\n")
stdscr.refresh()

user_input = input_field(stdscr)

stdscr.clear()
if user_input is None:
stdscr.addstr("Input cancelled.\n")
else:
stdscr.addstr(f"You entered: {user_input}\n")

stdscr.addstr("Press any key to exit.")
stdscr.getch()

curses.wrapper(main)

1 change: 0 additions & 1 deletion calcure/moon.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Module that controls display of the moon phases"""

from datetime import datetime
from astral import moon


# Dictionary of moon phases taken from:
Expand Down

0 comments on commit 399fb0e

Please sign in to comment.