Parcourir la source

fix自动滚动

wangweimin il y a 5 ans
Parent
commit
4cda03ea0e
2 fichiers modifiés avec 52 ajouts et 3 suppressions
  1. 2 3
      wsrepl/html/index.html
  2. 50 0
      wsrepl/output.py

+ 2 - 3
wsrepl/html/index.html

@@ -104,11 +104,10 @@
     };
     var viewer = $('.viewer');
     ws.onmessage = function (evt) {
-        viewer.scrollTop(md_body[0].scrollHeight);  // 自动滚动
-
-        var msg = JSON.parse(evt.data);
         console.log('>>>', msg);
+        var msg = JSON.parse(evt.data);
         ctrl.handle_message(msg);
+        viewer.scrollTop(md_body[0].scrollHeight);  // 自动滚动
     };
     ws.onclose = function () {
         document.title = 'Closed';

+ 50 - 0
wsrepl/output.py

@@ -0,0 +1,50 @@
+import json
+import logging
+from collections.abc import Mapping
+
+from .framework import Global
+from .input_ctrl import send_msg, single_input, input_control
+
+
+def set_title(title):
+    send_msg('output_ctl', dict(title=title))
+
+
+def text_print(text, *, ws=None):
+    msg = dict(command="output", spec=dict(content=text, type='text'))
+    (ws or Global.active_ws).write_message(json.dumps(msg))
+
+
+def json_print(obj):
+    text = "```\n%s\n```" % json.dumps(obj, indent=4, ensure_ascii=False)
+    text_print(text)
+
+
+put_markdown = text_print
+
+
+def put_table(tdata):
+    """
+    |      \|      |      |      |
+    | ---- | ---- | ---- | ---- |
+    |      |      |      |      |
+    |      |      |      |      |
+    |      |      |      |      |
+    :param tdata:
+    :return:
+    """
+
+    def quote(data):
+        return data.replace('|', r'\|')
+
+    header = "|%s|" % "|".join(map(quote, tdata[0]))
+    res = [header]
+    res.append("|%s|" % "|".join(['----'] * len(tdata[0])))
+    for tr in tdata[1:]:
+        t = "|%s|" % "|".join(map(quote, tr))
+        res.append(t)
+    text_print('\n'.join(res))
+
+
+def buttons(buttons, onclick=None):
+    pass