Merge pull request #3497 from chiizujin/editor_justify

Add width argument to editor's :j and :f commands
This commit is contained in:
Griatch 2024-06-14 09:37:34 +02:00 committed by GitHub
commit 4ce456b46f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -67,7 +67,7 @@ _DEFAULT_WIDTH = settings.CLIENT_DEFAULT_WIDTH
# ------------------------------------------------------------- # -------------------------------------------------------------
_HELP_TEXT = _( _HELP_TEXT = _(
""" f"""
<txt> - any non-command is appended to the end of the buffer. <txt> - any non-command is appended to the end of the buffer.
: <l> - view buffer or only line(s) <l> : <l> - view buffer or only line(s) <l>
:: <l> - raw-view buffer or only line(s) <l> :: <l> - raw-view buffer or only line(s) <l>
@ -97,8 +97,11 @@ _HELP_TEXT = _(
:s <l> <w> <txt> - search/replace word or regex <w> in buffer or on line <l> :s <l> <w> <txt> - search/replace word or regex <w> in buffer or on line <l>
:j <l> <w> - justify buffer or line <l>. <w> is f, c, l or r. Default f (full) :j <l> <a> = <w> - justify buffer or line <l>. <a> is f, c, l or r. <w> is
:f <l> - flood-fill entire buffer or line <l>. Equivalent to :j <l> l width. <a> and <w> are optional and default to l (left)
and {_DEFAULT_WIDTH} respectively
:f <l> = <w> - flood-fill entire buffer or line <l> to width <w>.
Equivalent to :j <l> l. <w> is optional, as for :j
:fi <l> - indent entire buffer or line <l> :fi <l> - indent entire buffer or line <l>
:fd <l> - de-indent entire buffer or line <l> :fd <l> - de-indent entire buffer or line <l>
@ -686,7 +689,14 @@ class CmdEditorGroup(CmdEditorBase):
editor.update_buffer(buf) editor.update_buffer(buf)
elif cmd == ":f": elif cmd == ":f":
# :f <l> flood-fill buffer or <l> lines of buffer. # :f <l> flood-fill buffer or <l> lines of buffer.
# :f <l> =<w> flood-fill buffer or <l> lines of buffer to width <w>.
width = _DEFAULT_WIDTH width = _DEFAULT_WIDTH
if self.arg1:
value = self.arg1.lstrip("=")
if not value.isdigit():
self.caller.msg("Width must be a number.")
return
width = int(value)
if not self.linerange: if not self.linerange:
lstart = 0 lstart = 0
lend = self.cline + 1 lend = self.cline + 1
@ -698,7 +708,7 @@ class CmdEditorGroup(CmdEditorBase):
buf = linebuffer[:lstart] + fbuf.split("\n") + linebuffer[lend:] buf = linebuffer[:lstart] + fbuf.split("\n") + linebuffer[lend:]
editor.update_buffer(buf) editor.update_buffer(buf)
elif cmd == ":j": elif cmd == ":j":
# :f <l> <w> justify buffer of <l> with <w> as align (one of # :j <l> <a> =<w> justify buffer of <l> to width <w> with <a> as align (one of
# f(ull), c(enter), r(ight) or l(left). Default is full. # f(ull), c(enter), r(ight) or l(left). Default is full.
align_map = { align_map = {
"full": "f", "full": "f",
@ -711,7 +721,10 @@ class CmdEditorGroup(CmdEditorBase):
"l": "l", "l": "l",
} }
align_name = {"f": "Full", "c": "Center", "l": "Left", "r": "Right"} align_name = {"f": "Full", "c": "Center", "l": "Left", "r": "Right"}
width = _DEFAULT_WIDTH # shift width arg right if no alignment specified
if self.arg1.startswith('='):
self.arg2 = self.arg1
self.arg1 = None
if self.arg1 and self.arg1.lower() not in align_map: if self.arg1 and self.arg1.lower() not in align_map:
self.caller.msg( self.caller.msg(
_("Valid justifications are") _("Valid justifications are")
@ -719,6 +732,13 @@ class CmdEditorGroup(CmdEditorBase):
) )
return return
align = align_map[self.arg1.lower()] if self.arg1 else "f" align = align_map[self.arg1.lower()] if self.arg1 else "f"
width = _DEFAULT_WIDTH
if self.arg2:
value = self.arg2.lstrip("=")
if not value.isdigit():
self.caller.msg("Width must be a number.")
return
width = int(value)
if not self.linerange: if not self.linerange:
lstart = 0 lstart = 0
lend = self.cline + 1 lend = self.cline + 1