mirror of
https://github.com/fish-shell/fish-shell.git
synced 2024-11-27 03:13:37 +08:00
Merge branch 'webserver' of git://github.com/simukis/fish-shell into simukis-webserver
Conflicts: share/tools/web_config/webconfig.py
This commit is contained in:
commit
3606664cf7
|
@ -7,10 +7,11 @@ IS_PY2 = sys.version_info[0] == 2
|
|||
if IS_PY2:
|
||||
import SimpleHTTPServer
|
||||
import SocketServer
|
||||
import urlparse
|
||||
from urlparse import parse_qs
|
||||
else:
|
||||
import http.server as SimpleHTTPServer
|
||||
import socketserver as SocketServer
|
||||
from urllib.parse import parse_qs
|
||||
import webbrowser
|
||||
import subprocess
|
||||
import re, json, socket, os, sys, cgi, select, time
|
||||
|
@ -18,18 +19,18 @@ import re, json, socket, os, sys, cgi, select, time
|
|||
def run_fish_cmd(text):
|
||||
from subprocess import PIPE
|
||||
p = subprocess.Popen(["fish"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
||||
if not IS_PY2:
|
||||
text = text.encode('utf-8')
|
||||
out, err = p.communicate(text)
|
||||
if not IS_PY2:
|
||||
if IS_PY2:
|
||||
out, err = p.communicate(text)
|
||||
else:
|
||||
out, err = p.communicate(bytes(text, 'utf-8'))
|
||||
out = str(out, 'utf-8')
|
||||
err = str(err, 'utf-8')
|
||||
return(out, err)
|
||||
|
||||
def escape_fish_cmd(text):
|
||||
# Replace one backslash with two, and single quotes with backslash-quote
|
||||
escaped = text.replace('\\', '\\\\').replace("'", "\\'")
|
||||
return "'" + escaped + "'"
|
||||
# Replace one backslash with two, and single quotes with backslash-quote
|
||||
escaped = text.replace('\\', '\\\\').replace("'", "\\'")
|
||||
return "'" + escaped + "'"
|
||||
|
||||
named_colors = {
|
||||
'black' : '000000',
|
||||
|
@ -57,14 +58,14 @@ def parse_one_color(comp):
|
|||
return ''
|
||||
|
||||
def better_color(c1, c2):
|
||||
""" Indicate which color is "better", i.e. prefer term256 colors """
|
||||
if not c2: return c1
|
||||
if not c1: return c2
|
||||
if c1 == 'normal': return c2
|
||||
if c2 == 'normal': return c1
|
||||
if c2 in named_colors: return c1
|
||||
if c1 in named_colors: return c2
|
||||
return c1
|
||||
""" Indicate which color is "better", i.e. prefer term256 colors """
|
||||
if not c2: return c1
|
||||
if not c1: return c2
|
||||
if c1 == 'normal': return c2
|
||||
if c2 == 'normal': return c1
|
||||
if c2 in named_colors: return c1
|
||||
if c1 in named_colors: return c2
|
||||
return c1
|
||||
|
||||
|
||||
def parse_color(color_str):
|
||||
|
@ -115,9 +116,9 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
|||
|
||||
def write_to_wfile(self, txt):
|
||||
if IS_PY2:
|
||||
self.wfile.write(txt)
|
||||
self.wfile.write(txt)
|
||||
else: # Python 3
|
||||
self.wfile.write(bytes(txt, 'utf-8'))
|
||||
self.wfile.write(bytes(txt, 'utf-8'))
|
||||
|
||||
|
||||
def do_get_colors(self):
|
||||
|
@ -158,7 +159,7 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
|||
'operator': 'Like * and ~',
|
||||
'escape': 'Escapes like \\n',
|
||||
'cwd': 'Current directory',
|
||||
'cwd_root': 'cwd for root user',
|
||||
'cwd_root': 'cwd for root user',
|
||||
'valid_path': 'Valid paths',
|
||||
'autosuggestion': 'Suggested completion'
|
||||
}
|
||||
|
@ -249,9 +250,9 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
|||
return out
|
||||
|
||||
def do_delete_history_item(self, history_item_text):
|
||||
# It's really lame that we always return success here
|
||||
out, err = run_fish_cmd('builtin history --save --delete -- ' + escape_fish_cmd(history_item_text))
|
||||
return True
|
||||
# It's really lame that we always return success here
|
||||
out, err = run_fish_cmd('builtin history --save --delete -- ' + escape_fish_cmd(history_item_text))
|
||||
return True
|
||||
|
||||
def do_GET(self):
|
||||
p = self.path
|
||||
|
@ -286,6 +287,7 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
|||
ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
|
||||
else: # Python 3
|
||||
ctype, pdict = cgi.parse_header(self.headers['content-type'])
|
||||
|
||||
if ctype == 'multipart/form-data':
|
||||
postvars = cgi.parse_multipart(self.rfile, pdict)
|
||||
elif ctype == 'application/x-www-form-urlencoded':
|
||||
|
@ -293,7 +295,6 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
|||
length = int(self.headers.getheader('content-length'))
|
||||
except AttributeError:
|
||||
length = int(self.headers['content-length'])
|
||||
|
||||
# parse_qs borks if we give it a Unicode string in Python2.
|
||||
url_str = self.rfile.read(length).decode('utf-8')
|
||||
if IS_PY2: url_str = str(url_str)
|
||||
|
@ -308,18 +309,6 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
|||
bold = postvars.get('bold')
|
||||
underline = postvars.get('underline')
|
||||
|
||||
if what == None: #Will be None for python3
|
||||
what = postvars.get(b'what')
|
||||
what[0] = str(what[0]).lstrip("b'").rstrip("'")
|
||||
color = postvars.get(b'color')
|
||||
color[0] = str(color[0]).lstrip("b'").rstrip("'")
|
||||
background_color = postvars.get(b'background_color')
|
||||
background_color[0] = str(background_color[0]).lstrip("b'").rstrip("'")
|
||||
bold = postvars.get(b'bold')
|
||||
bold[0] = str(bold[0]).lstrip("b'").rstrip("'")
|
||||
underline = postvars.get(b'underline')
|
||||
underline[0] = str(underline[0]).lstrip("b'").rstrip("'")
|
||||
|
||||
if what:
|
||||
# Not sure why we get lists here?
|
||||
output = self.do_set_color_for_variable(what[0], color[0], background_color[0], parse_bool(bold[0]), parse_bool(underline[0]))
|
||||
|
@ -327,20 +316,13 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
|||
output = 'Bad request'
|
||||
elif p == '/get_function/':
|
||||
what = postvars.get('what')
|
||||
if what == None: #Will be None for python3
|
||||
what = postvars.get(b'what')
|
||||
what[0] = str(what[0]).lstrip("b'").rstrip("'")
|
||||
output = [self.do_get_function(what[0])]
|
||||
elif p == '/delete_history_item/':
|
||||
what = postvars.get('what')
|
||||
if what == None: #Will be None for python3
|
||||
what = postvars.get(b'what')
|
||||
what[0] = str(what[0]).lstrip("b'").rstrip("'")
|
||||
what = what[0] # It's a list!
|
||||
if self.do_delete_history_item(what):
|
||||
output = ["OK"]
|
||||
if self.do_delete_history_item(what[0]):
|
||||
output = ["OK"]
|
||||
else:
|
||||
output = ["Unable to delete history item"]
|
||||
output = ["Unable to delete history item"]
|
||||
else:
|
||||
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_POST(self)
|
||||
|
||||
|
@ -385,10 +367,10 @@ if PORT > 9000:
|
|||
# Just look at the first letter
|
||||
initial_tab = ''
|
||||
if len(sys.argv) > 1:
|
||||
for tab in ['functions', 'colors', 'variables', 'history']:
|
||||
if tab.startswith(sys.argv[1]):
|
||||
initial_tab = '#' + tab
|
||||
break
|
||||
for tab in ['functions', 'colors', 'variables', 'history']:
|
||||
if tab.startswith(sys.argv[1]):
|
||||
initial_tab = '#' + tab
|
||||
break
|
||||
|
||||
url = 'http://localhost:{0}/{1}'.format(PORT, initial_tab)
|
||||
print("Web config started at '{0}'. Hit enter to stop.".format(url))
|
||||
|
|
Loading…
Reference in New Issue
Block a user