Compare commits

...

9 commits

6 changed files with 186 additions and 12 deletions

View file

@ -1,11 +1,9 @@
TODO TODO
---------------------------------------- ----------------------------------------
bug: can't input unicode
bug: xterm is borked
dumb rendering mode per site dumb rendering mode per site
well, preferences per site maybe? well, preferences per site maybe?
does encoding really work? cf. egsam does encoding really work? cf. egsam
get/set config using command-line add metadata to status bar
more UT more UT
setup.py setup.py
@ -14,6 +12,7 @@ setup.py
BACKLOG BACKLOG
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
click on links to open them click on links to open them
get/set config using command-line
download without memory buffer download without memory buffer
download in the background download in the background
download view instead of last download download view instead of last download

View file

@ -217,6 +217,7 @@ class Browser:
elif char == curses.ascii.ESC: # Can be ESC or ALT char. elif char == curses.ascii.ESC: # Can be ESC or ALT char.
self.screen.nodelay(True) self.screen.nodelay(True)
char = self.screen.getch() char = self.screen.getch()
self.screen.nodelay(False)
if char == -1: if char == -1:
self.reset_status() self.reset_status()
else: # ALT keybinds. else: # ALT keybinds.
@ -228,7 +229,6 @@ class Browser:
self.scroll_page_vertically(-1) self.scroll_page_vertically(-1)
elif char == ord("l"): elif char == ord("l"):
self.scroll_page_horizontally(1) self.scroll_page_horizontally(1)
self.screen.nodelay(False)
# elif char == ord("@"): # elif char == ord("@"):
# self.current_url = "bebop:debugzone" # self.current_url = "bebop:debugzone"
# t = "\n".join("* " + u for u in self.history.urls) # t = "\n".join("* " + u for u in self.history.urls)
@ -303,6 +303,7 @@ class Browser:
num_words = len(words) num_words = len(words)
if num_words == 0: if num_words == 0:
return return
command = words[0] command = words[0]
if num_words == 1: if num_words == 1:
if command in ("q", "quit"): if command in ("q", "quit"):

View file

@ -17,8 +17,8 @@ class ColorPair(IntEnum):
BLOCKQUOTE = 8 BLOCKQUOTE = 8
# Colors for other usage in the browser. # Colors for other usage in the browser.
LINK_PREVIEW = 9 DEBUG = 9
DEBUG = 99 LINK_PREVIEW = 10
def init_colors(): def init_colors():

View file

@ -2,13 +2,14 @@
import curses import curses
import curses.ascii import curses.ascii
import curses.textpad
import os import os
import logging
import tempfile import tempfile
from typing import Optional from typing import Optional
from bebop.external import open_external_program from bebop.external import open_external_program
from bebop.links import Links from bebop.links import Links
from bebop.textbox import Textbox
class CommandLine: class CommandLine:
@ -32,7 +33,7 @@ class CommandLine:
def __init__(self, window, editor_command): def __init__(self, window, editor_command):
self.window = window self.window = window
self.editor_command = editor_command self.editor_command = editor_command
self.textbox = curses.textpad.Textbox(self.window) self.textbox = Textbox(self.window, insert_mode=True)
def clear(self): def clear(self):
"""Clear command-line contents.""" """Clear command-line contents."""
@ -102,18 +103,19 @@ class CommandLine:
to handle the keys above. to handle the keys above.
""" """
if ch == curses.KEY_BACKSPACE: # Cancel input if all line is cleaned. if ch == curses.KEY_BACKSPACE: # Cancel input if all line is cleaned.
text = self.gather() _, x = self.textbox.win.getyx()
if len(text) == 0: if x == 1:
raise EscapeCommandInterrupt() raise EscapeCommandInterrupt()
pass
elif ch == curses.ascii.ESC: # Could be ESC or ALT elif ch == curses.ascii.ESC: # Could be ESC or ALT
self.window.nodelay(True) self.window.nodelay(True)
ch = self.window.getch() ch = self.window.getch()
self.window.nodelay(False)
if ch == -1: if ch == -1:
raise EscapeCommandInterrupt() raise EscapeCommandInterrupt()
else: # ALT keybinds. else: # ALT keybinds.
if ch == ord("e"): if ch == ord("e"):
self.open_editor(self.gather()) self.open_editor(self.gather())
self.window.nodelay(False)
return ch return ch
def focus_for_link_navigation(self, init_char: int, links: Links): def focus_for_link_navigation(self, init_char: int, links: Links):
@ -194,6 +196,7 @@ class CommandLine:
temp_file.write(existing_content) temp_file.write(existing_content)
temp_filepath = temp_file.name temp_filepath = temp_file.name
except OSError: except OSError:
logging.error("Could not open or write to temporary file.")
return return
command = self.editor_command + [temp_filepath] command = self.editor_command + [temp_filepath]
@ -204,6 +207,7 @@ class CommandLine:
content = temp_file.read().rstrip("\r\n") content = temp_file.read().rstrip("\r\n")
os.unlink(temp_filepath) os.unlink(temp_filepath)
except OSError: except OSError:
logging.error("Could not read temporary file after user edition.")
return return
raise TerminateCommandInterrupt(content) raise TerminateCommandInterrupt(content)

View file

@ -77,7 +77,11 @@ Your current configuration is:
def get_help(config): def get_help(config):
config_list = "\n".join( 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() for key, value in config.items()
) )
return HELP_PAGE + config_list return HELP_PAGE + config_list

166
bebop/textbox.py Normal file
View 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()