Drew reminds that humans like sorted drop downs #4339
This commit is contained in:
parent
f3f6fe288c
commit
cede67c5e5
Binary file not shown.
|
@ -3,6 +3,9 @@ package com.rusefi.test;
|
|||
import com.rusefi.VariableRegistry;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.rusefi.VariableRegistry.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
@ -55,4 +58,14 @@ public class VariableRegistryTest {
|
|||
assertEquals("1", registry.get("key_int"));
|
||||
assertEquals("1", registry.get("key_int" + _HEX_SUFFIX));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHumanSorted() {
|
||||
Map<Integer, String> input = new HashMap<>();
|
||||
input.put(0, "NONE");
|
||||
input.put(1, "A");
|
||||
input.put(2, "Z");
|
||||
input.put(3, "N");
|
||||
assertEquals("0=\"NONE\",1=\"A\",3=\"N\",2=\"Z\"", VariableRegistry.getString(input));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,12 +2,14 @@ package com.rusefi;
|
|||
|
||||
import com.devexperts.logging.Logging;
|
||||
import com.rusefi.enum_reader.Value;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
@ -90,12 +92,9 @@ public class VariableRegistry {
|
|||
* @return value>name map for specified enum name.
|
||||
*/
|
||||
@Nullable
|
||||
public TreeMap<Integer, String> resolveEnumValues(EnumsReader enumsReader, String enumName) {
|
||||
private TreeMap<Integer, String> resolveEnumValues(@NotNull EnumsReader.EnumState stringValueMap) {
|
||||
TreeMap<Integer, String> valueNameById = new TreeMap<>();
|
||||
|
||||
EnumsReader.EnumState stringValueMap = enumsReader.getEnums().get(enumName);
|
||||
if (stringValueMap == null)
|
||||
return null;
|
||||
for (Value value : stringValueMap.values()) {
|
||||
if (value.isForceSize())
|
||||
continue;
|
||||
|
@ -113,21 +112,44 @@ public class VariableRegistry {
|
|||
return valueNameById;
|
||||
}
|
||||
|
||||
private static void appendCommaIfNeeded(StringBuilder sb) {
|
||||
if (sb.length() > 0)
|
||||
sb.append(",");
|
||||
}
|
||||
|
||||
public String getEnumOptionsForTunerStudio(EnumsReader enumsReader, String enumName) {
|
||||
TreeMap<Integer, String> valueNameById = resolveEnumValues(enumsReader, enumName);
|
||||
EnumsReader.EnumState stringValueMap = enumsReader.getEnums().get(enumName);
|
||||
if (stringValueMap == null)
|
||||
return null;
|
||||
|
||||
TreeMap<Integer, String> valueNameById = resolveEnumValues(stringValueMap);
|
||||
if (valueNameById == null)
|
||||
return null;
|
||||
|
||||
int maxValue = valueNameById.lastKey();
|
||||
return getString(valueNameById);
|
||||
}
|
||||
|
||||
private static String quote(String string) {
|
||||
return "\"" + string + "\"";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String getString(Map<Integer, String> valueNameById) {
|
||||
TreeMap<Integer, String> humanDropDownSorted = new TreeMap<>((o1, o2) -> {
|
||||
if (o1.intValue() == o2)
|
||||
return 0;
|
||||
if (o1.intValue() == 0)
|
||||
return -1; // "None" always go first
|
||||
if (o2.intValue()==0)
|
||||
return 1;
|
||||
return valueNameById.get(o1).compareTo(valueNameById.get(o2));
|
||||
});
|
||||
humanDropDownSorted.putAll(valueNameById);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
// todo: TS enum key-value form #4232
|
||||
for (int i = 0; i <= maxValue; i++) {
|
||||
if (sb.length() > 0)
|
||||
sb.append(", ");
|
||||
|
||||
String value = valueNameById.getOrDefault(i, INVALID);
|
||||
sb.append("\"" + value + "\"");
|
||||
for (Map.Entry<Integer, String> e : humanDropDownSorted.entrySet()) {
|
||||
appendCommaIfNeeded(sb);
|
||||
sb.append(e.getKey() + "=" + quote(e.getValue()));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue