2014-10-10 08:51:43 +08:00
|
|
|
from __future__ import unicode_literals
|
2016-12-05 07:44:57 +08:00
|
|
|
from __future__ import print_function
|
2016-11-11 08:23:08 +08:00
|
|
|
import binascii
|
|
|
|
import cgi
|
2019-05-05 18:09:25 +08:00
|
|
|
|
2018-12-01 02:44:20 +08:00
|
|
|
try:
|
|
|
|
from html import escape as escape_html
|
|
|
|
except ImportError:
|
|
|
|
from cgi import escape as escape_html
|
2017-05-28 14:46:25 +08:00
|
|
|
from distutils.version import LooseVersion
|
2016-11-11 08:23:08 +08:00
|
|
|
import glob
|
2014-07-30 03:12:32 +08:00
|
|
|
import multiprocessing.pool
|
2014-07-23 11:50:24 +08:00
|
|
|
import operator
|
2016-11-11 08:23:08 +08:00
|
|
|
import os
|
2017-05-28 14:46:25 +08:00
|
|
|
import platform
|
2016-11-11 08:23:08 +08:00
|
|
|
import random
|
|
|
|
import re
|
|
|
|
import select
|
2015-05-18 10:13:50 +08:00
|
|
|
import socket
|
2016-11-11 08:23:08 +08:00
|
|
|
import string
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2019-01-22 05:59:36 +08:00
|
|
|
from itertools import chain
|
2016-11-11 08:23:08 +08:00
|
|
|
|
|
|
|
FISH_BIN_PATH = False # will be set later
|
2012-07-27 15:31:00 +08:00
|
|
|
IS_PY2 = sys.version_info[0] == 2
|
|
|
|
|
|
|
|
if IS_PY2:
|
2012-06-05 22:25:11 +08:00
|
|
|
import SimpleHTTPServer
|
|
|
|
import SocketServer
|
2015-01-30 22:52:07 +08:00
|
|
|
from urlparse import parse_qs
|
2012-07-27 15:31:00 +08:00
|
|
|
else:
|
|
|
|
import http.server as SimpleHTTPServer
|
2012-06-05 22:25:11 +08:00
|
|
|
import socketserver as SocketServer
|
2012-08-20 04:26:15 +08:00
|
|
|
from urllib.parse import parse_qs
|
2013-11-20 03:42:05 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
|
2017-05-28 14:46:25 +08:00
|
|
|
def isMacOS10_12_5_OrLater():
|
|
|
|
""" Return whether this system is macOS 10.12.5 or a later version. """
|
|
|
|
version = platform.mac_ver()[0]
|
2019-05-05 18:09:25 +08:00
|
|
|
return version and LooseVersion(version) >= LooseVersion("10.12.5")
|
|
|
|
|
2017-05-28 14:46:25 +08:00
|
|
|
|
2018-04-03 07:36:11 +08:00
|
|
|
def is_wsl():
|
|
|
|
""" Return whether we are running under the Windows Subsystem for Linux """
|
2019-05-05 18:09:25 +08:00
|
|
|
if "linux" in platform.system().lower():
|
|
|
|
with open("/proc/version", "r") as f:
|
|
|
|
if "Microsoft" in f.read():
|
2018-04-03 07:36:11 +08:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2017-05-28 14:46:25 +08:00
|
|
|
|
2013-11-20 03:42:05 +08:00
|
|
|
# Disable CLI web browsers
|
2019-05-05 18:09:25 +08:00
|
|
|
term = os.environ.pop("TERM", None)
|
2018-04-02 21:35:10 +08:00
|
|
|
# This import must be done with an empty $TERM, otherwise a command-line browser may be started
|
|
|
|
# which will block the whole process - see https://docs.python.org/3/library/webbrowser.html
|
|
|
|
import webbrowser
|
2019-05-05 18:09:25 +08:00
|
|
|
|
2013-11-20 03:42:05 +08:00
|
|
|
if term:
|
2019-05-05 18:09:25 +08:00
|
|
|
os.environ["TERM"] = term
|
2013-11-20 03:42:05 +08:00
|
|
|
|
2012-12-12 19:51:55 +08:00
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
2012-03-15 18:43:45 +08:00
|
|
|
|
2016-11-11 08:23:08 +08:00
|
|
|
|
2012-03-15 18:43:45 +08:00
|
|
|
def run_fish_cmd(text):
|
2016-11-11 08:23:08 +08:00
|
|
|
# Ensure that fish is using UTF-8.
|
2019-05-05 18:09:25 +08:00
|
|
|
ctype = os.environ.get("LC_ALL", os.environ.get("LC_CTYPE", os.environ.get("LANG")))
|
2014-10-10 11:21:26 +08:00
|
|
|
env = None
|
2014-12-06 04:39:21 +08:00
|
|
|
if ctype is None or re.search(r"\.utf-?8$", ctype, flags=re.I) is None:
|
2014-10-10 11:21:26 +08:00
|
|
|
# override LC_CTYPE with en_US.UTF-8
|
|
|
|
# We're assuming this locale exists.
|
|
|
|
# Fish makes the same assumption in config.fish
|
|
|
|
env = os.environ.copy()
|
|
|
|
env.update(LC_CTYPE="en_US.UTF-8", LANG="en_US.UTF-8")
|
2019-05-01 05:08:11 +08:00
|
|
|
|
|
|
|
print("$ " + text)
|
2019-05-05 18:09:25 +08:00
|
|
|
p = subprocess.Popen(
|
|
|
|
[FISH_BIN_PATH],
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
env=env,
|
|
|
|
)
|
|
|
|
out, err = p.communicate(text.encode("utf-8"))
|
|
|
|
out = out.decode("utf-8", "replace")
|
|
|
|
err = err.decode("utf-8", "replace")
|
2016-12-05 07:44:57 +08:00
|
|
|
return out, err
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2016-11-11 08:23:08 +08:00
|
|
|
|
2012-07-27 15:31:00 +08:00
|
|
|
def escape_fish_cmd(text):
|
2012-08-20 03:55:50 +08:00
|
|
|
# Replace one backslash with two, and single quotes with backslash-quote
|
2019-05-05 18:09:25 +08:00
|
|
|
escaped = text.replace("\\", "\\\\").replace("'", "\\'")
|
2012-08-20 03:55:50 +08:00
|
|
|
return "'" + escaped + "'"
|
2012-03-15 18:43:45 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
|
2012-03-23 01:23:07 +08:00
|
|
|
named_colors = {
|
2019-05-05 18:09:25 +08:00
|
|
|
"black": "000000",
|
|
|
|
"red": "800000",
|
|
|
|
"green": "008000",
|
|
|
|
"brown": "725000",
|
|
|
|
"yellow": "808000",
|
|
|
|
"blue": "000080",
|
|
|
|
"magenta": "800080",
|
|
|
|
"purple": "800080",
|
|
|
|
"cyan": "008080",
|
|
|
|
"grey": "e5e5e5",
|
|
|
|
"brgrey": "555555",
|
|
|
|
"white": "c0c0c0",
|
|
|
|
"brblack": "808080",
|
|
|
|
"brred": "ff0000",
|
|
|
|
"brgreen": "00ff00",
|
|
|
|
"brbrown": "ffff00",
|
|
|
|
"bryellow": "ffff00",
|
|
|
|
"brblue": "0000ff",
|
|
|
|
"brmagenta": "ff00ff",
|
|
|
|
"brpurple": "ff00ff",
|
|
|
|
"brcyan": "00ffff",
|
|
|
|
"brwhite": "ffffff",
|
2012-03-23 01:23:07 +08:00
|
|
|
}
|
|
|
|
|
2016-08-23 17:34:06 +08:00
|
|
|
bindings_blacklist = set(["self-insert", "'begin;end'"])
|
|
|
|
|
2016-11-11 08:23:08 +08:00
|
|
|
|
2012-03-25 18:00:38 +08:00
|
|
|
def parse_one_color(comp):
|
2012-05-09 08:10:38 +08:00
|
|
|
""" A basic function to parse a single color value like 'FFA000' """
|
|
|
|
if comp in named_colors:
|
|
|
|
# Named color
|
|
|
|
return named_colors[comp]
|
2019-05-05 18:09:25 +08:00
|
|
|
elif (
|
|
|
|
re.match(r"[0-9a-fA-F]{3}", comp) is not None
|
|
|
|
or re.match(r"[0-9a-fA-F]{6}", comp) is not None
|
|
|
|
):
|
2012-05-09 08:10:38 +08:00
|
|
|
# Hex color
|
|
|
|
return comp
|
|
|
|
else:
|
|
|
|
# Unknown
|
2019-05-05 18:09:25 +08:00
|
|
|
return ""
|
2012-03-25 18:00:38 +08:00
|
|
|
|
2016-11-11 08:23:08 +08:00
|
|
|
|
2012-06-05 16:19:59 +08:00
|
|
|
def better_color(c1, c2):
|
2012-08-20 03:55:50 +08:00
|
|
|
""" Indicate which color is "better", i.e. prefer term256 colors """
|
2016-11-11 08:23:08 +08:00
|
|
|
if not c2:
|
|
|
|
return c1
|
|
|
|
if not c1:
|
|
|
|
return c2
|
2019-05-05 18:09:25 +08:00
|
|
|
if c1 == "normal":
|
2016-11-11 08:23:08 +08:00
|
|
|
return c2
|
2019-05-05 18:09:25 +08:00
|
|
|
if c2 == "normal":
|
2016-11-11 08:23:08 +08:00
|
|
|
return c1
|
|
|
|
if c2 in named_colors:
|
|
|
|
return c1
|
|
|
|
if c1 in named_colors:
|
|
|
|
return c2
|
2012-08-20 03:55:50 +08:00
|
|
|
return c1
|
|
|
|
|
2016-11-11 08:23:08 +08:00
|
|
|
|
2012-03-23 01:23:07 +08:00
|
|
|
def parse_color(color_str):
|
2016-11-11 08:23:08 +08:00
|
|
|
""" A basic function to parse a color string, for example, 'red' '--bold'.
|
|
|
|
"""
|
2019-05-05 18:09:25 +08:00
|
|
|
comps = color_str.split(" ")
|
|
|
|
color = "normal"
|
|
|
|
background_color = ""
|
2018-12-05 17:05:33 +08:00
|
|
|
bold, underline, italics, dim, reverse = False, False, False, False, False
|
2012-05-09 08:10:38 +08:00
|
|
|
for comp in comps:
|
|
|
|
# Remove quotes
|
|
|
|
comp = comp.strip("'\" ")
|
2019-05-05 18:09:25 +08:00
|
|
|
if comp == "--bold":
|
2012-05-09 08:10:38 +08:00
|
|
|
bold = True
|
2019-05-05 18:09:25 +08:00
|
|
|
elif comp == "--underline":
|
2012-05-09 08:10:38 +08:00
|
|
|
underline = True
|
2019-05-05 18:09:25 +08:00
|
|
|
elif comp == "--italics":
|
2018-12-05 17:05:33 +08:00
|
|
|
italics = True
|
2019-05-05 18:09:25 +08:00
|
|
|
elif comp == "--dim":
|
2018-12-05 17:05:33 +08:00
|
|
|
dim = True
|
2019-05-05 18:09:25 +08:00
|
|
|
elif comp == "--reverse":
|
2018-12-05 17:05:33 +08:00
|
|
|
reverse = True
|
2019-05-05 18:09:25 +08:00
|
|
|
elif comp.startswith("--background="):
|
2012-05-09 08:10:38 +08:00
|
|
|
# Background color
|
2016-11-11 08:23:08 +08:00
|
|
|
background_color = better_color(
|
2019-05-05 18:09:25 +08:00
|
|
|
background_color, parse_one_color(comp[len("--background=") :])
|
|
|
|
)
|
2012-05-09 08:10:38 +08:00
|
|
|
else:
|
|
|
|
# Regular color
|
2012-06-05 16:19:59 +08:00
|
|
|
color = better_color(color, parse_one_color(comp))
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
return {
|
|
|
|
"color": color,
|
|
|
|
"background": background_color,
|
|
|
|
"bold": bold,
|
|
|
|
"underline": underline,
|
|
|
|
"italics": italics,
|
|
|
|
"dim": dim,
|
|
|
|
"reverse": reverse,
|
|
|
|
}
|
2016-11-11 08:23:08 +08:00
|
|
|
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-03-25 18:00:38 +08:00
|
|
|
def parse_bool(val):
|
2012-05-09 08:10:38 +08:00
|
|
|
val = val.lower()
|
2019-05-05 18:09:25 +08:00
|
|
|
if val.startswith("f") or val.startswith("0"):
|
2016-11-11 08:23:08 +08:00
|
|
|
return False
|
2019-05-05 18:09:25 +08:00
|
|
|
if val.startswith("t") or val.startswith("1"):
|
2016-11-11 08:23:08 +08:00
|
|
|
return True
|
2012-05-09 08:10:38 +08:00
|
|
|
return bool(val)
|
2012-03-23 01:23:07 +08:00
|
|
|
|
2016-11-11 08:23:08 +08:00
|
|
|
|
2012-09-06 16:30:26 +08:00
|
|
|
def html_color_for_ansi_color_index(val):
|
2019-05-05 18:09:25 +08:00
|
|
|
arr = [
|
|
|
|
"black",
|
|
|
|
"#AA0000",
|
|
|
|
"#00AA00",
|
|
|
|
"#AA5500",
|
|
|
|
"#0000AA",
|
|
|
|
"#AA00AA",
|
|
|
|
"#00AAAA",
|
|
|
|
"#AAAAAA",
|
|
|
|
"#555555",
|
|
|
|
"#FF5555",
|
|
|
|
"#55FF55",
|
|
|
|
"#FFFF55",
|
|
|
|
"#5555FF",
|
|
|
|
"#FF55FF",
|
|
|
|
"#55FFFF",
|
|
|
|
"white",
|
|
|
|
"#000000",
|
|
|
|
"#00005f",
|
|
|
|
"#000087",
|
|
|
|
"#0000af",
|
|
|
|
"#0000d7",
|
|
|
|
"#0000ff",
|
|
|
|
"#005f00",
|
|
|
|
"#005f5f",
|
|
|
|
"#005f87",
|
|
|
|
"#005faf",
|
|
|
|
"#005fd7",
|
|
|
|
"#005fff",
|
|
|
|
"#008700",
|
|
|
|
"#00875f",
|
|
|
|
"#008787",
|
|
|
|
"#0087af",
|
|
|
|
"#0087d7",
|
|
|
|
"#0087ff",
|
|
|
|
"#00af00",
|
|
|
|
"#00af5f",
|
|
|
|
"#00af87",
|
|
|
|
"#00afaf",
|
|
|
|
"#00afd7",
|
|
|
|
"#00afff",
|
|
|
|
"#00d700",
|
|
|
|
"#00d75f",
|
|
|
|
"#00d787",
|
|
|
|
"#00d7af",
|
|
|
|
"#00d7d7",
|
|
|
|
"#00d7ff",
|
|
|
|
"#00ff00",
|
|
|
|
"#00ff5f",
|
|
|
|
"#00ff87",
|
|
|
|
"#00ffaf",
|
|
|
|
"#00ffd7",
|
|
|
|
"#00ffff",
|
|
|
|
"#5f0000",
|
|
|
|
"#5f005f",
|
|
|
|
"#5f0087",
|
|
|
|
"#5f00af",
|
|
|
|
"#5f00d7",
|
|
|
|
"#5f00ff",
|
|
|
|
"#5f5f00",
|
|
|
|
"#5f5f5f",
|
|
|
|
"#5f5f87",
|
|
|
|
"#5f5faf",
|
|
|
|
"#5f5fd7",
|
|
|
|
"#5f5fff",
|
|
|
|
"#5f8700",
|
|
|
|
"#5f875f",
|
|
|
|
"#5f8787",
|
|
|
|
"#5f87af",
|
|
|
|
"#5f87d7",
|
|
|
|
"#5f87ff",
|
|
|
|
"#5faf00",
|
|
|
|
"#5faf5f",
|
|
|
|
"#5faf87",
|
|
|
|
"#5fafaf",
|
|
|
|
"#5fafd7",
|
|
|
|
"#5fafff",
|
|
|
|
"#5fd700",
|
|
|
|
"#5fd75f",
|
|
|
|
"#5fd787",
|
|
|
|
"#5fd7af",
|
|
|
|
"#5fd7d7",
|
|
|
|
"#5fd7ff",
|
|
|
|
"#5fff00",
|
|
|
|
"#5fff5f",
|
|
|
|
"#5fff87",
|
|
|
|
"#5fffaf",
|
|
|
|
"#5fffd7",
|
|
|
|
"#5fffff",
|
|
|
|
"#870000",
|
|
|
|
"#87005f",
|
|
|
|
"#870087",
|
|
|
|
"#8700af",
|
|
|
|
"#8700d7",
|
|
|
|
"#8700ff",
|
|
|
|
"#875f00",
|
|
|
|
"#875f5f",
|
|
|
|
"#875f87",
|
|
|
|
"#875faf",
|
|
|
|
"#875fd7",
|
|
|
|
"#875fff",
|
|
|
|
"#878700",
|
|
|
|
"#87875f",
|
|
|
|
"#878787",
|
|
|
|
"#8787af",
|
|
|
|
"#8787d7",
|
|
|
|
"#8787ff",
|
|
|
|
"#87af00",
|
|
|
|
"#87af5f",
|
|
|
|
"#87af87",
|
|
|
|
"#87afaf",
|
|
|
|
"#87afd7",
|
|
|
|
"#87afff",
|
|
|
|
"#87d700",
|
|
|
|
"#87d75f",
|
|
|
|
"#87d787",
|
|
|
|
"#87d7af",
|
|
|
|
"#87d7d7",
|
|
|
|
"#87d7ff",
|
|
|
|
"#87ff00",
|
|
|
|
"#87ff5f",
|
|
|
|
"#87ff87",
|
|
|
|
"#87ffaf",
|
|
|
|
"#87ffd7",
|
|
|
|
"#87ffff",
|
|
|
|
"#af0000",
|
|
|
|
"#af005f",
|
|
|
|
"#af0087",
|
|
|
|
"#af00af",
|
|
|
|
"#af00d7",
|
|
|
|
"#af00ff",
|
|
|
|
"#af5f00",
|
|
|
|
"#af5f5f",
|
|
|
|
"#af5f87",
|
|
|
|
"#af5faf",
|
|
|
|
"#af5fd7",
|
|
|
|
"#af5fff",
|
|
|
|
"#af8700",
|
|
|
|
"#af875f",
|
|
|
|
"#af8787",
|
|
|
|
"#af87af",
|
|
|
|
"#af87d7",
|
|
|
|
"#af87ff",
|
|
|
|
"#afaf00",
|
|
|
|
"#afaf5f",
|
|
|
|
"#afaf87",
|
|
|
|
"#afafaf",
|
|
|
|
"#afafd7",
|
|
|
|
"#afafff",
|
|
|
|
"#afd700",
|
|
|
|
"#afd75f",
|
|
|
|
"#afd787",
|
|
|
|
"#afd7af",
|
|
|
|
"#afd7d7",
|
|
|
|
"#afd7ff",
|
|
|
|
"#afff00",
|
|
|
|
"#afff5f",
|
|
|
|
"#afff87",
|
|
|
|
"#afffaf",
|
|
|
|
"#afffd7",
|
|
|
|
"#afffff",
|
|
|
|
"#d70000",
|
|
|
|
"#d7005f",
|
|
|
|
"#d70087",
|
|
|
|
"#d700af",
|
|
|
|
"#d700d7",
|
|
|
|
"#d700ff",
|
|
|
|
"#d75f00",
|
|
|
|
"#d75f5f",
|
|
|
|
"#d75f87",
|
|
|
|
"#d75faf",
|
|
|
|
"#d75fd7",
|
|
|
|
"#d75fff",
|
|
|
|
"#d78700",
|
|
|
|
"#d7875f",
|
|
|
|
"#d78787",
|
|
|
|
"#d787af",
|
|
|
|
"#d787d7",
|
|
|
|
"#d787ff",
|
|
|
|
"#d7af00",
|
|
|
|
"#d7af5f",
|
|
|
|
"#d7af87",
|
|
|
|
"#d7afaf",
|
|
|
|
"#d7afd7",
|
|
|
|
"#d7afff",
|
|
|
|
"#d7d700",
|
|
|
|
"#d7d75f",
|
|
|
|
"#d7d787",
|
|
|
|
"#d7d7af",
|
|
|
|
"#d7d7d7",
|
|
|
|
"#d7d7ff",
|
|
|
|
"#d7ff00",
|
|
|
|
"#d7ff5f",
|
|
|
|
"#d7ff87",
|
|
|
|
"#d7ffaf",
|
|
|
|
"#d7ffd7",
|
|
|
|
"#d7ffff",
|
|
|
|
"#ff0000",
|
|
|
|
"#ff005f",
|
|
|
|
"#ff0087",
|
|
|
|
"#ff00af",
|
|
|
|
"#ff00d7",
|
|
|
|
"#ff00ff",
|
|
|
|
"#ff5f00",
|
|
|
|
"#ff5f5f",
|
|
|
|
"#ff5f87",
|
|
|
|
"#ff5faf",
|
|
|
|
"#ff5fd7",
|
|
|
|
"#ff5fff",
|
|
|
|
"#ff8700",
|
|
|
|
"#ff875f",
|
|
|
|
"#ff8787",
|
|
|
|
"#ff87af",
|
|
|
|
"#ff87d7",
|
|
|
|
"#ff87ff",
|
|
|
|
"#ffaf00",
|
|
|
|
"#ffaf5f",
|
|
|
|
"#ffaf87",
|
|
|
|
"#ffafaf",
|
|
|
|
"#ffafd7",
|
|
|
|
"#ffafff",
|
|
|
|
"#ffd700",
|
|
|
|
"#ffd75f",
|
|
|
|
"#ffd787",
|
|
|
|
"#ffd7af",
|
|
|
|
"#ffd7d7",
|
|
|
|
"#ffd7ff",
|
|
|
|
"#ffff00",
|
|
|
|
"#ffff5f",
|
|
|
|
"#ffff87",
|
|
|
|
"#ffffaf",
|
|
|
|
"#ffffd7",
|
|
|
|
"#ffffff",
|
|
|
|
"#080808",
|
|
|
|
"#121212",
|
|
|
|
"#1c1c1c",
|
|
|
|
"#262626",
|
|
|
|
"#303030",
|
|
|
|
"#3a3a3a",
|
|
|
|
"#444444",
|
|
|
|
"#4e4e4e",
|
|
|
|
"#585858",
|
|
|
|
"#626262",
|
|
|
|
"#6c6c6c",
|
|
|
|
"#767676",
|
|
|
|
"#808080",
|
|
|
|
"#8a8a8a",
|
|
|
|
"#949494",
|
|
|
|
"#9e9e9e",
|
|
|
|
"#a8a8a8",
|
|
|
|
"#b2b2b2",
|
|
|
|
"#bcbcbc",
|
|
|
|
"#c6c6c6",
|
|
|
|
"#d0d0d0",
|
|
|
|
"#dadada",
|
|
|
|
"#e4e4e4",
|
|
|
|
"#eeeeee",
|
|
|
|
]
|
2012-09-06 17:03:21 +08:00
|
|
|
if val < 0 or val >= len(arr):
|
2019-05-05 18:09:25 +08:00
|
|
|
return ""
|
2012-09-06 17:03:21 +08:00
|
|
|
else:
|
|
|
|
return arr[val]
|
2012-09-06 16:30:26 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
|
2012-09-06 16:30:26 +08:00
|
|
|
# Function to return special ANSI escapes like exit_attribute_mode
|
|
|
|
g_special_escapes_dict = None
|
2016-11-11 08:23:08 +08:00
|
|
|
|
|
|
|
|
2012-09-06 16:30:26 +08:00
|
|
|
def get_special_ansi_escapes():
|
2012-09-06 17:03:21 +08:00
|
|
|
global g_special_escapes_dict
|
|
|
|
if g_special_escapes_dict is None:
|
|
|
|
import curses
|
2019-05-05 18:09:25 +08:00
|
|
|
|
2012-09-06 17:03:21 +08:00
|
|
|
g_special_escapes_dict = {}
|
|
|
|
curses.setupterm()
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-06 17:03:21 +08:00
|
|
|
# Helper function to get a value for a tparm
|
|
|
|
def get_tparm(key):
|
|
|
|
val = None
|
2019-05-01 05:08:11 +08:00
|
|
|
key = curses.tigetstr(key)
|
2016-11-11 08:23:08 +08:00
|
|
|
if key:
|
|
|
|
val = curses.tparm(key)
|
|
|
|
if val:
|
2019-05-05 18:09:25 +08:00
|
|
|
val = val.decode("utf-8")
|
2012-09-06 17:03:21 +08:00
|
|
|
return val
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-06 17:03:21 +08:00
|
|
|
# Just a few for now
|
2019-05-05 18:09:25 +08:00
|
|
|
g_special_escapes_dict["exit_attribute_mode"] = get_tparm("sgr0")
|
|
|
|
g_special_escapes_dict["bold"] = get_tparm("bold")
|
|
|
|
g_special_escapes_dict["underline"] = get_tparm("smul")
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-06 17:03:21 +08:00
|
|
|
return g_special_escapes_dict
|
2012-09-06 16:30:26 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
|
2012-09-06 16:30:26 +08:00
|
|
|
# Given a known ANSI escape sequence, convert it to HTML and append to the list
|
|
|
|
# Returns whether we have an open <span>
|
2016-11-11 08:23:08 +08:00
|
|
|
|
|
|
|
|
2012-09-06 16:30:26 +08:00
|
|
|
def append_html_for_ansi_escape(full_val, result, span_open):
|
2012-09-06 17:03:21 +08:00
|
|
|
# Strip off the initial \x1b[ and terminating m
|
|
|
|
val = full_val[2:-1]
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-06 17:03:21 +08:00
|
|
|
# Helper function to close a span if it's open
|
|
|
|
def close_span():
|
|
|
|
if span_open:
|
2019-05-05 18:09:25 +08:00
|
|
|
result.append("</span>")
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-06 17:03:21 +08:00
|
|
|
# term256 foreground color
|
2019-05-05 18:09:25 +08:00
|
|
|
match = re.match("38;5;(\d+)", val)
|
2012-09-06 17:03:21 +08:00
|
|
|
if match is not None:
|
|
|
|
close_span()
|
2016-11-11 08:23:08 +08:00
|
|
|
html_color = html_color_for_ansi_color_index(int(match.group(1)))
|
2012-09-06 17:03:21 +08:00
|
|
|
result.append('<span style="color: ' + html_color + '">')
|
2016-11-11 08:23:08 +08:00
|
|
|
return True # span now open
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2019-01-22 05:59:36 +08:00
|
|
|
# term16 foreground color
|
|
|
|
if val in (str(x) for x in chain(range(90, 97), range(30, 38))):
|
2012-09-06 17:03:21 +08:00
|
|
|
close_span()
|
2019-05-05 18:09:25 +08:00
|
|
|
html_color = html_color_for_ansi_color_index(
|
|
|
|
int(val) - (30 if int(val) < 90 else 82)
|
|
|
|
)
|
2012-09-06 17:03:21 +08:00
|
|
|
result.append('<span style="color: ' + html_color + '">')
|
2016-11-11 08:23:08 +08:00
|
|
|
return True # span now open
|
2012-09-06 17:03:21 +08:00
|
|
|
|
|
|
|
# Try special escapes
|
|
|
|
special_escapes = get_special_ansi_escapes()
|
2019-05-05 18:09:25 +08:00
|
|
|
if full_val == special_escapes["exit_attribute_mode"]:
|
2012-09-06 17:03:21 +08:00
|
|
|
close_span()
|
|
|
|
return False
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2019-01-22 05:59:36 +08:00
|
|
|
# TODO We don't handle bold, underline, italics, dim, or reverse yet
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-06 17:03:21 +08:00
|
|
|
# Do nothing on failure
|
|
|
|
return span_open
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2016-11-11 08:23:08 +08:00
|
|
|
|
2012-09-06 16:30:26 +08:00
|
|
|
def strip_ansi(val):
|
2012-09-06 17:03:21 +08:00
|
|
|
# Make a half-assed effort to strip ANSI control sequences
|
2019-01-16 17:23:53 +08:00
|
|
|
# We assume that all such sequences start with 0x1b and end with m or ctrl-o,
|
2012-09-06 17:03:21 +08:00
|
|
|
# which catches most cases
|
2019-05-05 18:09:25 +08:00
|
|
|
return re.sub("\x1b[^m]*m\x0f?", "", val)
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2016-11-11 08:23:08 +08:00
|
|
|
|
2012-09-06 16:30:26 +08:00
|
|
|
def ansi_prompt_line_width(val):
|
2016-11-11 08:23:08 +08:00
|
|
|
# Given an ANSI prompt, return the length of its longest line, as in the
|
|
|
|
# number of characters it takes up. Start by stripping off ANSI.
|
2012-09-06 17:03:21 +08:00
|
|
|
stripped_val = strip_ansi(val)
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-06 17:03:21 +08:00
|
|
|
# Now count the longest line
|
2019-05-05 18:09:25 +08:00
|
|
|
return max([len(x) for x in stripped_val.split("\n")])
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-06 16:30:26 +08:00
|
|
|
|
|
|
|
def ansi_to_html(val):
|
2016-11-11 08:23:08 +08:00
|
|
|
# Split us up by ANSI escape sequences. We want to catch not only the
|
|
|
|
# standard color codes, but also things like sgr0. Hence this lame check.
|
|
|
|
# Note that Python 2.6 doesn't have a flag param to re.split, so we have to
|
|
|
|
# compile it first.
|
2019-05-05 18:09:25 +08:00
|
|
|
reg = re.compile(
|
|
|
|
"""
|
2012-09-06 17:03:21 +08:00
|
|
|
( # Capture
|
2012-09-07 10:01:07 +08:00
|
|
|
\x1b # Escape
|
2018-12-01 03:19:37 +08:00
|
|
|
[^m]* # Zero or more non-'m's
|
2012-09-07 10:01:07 +08:00
|
|
|
m # Literal m terminates the sequence
|
2018-12-01 03:19:37 +08:00
|
|
|
\x0f? # HACK: A ctrl-o - this is how tmux' sgr0 ends
|
2012-09-06 17:03:21 +08:00
|
|
|
) # End capture
|
2019-05-05 18:09:25 +08:00
|
|
|
""",
|
|
|
|
re.VERBOSE,
|
|
|
|
)
|
2012-09-10 17:11:06 +08:00
|
|
|
separated = reg.split(val)
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-06 17:03:21 +08:00
|
|
|
# We have to HTML escape the text and convert ANSI escapes into HTML
|
|
|
|
# Collect it all into this array
|
|
|
|
result = []
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-06 17:03:21 +08:00
|
|
|
span_open = False
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-06 17:03:21 +08:00
|
|
|
# Text is at even indexes, escape sequences at odd indexes
|
|
|
|
for i in range(len(separated)):
|
|
|
|
component = separated[i]
|
|
|
|
if i % 2 == 0:
|
|
|
|
# It's text, possibly empty
|
|
|
|
# Clean up other ANSI junk
|
2018-12-01 02:44:20 +08:00
|
|
|
result.append(escape_html(strip_ansi(component)))
|
2012-09-06 17:03:21 +08:00
|
|
|
else:
|
|
|
|
# It's an escape sequence. Close the previous escape.
|
2019-05-05 18:09:25 +08:00
|
|
|
span_open = append_html_for_ansi_escape(component, result, span_open)
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-06 17:03:21 +08:00
|
|
|
# Close final escape
|
2016-11-11 08:23:08 +08:00
|
|
|
if span_open:
|
2019-05-05 18:09:25 +08:00
|
|
|
result.append("</span>")
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-07 10:01:07 +08:00
|
|
|
# Remove empty elements
|
|
|
|
result = [x for x in result if x]
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-07 10:01:07 +08:00
|
|
|
# Clean up empty spans, the nasty way
|
|
|
|
idx = len(result) - 1
|
|
|
|
while idx >= 1:
|
2019-05-05 18:09:25 +08:00
|
|
|
if result[idx] == "</span>" and result[idx - 1].startswith("<span"):
|
2013-07-06 23:39:20 +08:00
|
|
|
# Empty span, delete these two
|
2019-05-05 18:09:25 +08:00
|
|
|
result[idx - 1 : idx + 1] = []
|
2013-07-06 23:39:20 +08:00
|
|
|
idx = idx - 1
|
|
|
|
idx = idx - 1
|
2012-09-07 10:01:07 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
return "".join(result)
|
2012-09-06 16:30:26 +08:00
|
|
|
|
2016-11-11 08:23:08 +08:00
|
|
|
|
2012-03-17 08:21:37 +08:00
|
|
|
class FishVar:
|
2012-05-09 08:10:38 +08:00
|
|
|
""" A class that represents a variable """
|
2019-05-05 18:09:25 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
def __init__(self, name, value):
|
|
|
|
self.name = name
|
|
|
|
self.value = value
|
|
|
|
self.universal = False
|
|
|
|
self.exported = False
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
def get_json_obj(self):
|
|
|
|
# Return an array(3): name, value, flags
|
|
|
|
flags = []
|
2016-11-11 08:23:08 +08:00
|
|
|
if self.universal:
|
2019-05-05 18:09:25 +08:00
|
|
|
flags.append("universal")
|
2016-11-11 08:23:08 +08:00
|
|
|
if self.exported:
|
2019-05-05 18:09:25 +08:00
|
|
|
flags.append("exported")
|
|
|
|
return {"name": self.name, "value": self.value, "Flags": ", ".join(flags)}
|
2016-11-11 08:23:08 +08:00
|
|
|
|
2012-03-17 08:21:37 +08:00
|
|
|
|
2013-10-13 21:06:46 +08:00
|
|
|
class FishBinding:
|
|
|
|
"""A class that represents keyboard binding """
|
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
def __init__(self, command, raw_binding, readable_binding, description=None):
|
2016-11-11 08:23:08 +08:00
|
|
|
self.command = command
|
2016-08-23 17:10:30 +08:00
|
|
|
self.bindings = []
|
2013-10-13 21:06:46 +08:00
|
|
|
self.description = description
|
2016-08-23 17:10:30 +08:00
|
|
|
self.add_binding(raw_binding, readable_binding)
|
|
|
|
|
|
|
|
def add_binding(self, raw_binding, readable_binding):
|
|
|
|
for i in self.bindings:
|
2019-05-05 18:09:25 +08:00
|
|
|
if i["readable_binding"] == readable_binding:
|
|
|
|
i["raw_bindings"].append(raw_binding)
|
2016-08-23 17:10:30 +08:00
|
|
|
break
|
|
|
|
else:
|
2019-05-05 18:09:25 +08:00
|
|
|
self.bindings.append(
|
|
|
|
{"readable_binding": readable_binding, "raw_bindings": [raw_binding]}
|
|
|
|
)
|
2013-10-13 21:06:46 +08:00
|
|
|
|
|
|
|
def get_json_obj(self):
|
2019-05-05 18:09:25 +08:00
|
|
|
return {
|
|
|
|
"command": self.command,
|
|
|
|
"bindings": self.bindings,
|
|
|
|
"description": self.description,
|
|
|
|
}
|
2013-10-13 21:06:46 +08:00
|
|
|
|
2013-10-13 23:10:17 +08:00
|
|
|
|
|
|
|
class BindingParser:
|
|
|
|
""" Class to parse codes for bind command """
|
|
|
|
|
2016-11-11 08:23:08 +08:00
|
|
|
# TODO: What does snext and sprevious mean ?
|
2019-05-05 18:09:25 +08:00
|
|
|
readable_keys = {
|
|
|
|
"dc": "Delete",
|
|
|
|
"npage": "Page Up",
|
|
|
|
"ppage": "Page Down",
|
|
|
|
"sdc": "Shift Delete",
|
|
|
|
"shome": "Shift Home",
|
|
|
|
"left": "Left Arrow",
|
|
|
|
"right": "Right Arrow",
|
|
|
|
"up": "Up Arrow",
|
|
|
|
"down": "Down Arrow",
|
|
|
|
"sleft": "Shift Left",
|
|
|
|
"sright": "Shift Right",
|
|
|
|
"btab": "Shift Tab",
|
|
|
|
}
|
2013-10-14 00:03:05 +08:00
|
|
|
|
2014-06-08 18:32:05 +08:00
|
|
|
def set_buffer(self, buffer):
|
2013-10-13 23:10:17 +08:00
|
|
|
""" Sets code to parse """
|
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
self.buffer = buffer or b""
|
2013-10-13 23:10:17 +08:00
|
|
|
self.index = 0
|
|
|
|
|
|
|
|
def get_char(self):
|
|
|
|
""" Gets next character from buffer """
|
2014-02-10 12:19:04 +08:00
|
|
|
if self.index >= len(self.buffer):
|
2019-05-05 18:09:25 +08:00
|
|
|
return "\0"
|
2013-10-13 23:10:17 +08:00
|
|
|
c = self.buffer[self.index]
|
|
|
|
self.index += 1
|
|
|
|
return c
|
|
|
|
|
|
|
|
def unget_char(self):
|
|
|
|
""" Goes back by one character for parsing """
|
|
|
|
|
|
|
|
self.index -= 1
|
|
|
|
|
|
|
|
def end(self):
|
|
|
|
""" Returns true if reached end of buffer """
|
|
|
|
|
|
|
|
return self.index >= len(self.buffer)
|
|
|
|
|
|
|
|
def parse_control_sequence(self):
|
|
|
|
""" Parses terminal specifiec control sequences """
|
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
result = ""
|
2013-10-13 23:10:17 +08:00
|
|
|
c = self.get_char()
|
2013-10-25 02:35:26 +08:00
|
|
|
|
|
|
|
# \e0 is used to denote start of control sequence
|
2019-05-05 18:09:25 +08:00
|
|
|
if c == "O":
|
2013-10-13 23:10:17 +08:00
|
|
|
c = self.get_char()
|
|
|
|
|
2013-10-25 02:35:26 +08:00
|
|
|
# \[1\; is start of control sequence
|
2019-05-05 18:09:25 +08:00
|
|
|
if c == "1":
|
2016-11-11 08:23:08 +08:00
|
|
|
b = self.get_char()
|
|
|
|
c = self.get_char()
|
2019-05-05 18:09:25 +08:00
|
|
|
if b == "\\" and c == "~":
|
2016-08-23 18:33:58 +08:00
|
|
|
result += "Home"
|
|
|
|
elif c == ";":
|
2013-12-07 15:42:43 +08:00
|
|
|
c = self.get_char()
|
2013-10-13 23:10:17 +08:00
|
|
|
|
2013-10-25 02:35:26 +08:00
|
|
|
# 3 is Alt
|
2019-05-05 18:09:25 +08:00
|
|
|
if c == "3":
|
2013-10-25 02:35:26 +08:00
|
|
|
result += "ALT - "
|
|
|
|
c = self.get_char()
|
2013-10-13 23:10:17 +08:00
|
|
|
|
2016-08-23 18:33:58 +08:00
|
|
|
# \[4\~ is End
|
2019-05-05 18:09:25 +08:00
|
|
|
if c == "4":
|
2016-11-11 08:23:08 +08:00
|
|
|
b = self.get_char()
|
|
|
|
c = self.get_char()
|
2019-05-05 18:09:25 +08:00
|
|
|
if b == "\\" and c == "~":
|
2016-08-23 18:33:58 +08:00
|
|
|
result += "End"
|
|
|
|
|
2013-10-25 02:35:26 +08:00
|
|
|
# 5 is Ctrl
|
2019-05-05 18:09:25 +08:00
|
|
|
if c == "5":
|
2013-10-25 02:35:26 +08:00
|
|
|
result += "CTRL - "
|
|
|
|
c = self.get_char()
|
2013-10-13 23:10:17 +08:00
|
|
|
|
2013-10-25 02:35:26 +08:00
|
|
|
# 9 is Alt
|
2019-05-05 18:09:25 +08:00
|
|
|
if c == "9":
|
2013-10-25 02:35:26 +08:00
|
|
|
result += "ALT - "
|
|
|
|
c = self.get_char()
|
2013-10-13 23:10:17 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
if c == "A":
|
|
|
|
result += "Up Arrow"
|
|
|
|
elif c == "B":
|
|
|
|
result += "Down Arrow"
|
|
|
|
elif c == "C":
|
|
|
|
result += "Right Arrow"
|
|
|
|
elif c == "D":
|
2013-10-25 02:35:26 +08:00
|
|
|
result += "Left Arrow"
|
2019-05-05 18:09:25 +08:00
|
|
|
elif c == "F":
|
2013-10-25 02:35:26 +08:00
|
|
|
result += "End"
|
2019-05-05 18:09:25 +08:00
|
|
|
elif c == "H":
|
2013-10-25 02:35:26 +08:00
|
|
|
result += "Home"
|
2013-10-23 01:38:30 +08:00
|
|
|
|
2013-10-13 23:10:17 +08:00
|
|
|
return result
|
|
|
|
|
|
|
|
def get_readable_binding(self):
|
|
|
|
""" Gets a readable representation of binding """
|
|
|
|
|
2014-06-08 18:32:05 +08:00
|
|
|
try:
|
2016-08-23 18:40:37 +08:00
|
|
|
result = BindingParser.readable_keys[self.buffer.lower()]
|
2014-06-08 18:32:05 +08:00
|
|
|
except KeyError:
|
2013-10-14 00:03:05 +08:00
|
|
|
result = self.parse_binding()
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
def parse_binding(self):
|
2019-05-05 18:09:25 +08:00
|
|
|
readable_command = ""
|
|
|
|
result = ""
|
2013-10-13 23:10:17 +08:00
|
|
|
alt = ctrl = False
|
|
|
|
|
|
|
|
while not self.end():
|
|
|
|
c = self.get_char()
|
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
if c == "\\":
|
2013-11-09 20:56:44 +08:00
|
|
|
c = self.get_char()
|
2019-05-05 18:09:25 +08:00
|
|
|
if c == "e":
|
2013-10-25 02:35:26 +08:00
|
|
|
d = self.get_char()
|
2019-05-05 18:09:25 +08:00
|
|
|
if d == "O":
|
2013-10-25 02:35:26 +08:00
|
|
|
self.unget_char()
|
2013-10-13 23:10:17 +08:00
|
|
|
result += self.parse_control_sequence()
|
2019-05-05 18:09:25 +08:00
|
|
|
elif d == "\\":
|
|
|
|
if self.get_char() == "[":
|
2013-10-25 02:35:26 +08:00
|
|
|
result += self.parse_control_sequence()
|
|
|
|
else:
|
|
|
|
self.unget_char()
|
|
|
|
self.unget_char()
|
|
|
|
alt = True
|
2019-05-05 18:09:25 +08:00
|
|
|
elif d == "\0":
|
|
|
|
result += "ESC"
|
2013-10-25 02:35:26 +08:00
|
|
|
else:
|
|
|
|
alt = True
|
|
|
|
self.unget_char()
|
2019-05-05 18:09:25 +08:00
|
|
|
elif c == "c":
|
2013-10-13 23:10:17 +08:00
|
|
|
ctrl = True
|
2019-05-05 18:09:25 +08:00
|
|
|
elif c == "n":
|
|
|
|
result += "Enter"
|
|
|
|
elif c == "t":
|
|
|
|
result += "Tab"
|
|
|
|
elif c == "b":
|
|
|
|
result += "Backspace"
|
2016-08-23 18:33:58 +08:00
|
|
|
elif c.isalpha():
|
2019-05-05 18:09:25 +08:00
|
|
|
result += "\\" + c
|
2013-10-25 02:35:26 +08:00
|
|
|
else:
|
2013-11-09 20:56:44 +08:00
|
|
|
result += c
|
2019-05-05 18:09:25 +08:00
|
|
|
elif c == "\x7f":
|
|
|
|
result += "Backspace"
|
2013-10-13 23:10:17 +08:00
|
|
|
else:
|
|
|
|
result += c
|
2013-11-09 20:56:44 +08:00
|
|
|
if ctrl:
|
2019-05-05 18:09:25 +08:00
|
|
|
readable_command += "CTRL - "
|
2013-10-13 23:10:17 +08:00
|
|
|
if alt:
|
2019-05-05 18:09:25 +08:00
|
|
|
readable_command += "ALT - "
|
2016-09-11 11:42:52 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
if result == "":
|
|
|
|
return "unknown-control-sequence"
|
2013-10-13 23:10:17 +08:00
|
|
|
|
|
|
|
return readable_command + result
|
|
|
|
|
2013-10-25 02:35:26 +08:00
|
|
|
|
2014-03-04 16:59:26 +08:00
|
|
|
class FishConfigTCPServer(SocketServer.TCPServer):
|
|
|
|
"""TCPServer that only accepts connections from localhost (IPv4/IPv6)."""
|
2019-05-05 18:09:25 +08:00
|
|
|
|
|
|
|
WHITELIST = set(["::1", "::ffff:127.0.0.1", "127.0.0.1"])
|
2014-03-04 16:59:26 +08:00
|
|
|
|
2016-11-11 07:43:59 +08:00
|
|
|
address_family = socket.AF_INET6 if socket.has_ipv6 else socket.AF_INET
|
2014-03-04 16:59:26 +08:00
|
|
|
|
|
|
|
def verify_request(self, request, client_address):
|
|
|
|
return client_address[0] in FishConfigTCPServer.WHITELIST
|
|
|
|
|
|
|
|
|
2012-03-15 18:43:45 +08:00
|
|
|
class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
2012-07-27 15:31:00 +08:00
|
|
|
def write_to_wfile(self, txt):
|
2019-05-05 18:09:25 +08:00
|
|
|
self.wfile.write(txt.encode("utf-8"))
|
2012-07-27 15:31:00 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
def do_get_colors(self):
|
|
|
|
# Looks for fish_color_*.
|
|
|
|
# Returns an array of lists [color_name, color_description, color_value]
|
|
|
|
result = []
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
# Make sure we return at least these
|
2019-05-05 18:09:25 +08:00
|
|
|
remaining = set(
|
|
|
|
[
|
|
|
|
"normal",
|
|
|
|
"error",
|
|
|
|
"command",
|
|
|
|
"end",
|
|
|
|
"param",
|
|
|
|
"comment",
|
|
|
|
"match",
|
|
|
|
"selection",
|
|
|
|
"search_match",
|
|
|
|
"operator",
|
|
|
|
"escape",
|
|
|
|
"quote",
|
|
|
|
"redirection",
|
|
|
|
"valid_path",
|
|
|
|
"autosuggestion" "user",
|
|
|
|
"host",
|
|
|
|
"cancel",
|
|
|
|
]
|
|
|
|
)
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
# Here are our color descriptions
|
|
|
|
descriptions = {
|
2019-05-05 18:09:25 +08:00
|
|
|
"normal": "Default text",
|
|
|
|
"command": "Ordinary commands",
|
|
|
|
"quote": "Text within quotes",
|
|
|
|
"redirection": "Like | and >",
|
|
|
|
"end": "Like ; and &",
|
|
|
|
"error": "Potential errors",
|
|
|
|
"param": "Command parameters",
|
|
|
|
"comment": "Comments start with #",
|
|
|
|
"match": "Matching parenthesis",
|
|
|
|
"selection": "Selected text",
|
|
|
|
"search_match": "History searching",
|
|
|
|
"history_current": "Directory history",
|
|
|
|
"operator": "Like * and ~",
|
|
|
|
"escape": "Escapes like \\n",
|
|
|
|
"cwd": "Current directory",
|
|
|
|
"cwd_root": "cwd for root user",
|
|
|
|
"valid_path": "Valid paths",
|
|
|
|
"autosuggestion": "Suggested completion",
|
|
|
|
"user": "Username in the prompt",
|
|
|
|
"host": "Hostname in the prompt",
|
|
|
|
"cancel": "The ^C cancel indicator",
|
2012-05-09 08:10:38 +08:00
|
|
|
}
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
out, err = run_fish_cmd("set -L")
|
|
|
|
for line in out.split("\n"):
|
2012-06-05 22:25:11 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
for match in re.finditer(r"^fish_color_(\S+) ?(.*)", line):
|
|
|
|
color_name, color_value = [x.strip() for x in match.group(1, 2)]
|
2019-05-05 18:09:25 +08:00
|
|
|
color_desc = descriptions.get(color_name, "")
|
2016-11-11 08:23:08 +08:00
|
|
|
data = {"name": color_name, "description": color_desc}
|
2013-08-17 02:32:58 +08:00
|
|
|
data.update(parse_color(color_value))
|
|
|
|
result.append(data)
|
2012-05-09 08:10:38 +08:00
|
|
|
remaining.discard(color_name)
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2014-09-23 19:48:00 +08:00
|
|
|
# Sort our result (by their keys)
|
2019-05-05 18:09:25 +08:00
|
|
|
result.sort(key=operator.itemgetter("name"))
|
2014-09-23 19:48:00 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
# Ensure that we have all the color names we know about, so that if the
|
|
|
|
# user deletes one he can still set it again via the web interface
|
|
|
|
for color_name in remaining:
|
2019-05-05 18:09:25 +08:00
|
|
|
color_desc = descriptions.get(color_name, "")
|
|
|
|
result.append([color_name, color_desc, parse_color("")])
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
return result
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
def do_get_functions(self):
|
2019-05-05 18:09:25 +08:00
|
|
|
out, err = run_fish_cmd("functions")
|
2012-05-09 08:10:38 +08:00
|
|
|
out = out.strip()
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
# Not sure why fish sometimes returns this with newlines
|
|
|
|
if "\n" in out:
|
2019-05-05 18:09:25 +08:00
|
|
|
return out.split("\n")
|
2012-05-09 08:10:38 +08:00
|
|
|
else:
|
2019-05-05 18:09:25 +08:00
|
|
|
return out.strip().split(", ")
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
def do_get_variable_names(self, cmd):
|
|
|
|
" Given a command like 'set -U' return all the variable names "
|
|
|
|
out, err = run_fish_cmd(cmd)
|
2019-05-05 18:09:25 +08:00
|
|
|
return out.split("\n")
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
def do_get_variables(self):
|
2019-05-05 18:09:25 +08:00
|
|
|
out, err = run_fish_cmd("set -L")
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
# Put all the variables into a dictionary
|
|
|
|
vars = {}
|
2019-05-05 18:09:25 +08:00
|
|
|
for line in out.split("\n"):
|
|
|
|
comps = line.split(" ", 1)
|
2016-11-11 08:23:08 +08:00
|
|
|
if len(comps) < 2:
|
|
|
|
continue
|
2012-05-09 08:10:38 +08:00
|
|
|
fish_var = FishVar(comps[0], comps[1])
|
|
|
|
vars[fish_var.name] = fish_var
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
# Mark universal variables. L means don't abbreviate.
|
2019-05-05 18:09:25 +08:00
|
|
|
for name in self.do_get_variable_names("set -nUL"):
|
2016-11-11 08:23:08 +08:00
|
|
|
if name in vars:
|
|
|
|
vars[name].universal = True
|
2012-05-09 08:10:38 +08:00
|
|
|
# Mark exported variables. L means don't abbreviate.
|
2019-05-05 18:09:25 +08:00
|
|
|
for name in self.do_get_variable_names("set -nxL"):
|
2016-11-11 08:23:08 +08:00
|
|
|
if name in vars:
|
|
|
|
vars[name].exported = True
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
return [
|
|
|
|
vars[key].get_json_obj()
|
|
|
|
for key in sorted(vars.keys(), key=lambda x: x.lower())
|
|
|
|
]
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2013-10-13 21:06:46 +08:00
|
|
|
def do_get_bindings(self):
|
2013-10-14 00:03:05 +08:00
|
|
|
""" Get key bindings """
|
|
|
|
|
2013-10-20 17:33:47 +08:00
|
|
|
# Running __fish_config_interactive print fish greeting and
|
|
|
|
# loads key bindings
|
2019-05-05 18:09:25 +08:00
|
|
|
greeting, err = run_fish_cmd(" __fish_config_interactive")
|
2013-10-20 17:33:47 +08:00
|
|
|
|
|
|
|
# Load the key bindings and then list them with bind
|
2019-05-05 18:09:25 +08:00
|
|
|
out, err = run_fish_cmd("__fish_config_interactive; bind")
|
2013-10-14 00:03:05 +08:00
|
|
|
|
|
|
|
# Remove fish greeting from output
|
2019-05-05 18:09:25 +08:00
|
|
|
out = out[len(greeting) :]
|
2013-10-14 00:03:05 +08:00
|
|
|
|
|
|
|
# Put all the bindings into a list
|
2013-11-09 20:56:44 +08:00
|
|
|
bindings = []
|
2016-08-23 17:10:30 +08:00
|
|
|
command_to_binding = {}
|
2013-10-13 23:10:17 +08:00
|
|
|
binding_parser = BindingParser()
|
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
for line in out.split("\n"):
|
|
|
|
comps = line.split(" ", 2)
|
2014-06-08 18:32:05 +08:00
|
|
|
|
2019-01-16 16:29:17 +08:00
|
|
|
# If we don't have "bind", a sequence and a mapping,
|
|
|
|
# it's not a valid binding.
|
|
|
|
if len(comps) < 3:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Store the "--preset" value for later
|
2019-05-05 18:09:25 +08:00
|
|
|
if comps[1] == "--preset":
|
2019-01-16 16:29:17 +08:00
|
|
|
preset = True
|
|
|
|
# There's possibly a way to do this faster, but it's not important.
|
2019-05-05 18:09:25 +08:00
|
|
|
comps = line.split(" ", 3)[1:]
|
|
|
|
elif comps[1] == "--user":
|
2019-01-16 16:29:17 +08:00
|
|
|
preset = False
|
2019-05-05 18:09:25 +08:00
|
|
|
comps = line.split(" ", 3)[1:]
|
2019-01-16 16:29:17 +08:00
|
|
|
# Check again if we removed the level.
|
2014-07-25 11:05:13 +08:00
|
|
|
if len(comps) < 3:
|
2013-10-13 21:06:46 +08:00
|
|
|
continue
|
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
if comps[1] == "-k":
|
|
|
|
key_name, command = comps[2].split(" ", 1)
|
2016-08-23 18:40:37 +08:00
|
|
|
binding_parser.set_buffer(key_name.capitalize())
|
2014-07-23 04:23:15 +08:00
|
|
|
else:
|
|
|
|
key_name = None
|
2014-07-25 11:05:13 +08:00
|
|
|
command = comps[2]
|
|
|
|
binding_parser.set_buffer(comps[1])
|
2014-06-08 18:32:05 +08:00
|
|
|
|
2016-08-23 17:34:06 +08:00
|
|
|
if command in bindings_blacklist:
|
|
|
|
continue
|
|
|
|
|
2014-07-23 04:23:15 +08:00
|
|
|
readable_binding = binding_parser.get_readable_binding()
|
2016-08-23 17:10:30 +08:00
|
|
|
if command in command_to_binding:
|
|
|
|
fish_binding = command_to_binding[command]
|
|
|
|
fish_binding.add_binding(line, readable_binding)
|
|
|
|
else:
|
|
|
|
fish_binding = FishBinding(command, line, readable_binding)
|
|
|
|
bindings.append(fish_binding)
|
|
|
|
command_to_binding[command] = fish_binding
|
2013-10-13 21:06:46 +08:00
|
|
|
|
2016-11-11 08:23:08 +08:00
|
|
|
return [binding.get_json_obj() for binding in bindings]
|
2013-10-13 21:06:46 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
def do_get_history(self):
|
2019-01-16 16:31:03 +08:00
|
|
|
# Use NUL to distinguish between history items.
|
2019-05-05 18:09:25 +08:00
|
|
|
out, err = run_fish_cmd("builtin history -z")
|
|
|
|
result = out.split("\0")
|
2016-11-11 08:23:08 +08:00
|
|
|
if result:
|
|
|
|
result.pop() # trim off the trailing element
|
2012-05-09 08:10:38 +08:00
|
|
|
return result
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
def do_get_color_for_variable(self, name):
|
2016-11-11 08:23:08 +08:00
|
|
|
# Return the color with the given name, or the empty string if there is
|
|
|
|
# none.
|
2012-05-09 08:10:38 +08:00
|
|
|
out, err = run_fish_cmd("echo -n $" + name)
|
|
|
|
return out
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
def do_set_color_for_variable(
|
|
|
|
self, name, color, background_color, bold, underline, italics, dim, reverse
|
|
|
|
):
|
2018-11-29 22:18:00 +08:00
|
|
|
"Sets a color for a fish color name, like 'autosuggestion'"
|
2016-11-11 08:23:08 +08:00
|
|
|
if not color:
|
2019-05-05 18:09:25 +08:00
|
|
|
color = "normal"
|
|
|
|
varname = "fish_color_" + name
|
2018-11-29 22:18:00 +08:00
|
|
|
# If the name already starts with "fish_", use it as the varname
|
|
|
|
# This is needed for 'fish_pager_color' vars.
|
2019-05-05 18:09:25 +08:00
|
|
|
if name.startswith("fish_"):
|
2018-11-29 22:18:00 +08:00
|
|
|
varname = name
|
|
|
|
# TODO: Check if the varname is allowable.
|
2019-05-05 18:09:25 +08:00
|
|
|
command = "set -U " + varname
|
2016-11-11 08:23:08 +08:00
|
|
|
if color:
|
2019-05-05 18:09:25 +08:00
|
|
|
command += " " + color
|
2016-11-11 08:23:08 +08:00
|
|
|
if background_color:
|
2019-05-05 18:09:25 +08:00
|
|
|
command += " --background=" + background_color
|
2016-11-11 08:23:08 +08:00
|
|
|
if bold:
|
2019-05-05 18:09:25 +08:00
|
|
|
command += " --bold"
|
2016-11-11 08:23:08 +08:00
|
|
|
if underline:
|
2019-05-05 18:09:25 +08:00
|
|
|
command += " --underline"
|
2018-12-05 17:05:33 +08:00
|
|
|
if italics:
|
2019-05-05 18:09:25 +08:00
|
|
|
command += " --italics"
|
2018-12-05 17:05:33 +08:00
|
|
|
if dim:
|
2019-05-05 18:09:25 +08:00
|
|
|
command += " --dim"
|
2018-12-05 17:05:33 +08:00
|
|
|
if reverse:
|
2019-05-05 18:09:25 +08:00
|
|
|
command += " --reverse"
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
out, err = run_fish_cmd(command)
|
|
|
|
return out
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
def do_get_function(self, func_name):
|
2019-05-05 18:09:25 +08:00
|
|
|
out, err = run_fish_cmd("functions " + func_name + " | fish_indent --html")
|
2012-05-09 08:10:38 +08:00
|
|
|
return out
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-07-27 15:31:00 +08:00
|
|
|
def do_delete_history_item(self, history_item_text):
|
2012-08-20 03:55:50 +08:00
|
|
|
# It's really lame that we always return success here
|
2019-05-05 18:09:25 +08:00
|
|
|
cmd = (
|
|
|
|
"builtin history delete --case-sensitive --exact -- %s; builtin history save"
|
|
|
|
% escape_fish_cmd(history_item_text)
|
|
|
|
)
|
2016-09-11 11:42:52 +08:00
|
|
|
out, err = run_fish_cmd(cmd)
|
2012-08-20 03:55:50 +08:00
|
|
|
return True
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-06 16:30:26 +08:00
|
|
|
def do_set_prompt_function(self, prompt_func):
|
2019-05-05 18:09:25 +08:00
|
|
|
cmd = prompt_func + "\n" + "funcsave fish_prompt"
|
2012-09-06 17:03:21 +08:00
|
|
|
out, err = run_fish_cmd(cmd)
|
|
|
|
return len(err) == 0
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2014-07-30 03:12:32 +08:00
|
|
|
def do_get_prompt(self, command_to_run, prompt_function_text, extras_dict):
|
2012-09-06 17:03:21 +08:00
|
|
|
# Return the prompt output by the given command
|
|
|
|
prompt_demo_ansi, err = run_fish_cmd(command_to_run)
|
|
|
|
prompt_demo_html = ansi_to_html(prompt_demo_ansi)
|
|
|
|
prompt_demo_font_size = self.font_size_for_ansi_prompt(prompt_demo_ansi)
|
2019-05-05 18:09:25 +08:00
|
|
|
result = {
|
|
|
|
"function": prompt_function_text,
|
|
|
|
"demo": prompt_demo_html,
|
|
|
|
"font_size": prompt_demo_font_size,
|
|
|
|
}
|
2014-07-30 03:12:32 +08:00
|
|
|
if extras_dict:
|
|
|
|
result.update(extras_dict)
|
|
|
|
return result
|
2012-09-06 16:30:26 +08:00
|
|
|
|
|
|
|
def do_get_current_prompt(self):
|
2016-11-11 08:23:08 +08:00
|
|
|
# Return the current prompt. We run 'false' to demonstrate how the
|
|
|
|
# prompt shows the command status (#1624).
|
2019-05-05 18:09:25 +08:00
|
|
|
prompt_func, err = run_fish_cmd("functions fish_prompt")
|
2016-11-11 08:23:08 +08:00
|
|
|
result = self.do_get_prompt(
|
|
|
|
'builtin cd "' + initial_wd + '" ; false ; fish_prompt',
|
2019-05-05 18:09:25 +08:00
|
|
|
prompt_func.strip(),
|
|
|
|
{"name": "Current"},
|
|
|
|
)
|
2014-07-30 03:12:32 +08:00
|
|
|
return result
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2014-07-30 03:12:32 +08:00
|
|
|
def do_get_sample_prompt(self, text, extras_dict):
|
2016-11-11 08:23:08 +08:00
|
|
|
# Return the prompt you get from the given text. Extras_dict is a
|
|
|
|
# dictionary whose values get merged in. We run 'false' to demonstrate
|
|
|
|
# how the prompt shows the command status (#1624)
|
2019-05-05 18:09:25 +08:00
|
|
|
cmd = text + '\n builtin cd "' + initial_wd + '" \n false \n fish_prompt\n'
|
2014-07-30 03:12:32 +08:00
|
|
|
return self.do_get_prompt(cmd, text.strip(), extras_dict)
|
2012-09-06 16:30:26 +08:00
|
|
|
|
|
|
|
def parse_one_sample_prompt_hash(self, line, result_dict):
|
2012-09-06 17:03:21 +08:00
|
|
|
# Allow us to skip whitespace, etc.
|
2016-11-11 08:23:08 +08:00
|
|
|
if not line:
|
|
|
|
return True
|
|
|
|
if line.isspace():
|
|
|
|
return True
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-06 17:03:21 +08:00
|
|
|
# Parse a comment hash like '# name: Classic'
|
|
|
|
match = re.match(r"#\s*(\w+?): (.+)", line, re.IGNORECASE)
|
|
|
|
if match:
|
|
|
|
key = match.group(1).strip()
|
|
|
|
value = match.group(2).strip()
|
|
|
|
result_dict[key] = value
|
|
|
|
return True
|
|
|
|
# Skip other hash comments
|
2019-05-05 18:09:25 +08:00
|
|
|
return line.startswith("#")
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2014-07-30 03:12:32 +08:00
|
|
|
def read_one_sample_prompt(self, path):
|
|
|
|
try:
|
2019-05-05 18:09:25 +08:00
|
|
|
with open(path, "rb") as fd:
|
2014-07-30 03:12:32 +08:00
|
|
|
extras_dict = {}
|
|
|
|
# Read one sample prompt from fd
|
|
|
|
function_lines = []
|
|
|
|
parsing_hashes = True
|
2019-05-05 18:09:25 +08:00
|
|
|
unicode_lines = (line.decode("utf-8") for line in fd)
|
2014-10-10 08:51:43 +08:00
|
|
|
for line in unicode_lines:
|
2016-11-11 08:23:08 +08:00
|
|
|
# Parse hashes until parse_one_sample_prompt_hash return
|
|
|
|
# False.
|
2014-07-30 03:12:32 +08:00
|
|
|
if parsing_hashes:
|
2016-11-11 08:23:08 +08:00
|
|
|
parsing_hashes = self.parse_one_sample_prompt_hash(
|
2019-05-05 18:09:25 +08:00
|
|
|
line, extras_dict
|
|
|
|
)
|
2016-11-11 08:23:08 +08:00
|
|
|
# Maybe not we're not parsing hashes, or maybe we already
|
|
|
|
# were not.
|
2014-07-30 03:12:32 +08:00
|
|
|
if not parsing_hashes:
|
|
|
|
function_lines.append(line)
|
2019-05-05 18:09:25 +08:00
|
|
|
func = "".join(function_lines).strip()
|
2014-07-30 03:12:32 +08:00
|
|
|
result = self.do_get_sample_prompt(func, extras_dict)
|
|
|
|
return result
|
|
|
|
except IOError:
|
|
|
|
# Ignore unreadable files, etc.
|
|
|
|
return None
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-06 16:30:26 +08:00
|
|
|
def do_get_sample_prompts_list(self):
|
2014-07-30 03:12:32 +08:00
|
|
|
pool = multiprocessing.pool.ThreadPool(processes=8)
|
|
|
|
|
|
|
|
# Kick off the "Current" meta-sample
|
|
|
|
current_metasample_async = pool.apply_async(self.do_get_current_prompt)
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2012-09-06 17:03:21 +08:00
|
|
|
# Read all of the prompts in sample_prompts
|
2019-05-05 18:09:25 +08:00
|
|
|
paths = glob.iglob("sample_prompts/*.fish")
|
2014-07-30 03:12:32 +08:00
|
|
|
sample_results = pool.map(self.read_one_sample_prompt, paths, 1)
|
|
|
|
|
|
|
|
# Finish up
|
|
|
|
result = []
|
|
|
|
result.append(current_metasample_async.get())
|
|
|
|
result.extend([r for r in sample_results if r])
|
2012-09-06 17:03:21 +08:00
|
|
|
return result
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2014-10-05 12:19:57 +08:00
|
|
|
def do_get_abbreviations(self):
|
2018-09-09 12:34:54 +08:00
|
|
|
# Example abbreviation line:
|
|
|
|
# abbr -a -U -- ls 'ls -a'
|
|
|
|
result = []
|
2019-05-05 18:09:25 +08:00
|
|
|
out, err = run_fish_cmd("abbr --show")
|
|
|
|
for line in out.rstrip().split("\n"):
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
_, abbr = line.split(" -- ", 1)
|
|
|
|
word, phrase = abbr.split(" ", 1)
|
|
|
|
result.append({"word": word, "phrase": phrase})
|
2014-10-05 12:19:57 +08:00
|
|
|
return result
|
|
|
|
|
2014-10-17 10:27:13 +08:00
|
|
|
def do_remove_abbreviation(self, abbreviation):
|
2019-05-05 18:09:25 +08:00
|
|
|
out, err = run_fish_cmd("abbr --erase %s" % abbreviation["word"])
|
2016-12-05 07:44:57 +08:00
|
|
|
if err:
|
2014-10-17 10:27:13 +08:00
|
|
|
return err
|
|
|
|
else:
|
2016-12-05 07:44:57 +08:00
|
|
|
return None
|
2014-10-17 10:27:13 +08:00
|
|
|
|
|
|
|
def do_save_abbreviation(self, abbreviation):
|
2019-05-05 18:09:25 +08:00
|
|
|
out, err = run_fish_cmd(
|
|
|
|
"abbr --add '%s' '%s'" % (abbreviation["word"], abbreviation["phrase"])
|
|
|
|
)
|
2016-12-05 07:44:57 +08:00
|
|
|
if err:
|
|
|
|
return err
|
2014-10-17 10:27:13 +08:00
|
|
|
else:
|
2016-12-05 07:44:57 +08:00
|
|
|
return None
|
2014-10-17 10:27:13 +08:00
|
|
|
|
2014-08-12 08:50:56 +08:00
|
|
|
def secure_startswith(self, haystack, needle):
|
|
|
|
if len(haystack) < len(needle):
|
|
|
|
return False
|
|
|
|
bits = 0
|
2016-11-11 08:23:08 +08:00
|
|
|
for x, y in zip(haystack, needle):
|
2014-08-12 08:50:56 +08:00
|
|
|
bits |= ord(x) ^ ord(y)
|
|
|
|
return bits == 0
|
2016-08-04 17:03:22 +08:00
|
|
|
|
2012-09-06 16:30:26 +08:00
|
|
|
def font_size_for_ansi_prompt(self, prompt_demo_ansi):
|
2012-09-06 17:03:21 +08:00
|
|
|
width = ansi_prompt_line_width(prompt_demo_ansi)
|
|
|
|
# Pick a font size
|
2016-11-11 08:23:08 +08:00
|
|
|
if width >= 70:
|
2019-05-05 18:09:25 +08:00
|
|
|
font_size = "8pt"
|
2016-11-11 08:23:08 +08:00
|
|
|
if width >= 60:
|
2019-05-05 18:09:25 +08:00
|
|
|
font_size = "10pt"
|
2016-11-11 08:23:08 +08:00
|
|
|
elif width >= 50:
|
2019-05-05 18:09:25 +08:00
|
|
|
font_size = "11pt"
|
2016-11-11 08:23:08 +08:00
|
|
|
elif width >= 40:
|
2019-05-05 18:09:25 +08:00
|
|
|
font_size = "13pt"
|
2016-11-11 08:23:08 +08:00
|
|
|
elif width >= 30:
|
2019-05-05 18:09:25 +08:00
|
|
|
font_size = "15pt"
|
2016-11-11 08:23:08 +08:00
|
|
|
elif width >= 25:
|
2019-05-05 18:09:25 +08:00
|
|
|
font_size = "16pt"
|
2016-11-11 08:23:08 +08:00
|
|
|
elif width >= 20:
|
2019-05-05 18:09:25 +08:00
|
|
|
font_size = "17pt"
|
2016-11-11 08:23:08 +08:00
|
|
|
else:
|
2019-05-05 18:09:25 +08:00
|
|
|
font_size = "18pt"
|
2012-09-06 17:03:21 +08:00
|
|
|
return font_size
|
2012-09-06 16:30:26 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
def do_GET(self):
|
|
|
|
p = self.path
|
2014-08-04 13:34:26 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
authpath = "/" + authkey
|
2014-08-12 08:50:56 +08:00
|
|
|
if self.secure_startswith(p, authpath):
|
2019-05-05 18:09:25 +08:00
|
|
|
p = p[len(authpath) :]
|
2014-08-04 13:34:26 +08:00
|
|
|
else:
|
|
|
|
return self.send_error(403)
|
|
|
|
self.path = p
|
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
if p == "/colors/":
|
2012-05-09 08:10:38 +08:00
|
|
|
output = self.do_get_colors()
|
2019-05-05 18:09:25 +08:00
|
|
|
elif p == "/functions/":
|
2012-05-09 08:10:38 +08:00
|
|
|
output = self.do_get_functions()
|
2019-05-05 18:09:25 +08:00
|
|
|
elif p == "/variables/":
|
2012-05-09 08:10:38 +08:00
|
|
|
output = self.do_get_variables()
|
2019-05-05 18:09:25 +08:00
|
|
|
elif p == "/history/":
|
2012-07-27 15:31:00 +08:00
|
|
|
# start = time.time()
|
2012-05-09 08:10:38 +08:00
|
|
|
output = self.do_get_history()
|
2012-07-27 15:31:00 +08:00
|
|
|
# end = time.time()
|
|
|
|
# print "History: ", end - start
|
2019-05-05 18:09:25 +08:00
|
|
|
elif p == "/sample_prompts/":
|
2012-09-06 17:03:21 +08:00
|
|
|
output = self.do_get_sample_prompts_list()
|
2012-05-09 08:10:38 +08:00
|
|
|
elif re.match(r"/color/(\w+)/", p):
|
|
|
|
name = re.match(r"/color/(\w+)/", p).group(1)
|
|
|
|
output = self.do_get_color_for_variable(name)
|
2019-05-05 18:09:25 +08:00
|
|
|
elif p == "/bindings/":
|
2013-10-13 21:06:46 +08:00
|
|
|
output = self.do_get_bindings()
|
2019-05-05 18:09:25 +08:00
|
|
|
elif p == "/abbreviations/":
|
2014-10-05 12:19:57 +08:00
|
|
|
output = self.do_get_abbreviations()
|
2012-05-09 08:10:38 +08:00
|
|
|
else:
|
|
|
|
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
# Return valid output
|
|
|
|
self.send_response(200)
|
2019-05-05 18:09:25 +08:00
|
|
|
self.send_header("Content-type", "application/json")
|
2014-10-09 18:43:57 +08:00
|
|
|
self.end_headers()
|
2019-05-05 18:09:25 +08:00
|
|
|
self.write_to_wfile("\n")
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
# Output JSON
|
2012-07-27 15:31:00 +08:00
|
|
|
self.write_to_wfile(json.dumps(output))
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
def do_POST(self):
|
|
|
|
p = self.path
|
2014-08-04 13:34:26 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
authpath = "/" + authkey
|
2014-08-12 08:50:56 +08:00
|
|
|
if self.secure_startswith(p, authpath):
|
2019-05-05 18:09:25 +08:00
|
|
|
p = p[len(authpath) :]
|
2014-08-04 13:34:26 +08:00
|
|
|
else:
|
|
|
|
return self.send_error(403)
|
|
|
|
self.path = p
|
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
ctype, pdict = cgi.parse_header(self.headers["content-type"])
|
2012-08-20 04:19:07 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
if ctype == "multipart/form-data":
|
2012-05-09 08:10:38 +08:00
|
|
|
postvars = cgi.parse_multipart(self.rfile, pdict)
|
2019-05-05 18:09:25 +08:00
|
|
|
elif ctype == "application/x-www-form-urlencoded":
|
|
|
|
length = int(self.headers["content-length"])
|
|
|
|
url_str = self.rfile.read(length).decode("utf-8")
|
2015-01-30 22:52:07 +08:00
|
|
|
postvars = parse_qs(url_str, keep_blank_values=1)
|
2019-05-05 18:09:25 +08:00
|
|
|
elif ctype == "application/json":
|
|
|
|
length = int(self.headers["content-length"])
|
|
|
|
url_str = self.rfile.read(length).decode(pdict["charset"])
|
2014-10-17 10:27:13 +08:00
|
|
|
postvars = json.loads(url_str)
|
2012-05-09 08:10:38 +08:00
|
|
|
else:
|
|
|
|
postvars = {}
|
2012-06-05 22:25:11 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
if p == "/set_color/":
|
|
|
|
what = postvars.get("what")
|
|
|
|
color = postvars.get("color")
|
|
|
|
background_color = postvars.get("background_color")
|
|
|
|
bold = postvars.get("bold")
|
|
|
|
italics = postvars.get("italics")
|
|
|
|
reverse = postvars.get("reverse")
|
|
|
|
dim = postvars.get("dim")
|
|
|
|
underline = postvars.get("underline")
|
2012-06-05 22:25:11 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
if what:
|
|
|
|
# Not sure why we get lists here?
|
2016-11-11 08:23:08 +08:00
|
|
|
output = self.do_set_color_for_variable(
|
2019-05-05 18:09:25 +08:00
|
|
|
what[0],
|
|
|
|
color[0],
|
|
|
|
background_color[0],
|
|
|
|
parse_bool(bold[0]),
|
|
|
|
parse_bool(underline[0]),
|
|
|
|
parse_bool(italics[0]),
|
|
|
|
parse_bool(dim[0]),
|
|
|
|
parse_bool(reverse[0]),
|
|
|
|
)
|
2012-05-09 08:10:38 +08:00
|
|
|
else:
|
2019-05-05 18:09:25 +08:00
|
|
|
output = "Bad request"
|
|
|
|
elif p == "/get_function/":
|
|
|
|
what = postvars.get("what")
|
2012-05-09 08:10:38 +08:00
|
|
|
output = [self.do_get_function(what[0])]
|
2019-05-05 18:09:25 +08:00
|
|
|
elif p == "/delete_history_item/":
|
|
|
|
what = postvars.get("what")
|
2012-07-27 15:31:00 +08:00
|
|
|
if self.do_delete_history_item(what[0]):
|
2012-08-20 03:55:50 +08:00
|
|
|
output = ["OK"]
|
2012-07-27 15:31:00 +08:00
|
|
|
else:
|
2012-08-20 03:55:50 +08:00
|
|
|
output = ["Unable to delete history item"]
|
2019-05-05 18:09:25 +08:00
|
|
|
elif p == "/set_prompt/":
|
|
|
|
what = postvars.get("fish_prompt")
|
2015-01-30 22:54:11 +08:00
|
|
|
if self.do_set_prompt_function(what):
|
2012-09-06 17:03:21 +08:00
|
|
|
output = ["OK"]
|
|
|
|
else:
|
|
|
|
output = ["Unable to set prompt"]
|
2019-05-05 18:09:25 +08:00
|
|
|
elif p == "/save_abbreviation/":
|
2016-12-05 07:44:57 +08:00
|
|
|
errmsg = self.do_save_abbreviation(postvars)
|
|
|
|
if errmsg:
|
|
|
|
output = [errmsg]
|
2014-10-17 10:27:13 +08:00
|
|
|
else:
|
|
|
|
output = ["OK"]
|
2019-05-05 18:09:25 +08:00
|
|
|
elif p == "/remove_abbreviation/":
|
2016-12-05 07:44:57 +08:00
|
|
|
errmsg = self.do_remove_abbreviation(postvars)
|
|
|
|
if errmsg:
|
|
|
|
output = [errmsg]
|
2014-10-17 10:27:13 +08:00
|
|
|
else:
|
2016-12-05 07:44:57 +08:00
|
|
|
output = ["OK"]
|
2012-05-09 08:10:38 +08:00
|
|
|
else:
|
2014-10-12 16:23:15 +08:00
|
|
|
return self.send_error(404)
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
# Return valid output
|
|
|
|
self.send_response(200)
|
2019-05-05 18:09:25 +08:00
|
|
|
self.send_header("Content-type", "application/json")
|
2014-10-17 10:27:13 +08:00
|
|
|
self.end_headers()
|
2019-05-05 18:09:25 +08:00
|
|
|
self.write_to_wfile("\n")
|
2012-08-20 03:55:50 +08:00
|
|
|
|
2012-05-09 08:10:38 +08:00
|
|
|
# Output JSON
|
2012-07-27 15:31:00 +08:00
|
|
|
self.write_to_wfile(json.dumps(output))
|
2012-03-25 18:00:38 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
def log_request(self, code="-", size="-"):
|
2012-05-09 08:10:38 +08:00
|
|
|
""" Disable request logging """
|
|
|
|
pass
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2014-10-10 09:33:59 +08:00
|
|
|
def log_error(self, format, *args):
|
2019-05-05 18:09:25 +08:00
|
|
|
if format == "code %d, message %s":
|
2014-10-10 09:33:59 +08:00
|
|
|
# This appears to be a send_error() message
|
|
|
|
# We want to include the path
|
|
|
|
(code, msg) = args
|
2019-05-05 18:09:25 +08:00
|
|
|
format = "code %d, message %s, path %s"
|
2014-10-10 09:33:59 +08:00
|
|
|
args = (code, msg, self.path)
|
|
|
|
SimpleHTTPServer.SimpleHTTPRequestHandler.log_error(self, format, *args)
|
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
|
2014-08-04 13:34:26 +08:00
|
|
|
redirect_template_html = """
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="refresh" content="0;URL='%s'" />
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<p><a href="%s">Start the Fish Web config</a></p>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
|
2013-04-16 05:15:47 +08:00
|
|
|
# find fish
|
2019-05-05 18:09:25 +08:00
|
|
|
fish_bin_dir = os.environ.get("__fish_bin_dir")
|
2013-04-16 05:15:47 +08:00
|
|
|
fish_bin_path = None
|
|
|
|
if not fish_bin_dir:
|
2019-05-05 18:09:25 +08:00
|
|
|
print("The $__fish_bin_dir environment variable is not set. " "Looking in $PATH...")
|
2013-07-06 23:39:20 +08:00
|
|
|
# distutils.spawn is terribly broken, because it looks in wd before PATH,
|
2016-05-12 06:07:46 +08:00
|
|
|
# and doesn't actually validate that the file is even executable
|
2019-05-05 18:09:25 +08:00
|
|
|
for p in os.environ["PATH"].split(os.pathsep):
|
|
|
|
proposed_path = os.path.join(p, "fish")
|
2013-07-06 23:39:20 +08:00
|
|
|
if os.access(proposed_path, os.X_OK):
|
|
|
|
fish_bin_path = proposed_path
|
|
|
|
break
|
|
|
|
if not fish_bin_path:
|
|
|
|
print("fish could not be found. Is fish installed correctly?")
|
|
|
|
sys.exit(-1)
|
|
|
|
else:
|
|
|
|
print("fish found at '%s'" % fish_bin_path)
|
2013-04-16 05:15:47 +08:00
|
|
|
|
|
|
|
else:
|
2019-05-05 18:09:25 +08:00
|
|
|
fish_bin_path = os.path.join(fish_bin_dir, "fish")
|
2013-11-09 20:56:44 +08:00
|
|
|
|
2013-04-16 05:15:47 +08:00
|
|
|
if not os.access(fish_bin_path, os.X_OK):
|
2019-05-05 18:09:25 +08:00
|
|
|
print(
|
|
|
|
"fish could not be executed at path '%s'. "
|
|
|
|
"Is fish installed correctly?" % fish_bin_path
|
|
|
|
)
|
2013-07-06 23:39:20 +08:00
|
|
|
sys.exit(-1)
|
2013-04-16 05:15:47 +08:00
|
|
|
FISH_BIN_PATH = fish_bin_path
|
2012-03-15 18:43:45 +08:00
|
|
|
|
2012-09-06 16:30:26 +08:00
|
|
|
# We want to show the demo prompts in the directory from which this was invoked,
|
|
|
|
# so get the current working directory
|
|
|
|
initial_wd = os.getcwd()
|
|
|
|
|
2016-11-11 08:23:08 +08:00
|
|
|
# Make sure that the working directory is the one that contains the script
|
|
|
|
# server file, because the document root is the working directory.
|
2012-03-26 09:38:33 +08:00
|
|
|
where = os.path.dirname(sys.argv[0])
|
|
|
|
os.chdir(where)
|
|
|
|
|
2014-08-04 13:34:26 +08:00
|
|
|
# Generate a 16-byte random key as a hexadecimal string
|
2019-05-05 18:09:25 +08:00
|
|
|
authkey = binascii.b2a_hex(os.urandom(16)).decode("ascii")
|
2014-08-04 13:34:26 +08:00
|
|
|
|
2012-03-26 09:38:33 +08:00
|
|
|
# Try to find a suitable port
|
2012-03-16 18:03:43 +08:00
|
|
|
PORT = 8000
|
2016-11-11 07:43:59 +08:00
|
|
|
HOST = "::" if socket.has_ipv6 else "localhost"
|
2012-03-16 18:03:43 +08:00
|
|
|
while PORT <= 9000:
|
2012-05-09 08:10:38 +08:00
|
|
|
try:
|
|
|
|
Handler = FishConfigHTTPRequestHandler
|
2015-05-18 10:13:50 +08:00
|
|
|
httpd = FishConfigTCPServer((HOST, PORT), Handler)
|
2012-05-09 08:10:38 +08:00
|
|
|
# Success
|
2013-09-09 02:19:43 +08:00
|
|
|
break
|
2012-05-09 08:10:38 +08:00
|
|
|
except socket.error:
|
2012-08-17 16:14:05 +08:00
|
|
|
err_type, err_value = sys.exc_info()[:2]
|
|
|
|
# str(err_value) handles Python3 correctly
|
2019-05-05 18:09:25 +08:00
|
|
|
if "Address already in use" not in str(err_value):
|
2015-05-18 10:13:50 +08:00
|
|
|
print(str(err_value))
|
2012-05-09 08:10:38 +08:00
|
|
|
break
|
|
|
|
PORT += 1
|
2012-03-16 18:03:43 +08:00
|
|
|
|
|
|
|
if PORT > 9000:
|
2012-05-09 08:10:38 +08:00
|
|
|
# Nobody say it
|
2012-06-05 22:25:11 +08:00
|
|
|
print("Unable to find an open port between 8000 and 9000")
|
2012-05-09 08:10:38 +08:00
|
|
|
sys.exit(-1)
|
2012-03-15 18:43:45 +08:00
|
|
|
|
2012-07-28 04:40:43 +08:00
|
|
|
# Get any initial tab (functions, colors, etc)
|
|
|
|
# Just look at the first letter
|
2019-05-05 18:09:25 +08:00
|
|
|
initial_tab = ""
|
2012-07-28 04:40:43 +08:00
|
|
|
if len(sys.argv) > 1:
|
2019-05-05 18:09:25 +08:00
|
|
|
for tab in [
|
|
|
|
"functions",
|
|
|
|
"prompt",
|
|
|
|
"colors",
|
|
|
|
"variables",
|
|
|
|
"history",
|
|
|
|
"bindings",
|
|
|
|
"abbreviations",
|
|
|
|
]:
|
2012-08-20 03:55:50 +08:00
|
|
|
if tab.startswith(sys.argv[1]):
|
2019-05-05 18:09:25 +08:00
|
|
|
initial_tab = "#" + tab
|
2012-08-20 03:55:50 +08:00
|
|
|
break
|
2012-03-26 09:38:33 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
url = "http://localhost:%d/%s/%s" % (PORT, authkey, initial_tab)
|
2014-08-04 13:34:26 +08:00
|
|
|
|
2016-11-11 08:23:08 +08:00
|
|
|
# Create temporary file to hold redirect to real server. This prevents exposing
|
|
|
|
# the URL containing the authentication key on the command line (see
|
|
|
|
# CVE-2014-2914 or https://github.com/fish-shell/fish-shell/issues/1438).
|
2019-05-05 18:09:25 +08:00
|
|
|
if "XDG_CACHE_HOME" in os.environ:
|
|
|
|
dirname = os.path.expanduser(os.path.expandvars("$XDG_CACHE_HOME/fish/"))
|
2014-08-04 13:34:26 +08:00
|
|
|
else:
|
2019-05-05 18:09:25 +08:00
|
|
|
dirname = os.path.expanduser("~/.cache/fish/")
|
2014-08-04 13:34:26 +08:00
|
|
|
|
|
|
|
os.umask(0o0077)
|
|
|
|
try:
|
|
|
|
os.makedirs(dirname, 0o0700)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno == 17:
|
2016-11-11 08:23:08 +08:00
|
|
|
pass
|
2014-08-04 13:34:26 +08:00
|
|
|
else:
|
2016-11-11 08:23:08 +08:00
|
|
|
raise e
|
2014-08-04 13:34:26 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
randtoken = "".join(
|
|
|
|
random.choice(string.ascii_uppercase + string.digits) for _ in range(6)
|
|
|
|
)
|
|
|
|
filename = dirname + "web_config-%s.html" % randtoken
|
2014-08-04 13:34:26 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
f = open(filename, "w")
|
2014-08-04 13:34:26 +08:00
|
|
|
f.write(redirect_template_html % (url, url))
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
# Open temporary file as URL
|
2017-05-28 14:46:25 +08:00
|
|
|
# Use open on macOS >= 10.12.5 to work around #4035.
|
2019-05-05 18:09:25 +08:00
|
|
|
fileurl = "file://" + filename
|
2019-05-01 05:08:11 +08:00
|
|
|
|
|
|
|
esc = get_special_ansi_escapes()
|
2019-05-05 18:09:25 +08:00
|
|
|
print(
|
|
|
|
"Web config started at %s%s%s"
|
|
|
|
% (esc["underline"], fileurl, esc["exit_attribute_mode"])
|
|
|
|
)
|
|
|
|
print("%sHit ENTER to stop.%s" % (esc["bold"], esc["exit_attribute_mode"]))
|
2019-05-01 05:08:11 +08:00
|
|
|
|
2017-05-28 14:46:25 +08:00
|
|
|
if isMacOS10_12_5_OrLater():
|
2019-05-05 18:09:25 +08:00
|
|
|
subprocess.check_call(["open", fileurl])
|
2018-04-03 07:36:11 +08:00
|
|
|
elif is_wsl():
|
2019-05-05 18:09:25 +08:00
|
|
|
subprocess.call(["cmd.exe", "/c", "start %s" % url])
|
2017-05-28 14:46:25 +08:00
|
|
|
else:
|
|
|
|
webbrowser.open(fileurl)
|
2012-03-15 18:43:45 +08:00
|
|
|
|
2012-03-26 09:38:33 +08:00
|
|
|
# Select on stdin and httpd
|
|
|
|
stdin_no = sys.stdin.fileno()
|
2013-04-16 05:15:47 +08:00
|
|
|
try:
|
2013-07-06 23:39:20 +08:00
|
|
|
while True:
|
|
|
|
ready_read = select.select([sys.stdin.fileno(), httpd.fileno()], [], [])
|
|
|
|
if ready_read[0][0] < 1:
|
|
|
|
print("Shutting down.")
|
|
|
|
# Consume the newline so it doesn't get printed by the caller
|
|
|
|
sys.stdin.readline()
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
httpd.handle_request()
|
2013-04-16 05:15:47 +08:00
|
|
|
except KeyboardInterrupt:
|
2013-07-06 23:39:20 +08:00
|
|
|
print("\nShutting down.")
|
2013-04-16 05:15:47 +08:00
|
|
|
|
2014-08-04 13:34:26 +08:00
|
|
|
# Clean up temporary file
|
|
|
|
os.remove(filename)
|