Compare commits
9 commits
a65f7ceb39
...
0dd29c63ae
Author | SHA1 | Date | |
---|---|---|---|
dece | 0dd29c63ae | ||
dece | f6eeccdb32 | ||
dece | e2cc152f6d | ||
dece | ca72c4683e | ||
dece | 0a746a1e28 | ||
dece | 1ae2ed41e5 | ||
dece | 5829e7e267 | ||
dece | d07d4b3388 | ||
dece | 4726a5a281 |
|
@ -1,11 +1,9 @@
|
|||
TODO
|
||||
----------------------------------------
|
||||
bug: can't input unicode
|
||||
bug: xterm is borked
|
||||
dumb rendering mode per site
|
||||
well, preferences per site maybe?
|
||||
does encoding really work? cf. egsam
|
||||
get/set config using command-line
|
||||
add metadata to status bar
|
||||
more UT
|
||||
setup.py
|
||||
|
||||
|
@ -14,6 +12,7 @@ setup.py
|
|||
BACKLOG
|
||||
--------------------------------------------------------------------------------
|
||||
click on links to open them
|
||||
get/set config using command-line
|
||||
download without memory buffer
|
||||
download in the background
|
||||
download view instead of last download
|
||||
|
|
|
@ -217,6 +217,7 @@ class Browser:
|
|||
elif char == curses.ascii.ESC: # Can be ESC or ALT char.
|
||||
self.screen.nodelay(True)
|
||||
char = self.screen.getch()
|
||||
self.screen.nodelay(False)
|
||||
if char == -1:
|
||||
self.reset_status()
|
||||
else: # ALT keybinds.
|
||||
|
@ -228,7 +229,6 @@ class Browser:
|
|||
self.scroll_page_vertically(-1)
|
||||
elif char == ord("l"):
|
||||
self.scroll_page_horizontally(1)
|
||||
self.screen.nodelay(False)
|
||||
# elif char == ord("@"):
|
||||
# self.current_url = "bebop:debugzone"
|
||||
# t = "\n".join("* " + u for u in self.history.urls)
|
||||
|
@ -303,6 +303,7 @@ class Browser:
|
|||
num_words = len(words)
|
||||
if num_words == 0:
|
||||
return
|
||||
|
||||
command = words[0]
|
||||
if num_words == 1:
|
||||
if command in ("q", "quit"):
|
||||
|
|
|
@ -17,8 +17,8 @@ class ColorPair(IntEnum):
|
|||
BLOCKQUOTE = 8
|
||||
|
||||
# Colors for other usage in the browser.
|
||||
LINK_PREVIEW = 9
|
||||
DEBUG = 99
|
||||
DEBUG = 9
|
||||
LINK_PREVIEW = 10
|
||||
|
||||
|
||||
def init_colors():
|
||||
|
|
|
@ -2,13 +2,14 @@
|
|||
|
||||
import curses
|
||||
import curses.ascii
|
||||
import curses.textpad
|
||||
import os
|
||||
import logging
|
||||
import tempfile
|
||||
from typing import Optional
|
||||
|
||||
from bebop.external import open_external_program
|
||||
from bebop.links import Links
|
||||
from bebop.textbox import Textbox
|
||||
|
||||
|
||||
class CommandLine:
|
||||
|
@ -32,7 +33,7 @@ class CommandLine:
|
|||
def __init__(self, window, editor_command):
|
||||
self.window = window
|
||||
self.editor_command = editor_command
|
||||
self.textbox = curses.textpad.Textbox(self.window)
|
||||
self.textbox = Textbox(self.window, insert_mode=True)
|
||||
|
||||
def clear(self):
|
||||
"""Clear command-line contents."""
|
||||
|
@ -102,18 +103,19 @@ class CommandLine:
|
|||
to handle the keys above.
|
||||
"""
|
||||
if ch == curses.KEY_BACKSPACE: # Cancel input if all line is cleaned.
|
||||
text = self.gather()
|
||||
if len(text) == 0:
|
||||
_, x = self.textbox.win.getyx()
|
||||
if x == 1:
|
||||
raise EscapeCommandInterrupt()
|
||||
pass
|
||||
elif ch == curses.ascii.ESC: # Could be ESC or ALT
|
||||
self.window.nodelay(True)
|
||||
ch = self.window.getch()
|
||||
self.window.nodelay(False)
|
||||
if ch == -1:
|
||||
raise EscapeCommandInterrupt()
|
||||
else: # ALT keybinds.
|
||||
if ch == ord("e"):
|
||||
self.open_editor(self.gather())
|
||||
self.window.nodelay(False)
|
||||
return ch
|
||||
|
||||
def focus_for_link_navigation(self, init_char: int, links: Links):
|
||||
|
@ -194,6 +196,7 @@ class CommandLine:
|
|||
temp_file.write(existing_content)
|
||||
temp_filepath = temp_file.name
|
||||
except OSError:
|
||||
logging.error("Could not open or write to temporary file.")
|
||||
return
|
||||
|
||||
command = self.editor_command + [temp_filepath]
|
||||
|
@ -204,6 +207,7 @@ class CommandLine:
|
|||
content = temp_file.read().rstrip("\r\n")
|
||||
os.unlink(temp_filepath)
|
||||
except OSError:
|
||||
logging.error("Could not read temporary file after user edition.")
|
||||
return
|
||||
raise TerminateCommandInterrupt(content)
|
||||
|
||||
|
|
|
@ -77,7 +77,11 @@ Your current configuration is:
|
|||
|
||||
def get_help(config):
|
||||
config_list = "\n".join(
|
||||
f'* {key} = {value} (default "{DEFAULT_CONFIG[key]}")'
|
||||
(
|
||||
f'* {key} = {value} (default {repr(DEFAULT_CONFIG[key])})'
|
||||
if value != DEFAULT_CONFIG[key]
|
||||
else f'* {key} = {value}'
|
||||
)
|
||||
for key, value in config.items()
|
||||
)
|
||||
return HELP_PAGE + config_list
|
||||
|
|
166
bebop/textbox.py
Normal file
166
bebop/textbox.py
Normal file
|
@ -0,0 +1,166 @@
|
|||
"""Fork of Python's standard library curses.textpad module.
|
||||
|
||||
I guess it requires some license header?
|
||||
|
||||
Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
|
||||
2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021
|
||||
Python Software Foundation;
|
||||
All Rights Reserved
|
||||
|
||||
This version fixes a few quirks of the standard module, namely:
|
||||
|
||||
- Discard multi-lines mode: only one line is supported.
|
||||
- Moving in line more reasonably: no more going alone rightward.
|
||||
- Handle Unicode
|
||||
"""
|
||||
|
||||
import curses
|
||||
import curses.ascii
|
||||
|
||||
|
||||
class Textbox:
|
||||
"""Editing widget using the interior of a window object.
|
||||
|
||||
- Ctrl-A: Go to left edge of window.
|
||||
- Ctrl-B: Cursor left, wrapping to previous line if appropriate.
|
||||
- Ctrl-D: Delete character under cursor.
|
||||
- Ctrl-E: Go to right edge (stripspaces off) or end of line (stripspaces on).
|
||||
- Ctrl-F: Cursor right, wrapping to next line when appropriate.
|
||||
- Ctrl-H: Delete character backward.
|
||||
- Ctrl-K: If line is blank, delete it, otherwise clear to end of line.
|
||||
- Ctrl-L: Refresh screen.
|
||||
|
||||
Move operations do nothing if the cursor is at an edge where the movement
|
||||
is not possible. The following synonyms are supported where possible:
|
||||
|
||||
- KEY_LEFT: Ctrl-B
|
||||
- KEY_RIGHT: Ctrl-F
|
||||
- KEY_UP: Ctrl-P
|
||||
- KEY_DOWN: Ctrl-N
|
||||
- KEY_BACKSPACE: Ctrl-H
|
||||
"""
|
||||
def __init__(self, win, insert_mode=False):
|
||||
self.win = win
|
||||
self.insert_mode = insert_mode
|
||||
self._update_maxx()
|
||||
self.stripspaces = True
|
||||
win.keypad(1)
|
||||
|
||||
def _update_maxx(self):
|
||||
_, maxx = self.win.getmaxyx()
|
||||
self.maxx = maxx - 1
|
||||
|
||||
def _end_of_line(self):
|
||||
"""Return the index of the last non-blank character."""
|
||||
self._update_maxx()
|
||||
last = self.maxx
|
||||
while True:
|
||||
last_ch = curses.ascii.ascii(self.win.inch(0, last))
|
||||
if last_ch != curses.ascii.SP:
|
||||
last = min(self.maxx, last + 1)
|
||||
break
|
||||
elif last == 0:
|
||||
break
|
||||
last = last - 1
|
||||
return last
|
||||
|
||||
def _insert_printable_char(self, ch):
|
||||
self._update_maxx()
|
||||
_, x = self.win.getyx()
|
||||
backx = None
|
||||
while x < self.maxx:
|
||||
oldch = 0
|
||||
if self.insert_mode:
|
||||
oldch = self.win.inch()
|
||||
# The try-catch ignores the error we trigger from some curses
|
||||
# versions by trying to write into the lowest-rightmost spot
|
||||
# in the window.
|
||||
try:
|
||||
self.win.addch(ch)
|
||||
except curses.error:
|
||||
pass
|
||||
if not self.insert_mode or not curses.ascii.isprint(oldch):
|
||||
break
|
||||
ch = oldch
|
||||
_, x = self.win.getyx()
|
||||
# Remember where to put the cursor back since we are in insert_mode.
|
||||
if backx is None:
|
||||
backx = x
|
||||
|
||||
if backx is not None:
|
||||
self.win.move(0, backx)
|
||||
|
||||
COMMAND_KEYS = (curses.KEY_LEFT, curses.KEY_RIGHT, curses.KEY_BACKSPACE)
|
||||
|
||||
def do_command(self, ch):
|
||||
"""Process a single editing command."""
|
||||
self._update_maxx()
|
||||
_, x = self.win.getyx()
|
||||
if curses.ascii.iscntrl(ch) or ch in self.COMMAND_KEYS:
|
||||
return self.do_control(ch, x)
|
||||
if x < self.maxx:
|
||||
curses.ungetch(ch)
|
||||
ch = self.win.get_wch()
|
||||
self._insert_printable_char(ch)
|
||||
return 1
|
||||
|
||||
def do_control(self, ch, x):
|
||||
"""Process a control character."""
|
||||
if ch == curses.ascii.NL:
|
||||
return 0
|
||||
elif ch == curses.ascii.SOH: # ^a
|
||||
self.win.move(0, 0)
|
||||
elif ch in (
|
||||
curses.ascii.STX,
|
||||
curses.KEY_LEFT,
|
||||
curses.ascii.BS,
|
||||
curses.KEY_BACKSPACE
|
||||
):
|
||||
if x > 0:
|
||||
self.win.move(0, x - 1)
|
||||
if ch in (curses.ascii.BS, curses.KEY_BACKSPACE):
|
||||
self.win.delch()
|
||||
elif ch == curses.ascii.EOT: # ^d
|
||||
self.win.delch()
|
||||
elif ch == curses.ascii.ENQ: # ^e
|
||||
if self.stripspaces:
|
||||
self.win.move(0, self._end_of_line())
|
||||
else:
|
||||
self.win.move(0, self.maxx)
|
||||
elif ch in (curses.ascii.ACK, curses.KEY_RIGHT): # ^f
|
||||
if x < self.maxx:
|
||||
self.win.move(0, min(x + 1, self._end_of_line()))
|
||||
elif ch == curses.ascii.VT: # ^k
|
||||
if x == 0 and self._end_of_line() == 0:
|
||||
self.win.deleteln()
|
||||
else:
|
||||
# First undo the effect of self._end_of_line.
|
||||
self.win.move(0, x)
|
||||
self.win.clrtoeol()
|
||||
elif ch == curses.ascii.FF: # ^l
|
||||
self.win.refresh()
|
||||
return 1
|
||||
|
||||
def gather(self):
|
||||
"""Collect and return the contents of the window."""
|
||||
result = ""
|
||||
self._update_maxx()
|
||||
stop = self._end_of_line()
|
||||
for x in range(self.maxx + 1):
|
||||
if self.stripspaces and x > stop:
|
||||
break
|
||||
result += chr(self.win.inch(0, x))
|
||||
return result
|
||||
|
||||
def edit(self, validate=None):
|
||||
"""Edit in the widget window and collect the results."""
|
||||
while True:
|
||||
ch = self.win.getch()
|
||||
if validate:
|
||||
ch = validate(ch)
|
||||
if not ch:
|
||||
continue
|
||||
if not self.do_command(ch):
|
||||
break
|
||||
self.win.refresh()
|
||||
return self.gather()
|
Reference in a new issue