Patchwork D11638: chistedit: move layout() and dependencies onto state class

login
register
mail settings
Submitter phabricator
Date Oct. 12, 2021, 5:07 p.m.
Message ID <differential-rev-PHID-DREV-l7ptdxhns3kwkl7nqkuh-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/49962/
State Superseded
Headers show

Comments

phabricator - Oct. 12, 2021, 5:07 p.m.
martinvonz created this revision.
Herald added a reviewer: durin42.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D11638

AFFECTED FILES
  hgext/histedit.py

CHANGE DETAILS




To: martinvonz, durin42, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/hgext/histedit.py b/hgext/histedit.py
--- a/hgext/histedit.py
+++ b/hgext/histedit.py
@@ -1408,9 +1408,11 @@ 
         self,
         repo,
         rules,
+        stdscr,
     ):
         self.repo = repo
         self.rules = rules
+        self.stdscr = stdscr
         self.pos = 0
         self.selected = None
         self.mode = (MODE_INIT, MODE_INIT)
@@ -1475,6 +1477,46 @@ 
         win.addstr(y, 1, conflictstr[:length])
         win.noutrefresh()
 
+    def helplines(self):
+        if self.mode[0] == MODE_PATCH:
+            help = b"""\
+?: help, k/up: line up, j/down: line down, v: stop viewing patch
+pgup: prev page, space/pgdn: next page, c: commit, q: abort
+"""
+        else:
+            help = b"""\
+?: help, k/up: move up, j/down: move down, space: select, v: view patch
+d: drop, e: edit, f: fold, m: mess, p: pick, r: roll
+pgup/K: move patch up, pgdn/J: move patch down, c: commit, q: abort
+"""
+        return help.splitlines()
+
+    def render_help(self, win):
+        maxy, maxx = win.getmaxyx()
+        for y, line in enumerate(self.helplines()):
+            if y >= maxy:
+                break
+            addln(win, y, 0, line, curses.color_pair(COLOR_HELP))
+        win.noutrefresh()
+
+    def layout(self):
+        maxy, maxx = self.stdscr.getmaxyx()
+        helplen = len(self.helplines())
+        mainlen = maxy - helplen - 12
+        if mainlen < 1:
+            raise error.Abort(
+                _(b"terminal dimensions %d by %d too small for curses histedit")
+                % (maxy, maxx),
+                hint=_(
+                    b"enlarge your terminal or use --config ui.interface=text"
+                ),
+            )
+        return {
+            b'commit': (12, maxx),
+            b'help': (helplen, maxx),
+            b'main': (mainlen, maxx),
+        }
+
 
 def _chisteditmain(repo, rules, stdscr):
     try:
@@ -1503,29 +1545,6 @@ 
     except curses.error:
         pass
 
-    def helplines(mode):
-        if mode == MODE_PATCH:
-            help = b"""\
-?: help, k/up: line up, j/down: line down, v: stop viewing patch
-pgup: prev page, space/pgdn: next page, c: commit, q: abort
-"""
-        else:
-            help = b"""\
-?: help, k/up: move up, j/down: move down, space: select, v: view patch
-d: drop, e: edit, f: fold, m: mess, p: pick, r: roll
-pgup/K: move patch up, pgdn/J: move patch down, c: commit, q: abort
-"""
-        return help.splitlines()
-
-    def renderhelp(win, state):
-        maxy, maxx = win.getmaxyx()
-        mode, _ = state.mode
-        for y, line in enumerate(helplines(mode)):
-            if y >= maxy:
-                break
-            addln(win, y, 0, line, curses.color_pair(COLOR_HELP))
-        win.noutrefresh()
-
     def renderrules(rulesscr, state):
         rules = state.rules
         pos = state.pos
@@ -1600,30 +1619,12 @@ 
         content = state.modes[MODE_PATCH][b'patchcontents']
         renderstring(win, state, content[start:], diffcolors=True)
 
-    def layout(mode):
-        maxy, maxx = stdscr.getmaxyx()
-        helplen = len(helplines(mode))
-        mainlen = maxy - helplen - 12
-        if mainlen < 1:
-            raise error.Abort(
-                _(b"terminal dimensions %d by %d too small for curses histedit")
-                % (maxy, maxx),
-                hint=_(
-                    b"enlarge your terminal or use --config ui.interface=text"
-                ),
-            )
-        return {
-            b'commit': (12, maxx),
-            b'help': (helplen, maxx),
-            b'main': (mainlen, maxx),
-        }
-
     def drawvertwin(size, y, x):
         win = curses.newwin(size[0], size[1], y, x)
         y += size[0]
         return win, y, x
 
-    state = _chistedit_state(repo, rules)
+    state = _chistedit_state(repo, rules, stdscr)
 
     # eventloop
     ch = None
@@ -1645,8 +1646,8 @@ 
                 if size != stdscr.getmaxyx():
                     curses.resizeterm(*size)
 
+            sizes = state.layout()
             curmode, unused = state.mode
-            sizes = layout(curmode)
             if curmode != oldmode:
                 state.page_height = sizes[b'main'][0]
                 # Adjust the view to fit the current screen size.
@@ -1680,7 +1681,7 @@ 
             else:
                 renderrules(mainwin, state)
                 state.render_commit(commitwin)
-            renderhelp(helpwin, state)
+            state.render_help(helpwin)
             curses.doupdate()
             # done rendering
             ch = encoding.strtolocal(stdscr.getkey())