mergeSettingsScalars function
Replaces or inserts theme: and last_selected_device_id: lines while
preserving the rest of the file (plugins, comments).
Implementation
String mergeSettingsScalars(
String content, {
required String themeName,
required String? lastSelectedDeviceId,
}) {
final deviceValue = lastSelectedDeviceId ?? '~';
final lines = content.split('\n');
var themeReplaced = false;
var deviceReplaced = false;
for (var i = 0; i < lines.length; i++) {
final line = lines[i];
if (line.startsWith('theme:')) {
lines[i] = 'theme: $themeName';
themeReplaced = true;
} else if (line.startsWith('last_selected_device_id:')) {
lines[i] = 'last_selected_device_id: $deviceValue';
deviceReplaced = true;
}
}
if (themeReplaced && deviceReplaced) {
return lines.join('\n');
}
final insertAt = _insertIndexAfterHeader(lines);
final toInsert = <String>[];
if (!themeReplaced) toInsert.add('theme: $themeName');
if (!deviceReplaced) {
toInsert.add('last_selected_device_id: $deviceValue');
}
lines.insertAll(insertAt, toInsert);
return lines.join('\n');
}