Webconfig: Pass colorscheme in one json request

This used to pass each color in a separate url-encoded request, which is
just wasteful.

Also it passed separate parameters for modifiers like bold and
underlined, but never gave them actual values. Instead the color is
passed as one string.

So we just use json, and then iterate over it server-side.
This commit is contained in:
Fabian Homborg 2021-07-09 18:24:41 +02:00
parent 32826d3596
commit 2aec6e5814
2 changed files with 26 additions and 45 deletions

View File

@ -143,6 +143,10 @@ controllers.controller("colorsController", function($scope, $http) {
"fish_pager_color_progress" "fish_pager_color_progress"
]; ];
var remaining = settingNames.length; var remaining = settingNames.length;
postdata = {
"theme" : $scope.selectedColorScheme["name"],
"colors": [],
}
for (name of settingNames) { for (name of settingNames) {
var selected; var selected;
// Skip colors undefined in the current theme // Skip colors undefined in the current theme
@ -157,17 +161,16 @@ controllers.controller("colorsController", function($scope, $http) {
} else { } else {
selected = $scope.selectedColorScheme[name]; selected = $scope.selectedColorScheme[name];
} }
var postData = "what=" + name + "&color=" + selected + "&background_color=&bold=&underline=&dim=&reverse=&italics="; postdata.colors.push({
$http.post("set_color/", postData, { headers: {'Content-Type': 'application/x-www-form-urlencoded'} }).then(function(arg) { "what" : name,
"color" : selected,
});
}
$http.post("set_color/", postdata, { headers: {'Content-Type': 'application/json'} }).then(function(arg) {
if (arg.status == 200) { if (arg.status == 200) {
remaining -= 1;
if (remaining == 0) {
/* All styles set! */
$scope.saveThemeButtonTitle = "Theme Set!"; $scope.saveThemeButtonTitle = "Theme Set!";
} }
}
}) })
}
}; };
$scope.getCurrentTheme(); $scope.getCurrentTheme();

View File

@ -1075,9 +1075,11 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
return out return out
def do_set_color_for_variable( def do_set_color_for_variable(
self, name, color, background_color, bold, underline, italics, dim, reverse self, name, color
): ):
"Sets a color for a fish color name, like 'autosuggestion'" "Sets a color for a fish color name, like 'autosuggestion'"
if not name:
raise ValueError
if not color: if not color:
color = "normal" color = "normal"
varname = "fish_color_" + name varname = "fish_color_" + name
@ -1087,20 +1089,7 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
varname = name varname = name
# TODO: Check if the varname is allowable. # TODO: Check if the varname is allowable.
command = "set -U " + varname command = "set -U " + varname
if color:
command += " " + color command += " " + color
if background_color:
command += " --background=" + background_color
if bold:
command += " --bold"
if underline:
command += " --underline"
if italics:
command += " --italics"
if dim:
command += " --dim"
if reverse:
command += " --reverse"
out, err = run_fish_cmd(command) out, err = run_fish_cmd(command)
return out return out
@ -1368,26 +1357,15 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
postvars = {} postvars = {}
if p == "/set_color/": if p == "/set_color/":
what = postvars.get("what") print("# Colorscheme: " + postvars.get("theme"))
color = postvars.get("color") for item in postvars.get("colors"):
background_color = postvars.get("background_color") what = item.get("what")
bold = postvars.get("bold") color = item.get("color")
italics = postvars.get("italics")
reverse = postvars.get("reverse")
dim = postvars.get("dim")
underline = postvars.get("underline")
if what: if what:
# Not sure why we get lists here?
output = self.do_set_color_for_variable( output = self.do_set_color_for_variable(
what[0], what,
color[0], color,
background_color[0],
parse_bool(bold[0]),
parse_bool(underline[0]),
parse_bool(italics[0]),
parse_bool(dim[0]),
parse_bool(reverse[0]),
) )
else: else:
output = "Bad request" output = "Bad request"