{"id":208731,"date":"2020-11-21T21:49:57","date_gmt":"2020-11-21T13:49:57","guid":{"rendered":"http:\/\/4563.org\/?p=208731"},"modified":"2020-11-21T21:49:57","modified_gmt":"2020-11-21T13:49:57","slug":"%e5%88%86%e4%ba%ab%e4%b8%80%e4%b8%aa-python-%e5%91%bd%e4%bb%a4%e8%a1%8c%e4%b8%8b%e5%88%97%e8%a1%a8%e9%80%89%e6%8b%a9","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=208731","title":{"rendered":"\u5206\u4eab\u4e00\u4e2a Python \u547d\u4ee4\u884c\u4e0b\u5217\u8868\u9009\u62e9"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5206\u4eab\u4e00\u4e2a Python \u547d\u4ee4\u884c\u4e0b\u5217\u8868\u9009\u62e9               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lswang <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u4e0a\u4e0b\u952e + Enter \u6765\u9009\u62e9\uff0cCtrl+C \u53d6\u6d88<\/p>\n<blockquote>\n<p>[\u6ce8\u610f] \u53ea\u80fd\u652f\u6301 linux + macos<\/p>\n<\/blockquote>\n<p>\u8d85\u8fc7 20 \u4e2a\u6570\u636e\uff0c\u4f1a\u51fa\u73b0 <code>==== ^ ====<\/code> \u548c <code>==== v ====<\/code> \u63d0\u793a\u8fd8\u6709\u989d\u5916\u6570\u636e<\/p>\n<pre><code># -*- coding: utf-8 -*- import sys import termios import tty   left = u'u001b[1000D' right = u'u001b[1000C' clear_line = u'u001b[2K' up = u'u001b[1A' down = u'u001b[1B' clear_to_bottom = u'u001b[J'  MAX_SHOW_COUNT = 20  class CommandSingleSelector:     selectors = None     cur_index = 0     cursor_index = -1     header_word = None     is_cancel = False     top_index = 0     bottom_index = 0      def __init__(self, selectors, header_word):         self.selectors = selectors         if len(selectors) &gt; MAX_SHOW_COUNT:             self.bottom_index = MAX_SHOW_COUNT         else:             self.bottom_index = len(selectors) - 1          self.header_word = header_word      def up(self):         if self.cur_index &gt; 0:             self.cur_index = self.cur_index - 1             if self.cur_index &lt; self.top_index:                 self.top_index = self.top_index - 1                 self.bottom_index = self.bottom_index - 1             self.print_multi_line()      def down(self):         if self.cur_index &lt; len(self.selectors) - 1:             self.cur_index = self.cur_index + 1             if self.cur_index &gt; self.bottom_index:                 self.top_index = self.top_index + 1                 self.bottom_index = self.bottom_index + 1             self.print_multi_line()      def exit(self):         self.clear_multi_line()      def get_selector(self):         sys.stdout.write(self.header_word + 'n')         self.print_multi_line()         is_multi_first = False         is_multi_second = False          while True:             fd = sys.stdin.fileno()             old_settings = termios.tcgetattr(fd)             try:                 tty.setraw(sys.stdin.fileno())                 ch = sys.stdin.read(1)                 as_code = ord(ch)                  # find the multi key for up\/down                 if as_code == 27:                     is_multi_first = True                 elif as_code == 91:                     if is_multi_first:                         is_multi_second = True                 elif as_code == 65:                     if is_multi_first and is_multi_second:                         self.up()                     is_multi_second = False                     is_multi_first = False                 elif as_code == 66:                     if is_multi_first and is_multi_second:                         self.down()                     is_multi_second = False                     is_multi_first = False                 elif as_code == 13:                     is_multi_second = False                     is_multi_first = False                     self.exit()                     break                 elif as_code == 3:                     is_multi_second = False                     is_multi_first = False                     self.exit()                     self.is_cancel = True                     break                 else:                     is_multi_second = False                     is_multi_first = False             finally:                 termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)          if self.is_cancel:             raise Exception('Cancel this operation.')          return self.selectors[self.cur_index]      def print_multi_line(self):         sys.stdout.write((self.cursor_index + 1) * up + left)         sys.stdout.write(clear_to_bottom)          up_size = self.bottom_index - self.cur_index + 1 + 1 + 1 # first and last word          has_first_sign = '          '         if self.top_index &gt; 0:             has_first_sign = '   ==== ^ ===='          sys.stdout.write(left + clear_line + has_first_sign + 'n')           for index, item in enumerate(self.selectors):             if self.top_index &lt;= index &lt;= self.bottom_index:                 first_word = '   '                 if index == self.cur_index:                     first_word = ' &gt; '                     sys.stdout.write(left + clear_line + first_word + item + 'n')                 else:                     sys.stdout.write(left + clear_line + first_word + item + 'n')         has_last_sign = '          '         if len(self.selectors) - 1 &gt; self.bottom_index:             has_last_sign = '   ==== v ===='         sys.stdout.write(left + clear_line + has_last_sign + 'n')          back_to_cur_index = up * (up_size -1)         self.cursor_index = self.cur_index - self.top_index         sys.stdout.write(back_to_cur_index)         sys.stdout.write(left)      def clear_multi_line(self):         back_to_top = up * (self.cur_index - self.top_index + 2)         sys.stdout.write(back_to_top)         sys.stdout.write(left)         sys.stdout.write(clear_to_bottom)   if __name__ == '__main__':     selectors = ['HP Printer', 'XiaoMi Printer','DELL Printer']     command_selector = CommandSingleSelector(selectors=selectors, header_word='Please Select One Printer: ')     try:         sel = command_selector.get_selector()         print sel     except Exception, e:         print e.message  <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4340883\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lisniuse <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ef\u4ee5\u6765\u8fd9\u91cc\u5206\u4eab\uff1a https:\/\/www.kuxai.com\/forum                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4340884\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ClericPy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4ee5\u524d\u5012\u662f\u6298\u817e\u8fc7\u7c7b\u4f3c terminal UI \u7684, \u6709\u4e09\u4e2a\u5e93\u5370\u8c61\u6df1\u523b PyInquirer, console-menu, questionary<\/p>\n<p>\u8fd8\u597d\u6211\u53ea star \u6700\u597d\u7684, \u6240\u4ee5\u8fd8\u80fd\u627e\u5230                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4340885\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : renmu123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u6700\u8fd1\u518d\u7528\u4e00\u4e2a poetry \u51fa\u54c1\u7684 celo \u5de5\u5177\uff0c\u4e5f\u662f\u7c7b\u4f3c\u7684\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u76f8\u6bd4\u4e0a\u7684\u51e0\u4e2a\uff0c\u5f31\u5316\u4e86\u4ea4\u4e92\u5f0f\u7ec4\u4ef6\u7684\u90e8\u5206\uff0c\u589e\u5f3a\u4e86\u4f5c\u4e3a\u547d\u4ee4\u884c\u7684\u90e8\u5206\uff08\u5176\u5b9e\u5b8c\u5168\u53ef\u4ee5\u62fc\u4e00\u8d77\u9020\u4e2a\u878d\u5408\u602a\uff09                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4340886\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : frostming <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Paging \u561b\uff0crich \u6709\u73b0\u6210\u7684\u652f\u6301\uff0cprompt_toolkit \u4e5f\u6709                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5206\u4eab\u4e00\u4e2a Python \u547d\u4ee4\u884c\u4e0b\u5217&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/208731"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=208731"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/208731\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=208731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=208731"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=208731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}