Skip to content

Commit

Permalink
优化输入体验免回车
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Nov 15, 2019
1 parent 393bd0d commit 9439d18
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 28 deletions.
10 changes: 5 additions & 5 deletions v2ray_util/config_modify/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from ..util_core.v2ray import restart
from ..util_core.utils import readchar
from ..util_core.group import Vmess, Socks, Mtproto, SS
from ..util_core.writer import ClientWriter, GroupWriter
from ..util_core.selector import ClientSelector, GroupSelector
Expand Down Expand Up @@ -37,8 +38,7 @@ def dyn_port():
else:
print('{}: {}'.format(_("dyn_port status"), group.dyp))
gw = GroupWriter(group.tag, group.index)

choice = input(_("open/close dyn_port(y/n): ")).lower()
choice = readchar(_("open/close dyn_port(y/n): ")).lower()

if choice == 'y':
newAlterId = input(_("please input dyn_port alterID(default 32): "))
Expand Down Expand Up @@ -68,7 +68,7 @@ def new_email():
else:
client_index = cs.client_index
group_list = cs.group_list
print ("{}: {}".format(_("node email"), group.node_list[client_index].user_info))
print("{}: {}".format(_("node email"), group.node_list[client_index].user_info))
email = ""
while True:
is_duplicate_email=False
Expand Down Expand Up @@ -108,7 +108,7 @@ def new_uuid():
client_index = cs.client_index
if type(group.node_list[client_index]) == Vmess:
print("{}: {}".format(_("node UUID"), group.node_list[client_index].password))
choice = input(_("get new UUID?(y/n): ")).lower()
choice = readchar(_("get new UUID?(y/n): ")).lower()
if choice == "y":
import uuid
new_uuid = uuid.uuid1()
Expand Down Expand Up @@ -163,7 +163,7 @@ def tfo():
print(_("1.open TFO(force open)"))
print(_("2.close TFO(force close)"))
print(_("3.delete TFO(use system default profile)"))
choice = input(_("please select: "))
choice = readchar(_("please select: "))
if not choice:
return
if not choice in ("1", "2", "3"):
Expand Down
6 changes: 3 additions & 3 deletions v2ray_util/config_modify/multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ..util_core.writer import NodeWriter, GroupWriter
from ..util_core.group import Vmess, Socks, Mtproto, SS
from ..util_core.selector import GroupSelector, ClientSelector
from ..util_core.utils import StreamType, stream_list, is_email, clean_iptables, ColorStr
from ..util_core.utils import StreamType, stream_list, is_email, clean_iptables, ColorStr, readchar

@restart(True)
def new_port(new_stream=None):
Expand Down Expand Up @@ -121,7 +121,7 @@ def del_port():
else:
print(_("del group info: "))
print(group)
choice = input(_("delete?(y/n): ")).lower()
choice = readchar(_("delete?(y/n): ")).lower()
if choice == 'y':
nw = NodeWriter()
nw.del_port(group)
Expand All @@ -141,7 +141,7 @@ def del_user():
client_index = cs.client_index
print(_("del user info:"))
print(group.show_node(client_index))
choice = input(_("delete?(y/n): ")).lower()
choice = readchar(_("delete?(y/n): ")).lower()
if choice == 'y':
if len(group.node_list) == 1:
clean_iptables(group.port)
Expand Down
5 changes: 2 additions & 3 deletions v2ray_util/config_modify/ss.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
from ..util_core.v2ray import restart
from ..util_core.writer import GroupWriter
from ..util_core.selector import GroupSelector
from ..util_core.utils import ss_method, ColorStr
from ..util_core.utils import ss_method, ColorStr, readchar

class SSFactory:
def __init__(self):
self.method_tuple = ss_method()

def get_method(self):
print(_("please select shadowsocks method:"))
for index, method in enumerate(self.method_tuple):
print ("{}.{}".format(index + 1, method))
choice = input()
choice = readchar(_("please select shadowsocks method:"))
choice = int(choice)
if choice < 0 or choice > len(self.method_tuple):
print(_("input out of range!!"))
Expand Down
6 changes: 3 additions & 3 deletions v2ray_util/config_modify/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ..util_core.writer import GroupWriter
from ..util_core.group import Mtproto, SS
from ..util_core.selector import GroupSelector
from ..util_core.utils import get_ip, gen_cert
from ..util_core.utils import get_ip, gen_cert, readchar

class TLSModifier:
def __init__(self, group_tag, group_index, domain=''):
Expand All @@ -18,7 +18,7 @@ def turn_on(self):
print(_("1. Let's Encrypt certificate(auto create, please prepare domain)"))
print(_("2. Customize certificate(prepare certificate file paths)"))
print("")
choice=input(_("please select: "))
choice = readchar(_("please select: "))
input_domain = self.domain
if choice == "1":
if not input_domain:
Expand Down Expand Up @@ -80,7 +80,7 @@ def modify():
print("")
print(_("1.open TLS"))
print(_("2.close TLS"))
choice = input(_("please select: "))
choice = readchar(_("please select: "))
if not choice:
return
if not choice in ("1", "2"):
Expand Down
3 changes: 2 additions & 1 deletion v2ray_util/global_setting/ban_bt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ..util_core.loader import Loader
from ..util_core.v2ray import restart
from ..util_core.writer import GlobalWriter
from ..util_core.utils import readchar

@restart()
def manage():
Expand All @@ -12,7 +13,7 @@ def manage():

print("{}: {}".format(_("Ban BT status"), profile.ban_bt))

choice = input(_("Ban BT?(y/n): ")).lower()
choice = readchar(_("Ban BT?(y/n): ")).lower()

if not choice:
return
Expand Down
4 changes: 2 additions & 2 deletions v2ray_util/global_setting/iptables_ctr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pkg_resources

from ..util_core.loader import Loader
from ..util_core.utils import ColorStr, calcul_iptables_traffic
from ..util_core.utils import ColorStr, calcul_iptables_traffic, readchar

def manage():
if os.path.exists("/.dockerenv"):
Expand All @@ -27,7 +27,7 @@ def manage():
print(_("2.reset special port statistics"))
print("")

choice = input(_("please select: "))
choice = readchar(_("please select: "))
if choice == "1":
print("")
for group in group_list:
Expand Down
6 changes: 3 additions & 3 deletions v2ray_util/global_setting/stats_ctr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ..util_core.v2ray import V2ray
from ..util_core.loader import Loader
from ..util_core.writer import GlobalWriter
from ..util_core.utils import bytes_2_human_readable, ColorStr
from ..util_core.utils import bytes_2_human_readable, ColorStr, readchar

class StatsFactory:
def __init__(self, door_port):
Expand Down Expand Up @@ -81,7 +81,7 @@ def manage():
print(_("tip: restart v2ray will reset traffic statistics!!!"))
print("")

choice = input(_("please select: "))
choice = readchar(_("please select: "))

if choice in ("3", "4", "5") and not profile.stats.status:
print(_("only open traffic statistics to operate"))
Expand All @@ -90,7 +90,7 @@ def manage():

if choice == "1":
if os.popen(FIND_V2RAY_CRONTAB_CMD).readlines():
rchoice = input(_("open traffic statistics will close schedule update v2ray, continue?(y/n): "))
rchoice = readchar(_("open traffic statistics will close schedule update v2ray, continue?(y/n): "))
if rchoice == "y" or rchoice == "Y":
#关闭定时更新v2ray服务
os.system(DEL_UPDATE_TIMER_CMD)
Expand Down
4 changes: 2 additions & 2 deletions v2ray_util/global_setting/update_timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import platform

from ..util_core.config import Config
from ..util_core.utils import ColorStr
from ..util_core.utils import ColorStr, readchar

IS_CENTOS = True if "centos" in platform.linux_distribution()[0].lower() else False

Expand Down Expand Up @@ -45,7 +45,7 @@ def manage():
print("")
print(_("Tip: open schedule update v2ray at 3:00"))

choice = input(_("please select: "))
choice = readchar(_("please select: "))

if choice == "1":
if check_result:
Expand Down
1 change: 1 addition & 0 deletions v2ray_util/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ def menu():
from .util_core import client
client.generate()
else:
print("")
break

if __name__ == "__main__":
Expand Down
18 changes: 14 additions & 4 deletions v2ray_util/util_core/selector.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from .loader import Loader
from .utils import ColorStr
from .utils import ColorStr, readchar

class CommonSelector:
def __init__(self, collection, msg):
Expand All @@ -15,7 +15,11 @@ def __init__(self, collection, msg):
def select(self):
for index, element in enumerate(self.collection):
print("{0}.{1}".format(index + 1, element))
choice = input(self.msg)

if len(self.collection) < 10:
choice = readchar(self.msg)
else:
choice = input(self.msg)

if not choice.isnumeric():
raise RuntimeError(_('input error, please check is number'))
Expand Down Expand Up @@ -49,7 +53,10 @@ def __init__(self, action):
def select_client(self):
print(self.profile)
self.group = None
choice = input("{} {}: ".format(_("please input number to"), self.action))
if self.list_size < 10:
choice = readchar("{} {}: ".format(_("please input number to"), self.action))
else:
choice = input("{} {}: ".format(_("please input number to"), self.action))

if not choice.isnumeric():
print(ColorStr.red(_('input error, please check is number')))
Expand Down Expand Up @@ -83,7 +90,10 @@ def __init__(self, action):

def select_group(self):
print(self.profile)
choice = input("{} {}: ".format(_("please input group to"), self.action))
if len(self.group_list[-1].tag) == 1:
choice = readchar("{} {}: ".format(_("please input group to"), self.action))
else:
choice = input("{} {}: ".format(_("please input group to"), self.action))
group_list = [x for x in self.group_list if x.tag == str.upper(choice)]
if len(group_list) == 0:
print(ColorStr.red('{0} {1} {2}'.format(_("input error, please check group"), choice, _("exist"))))
Expand Down
25 changes: 23 additions & 2 deletions v2ray_util/util_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# -*- coding: utf-8 -*-
import os
import re
import sys
import tty
import termios
import pkg_resources
import urllib.request
from enum import Enum, unique
Expand Down Expand Up @@ -208,7 +211,10 @@ def loop_input_choice_number(input_tip, length):
"""
while True:
print("")
choice = input(input_tip)
if length >= 10:
choice = input(input_tip)
else:
choice = readchar(input_tip)
if not choice:
return
if choice.isnumeric():
Expand All @@ -219,4 +225,19 @@ def loop_input_choice_number(input_tip, length):
if (choice <= length and choice > 0):
return choice
else:
print(ColorStr.red(_("input error, please input again")))
print(ColorStr.red(_("input error, please input again")))

def readchar(prompt=""):
if prompt:
sys.stdout.write(prompt)
sys.stdout.flush()

fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)

return ch.strip()

0 comments on commit 9439d18

Please sign in to comment.