Programmatic formatting for the config header

This commit is contained in:
Vhati 2017-12-08 14:56:24 -05:00
parent 020ecc4145
commit 7949e1ac3d

View file

@ -5,11 +5,25 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
public class SlipstreamConfig { public class SlipstreamConfig {
public static final String ALLOW_ZIP = "allow_zip";
public static final String FTL_DATS_PATH = "ftl_dats_path";
public static final String RUN_STEAM_FTL = "run_steam_ftl";
public static final String NEVER_RUN_FTL = "never_run_ftl";
public static final String UPDATE_CATALOG = "update_catalog";
public static final String UPDATE_APP = "update_app";
public static final String USE_DEFAULT_UI = "use_default_ui";
public static final String REMEMBER_GEOMETRY = "remember_geometry";
public static final String MANAGER_GEOMETRY = "manager_geometry";
private Properties config; private Properties config;
private File configFile; private File configFile;
@ -60,21 +74,40 @@ public class SlipstreamConfig {
OutputStream out = null; OutputStream out = null;
try { try {
out = new FileOutputStream( configFile ); out = new FileOutputStream( configFile );
String configComments = "";
configComments += "\n"; Map<String, String> userFieldsMap = new LinkedHashMap<String, String>();
configComments += " allow_zip - Sets whether to treat .zip files as .ftl files. Default: false.\n"; Map<String, String> appFieldsMap = new LinkedHashMap<String, String>();
configComments += " ftl_dats_path - The path to FTL's resources folder. If invalid, you'll be prompted.\n";
configComments += " run_steam_ftl - If true, SMM will use Steam to launch FTL, if possible. Default: false.\n"; userFieldsMap.put( ALLOW_ZIP, "Sets whether to treat .zip files as .ftl files. Default: false." );
configComments += " never_run_ftl - If true, there will be no offer to run FTL after patching. Default: false.\n"; userFieldsMap.put( FTL_DATS_PATH, "The path to FTL's resources folder. If invalid, you'll be prompted." );
configComments += " update_catalog - If a number greater than 0, check for new mod descriptions every N days.\n"; userFieldsMap.put( RUN_STEAM_FTL, "If true, SMM will use Steam to launch FTL, if possible. Default: false." );
configComments += " update_app - If a number greater than 0, check for newer app versions every N days.\n"; userFieldsMap.put( NEVER_RUN_FTL, "If true, there will be no offer to run FTL after patching. Default: false." );
configComments += " use_default_ui - If true, no attempt will be made to resemble a native GUI. Default: false.\n"; userFieldsMap.put( UPDATE_CATALOG, "If a number greater than 0, check for new mod descriptions every N days." );
configComments += " remember_geometry - If true, window geometry will be saved on exit and restored on startup.\n"; userFieldsMap.put( UPDATE_APP, "If a number greater than 0, check for newer app versions every N days." );
configComments += "\n"; userFieldsMap.put( USE_DEFAULT_UI, "If true, no attempt will be made to resemble a native GUI. Default: false." );
configComments += " manager_geometry - Last saved position/size/etc of the main window.\n"; userFieldsMap.put( REMEMBER_GEOMETRY, "If true, window geometry will be saved on exit and restored on startup." );
appFieldsMap.put( MANAGER_GEOMETRY, "Last saved position/size/etc of the main window." );
List<String> allFieldsList = new ArrayList<String>( userFieldsMap.size() + appFieldsMap.size() );
allFieldsList.addAll( userFieldsMap.keySet() );
allFieldsList.addAll( userFieldsMap.keySet() );
int fieldWidth = 0;
for ( String fieldName : allFieldsList ) {
fieldWidth = Math.max( fieldName.length(), fieldWidth );
}
StringBuilder commentsBuf = new StringBuilder( "\n" );
for ( Map.Entry<String, String> entry : userFieldsMap.entrySet() ) {
commentsBuf.append( String.format( " %-"+ fieldWidth +"s - %s\n", entry.getKey(), entry.getValue() ) );
}
commentsBuf.append( "\n" );
for ( Map.Entry<String, String> entry : appFieldsMap.entrySet() ) {
commentsBuf.append( String.format( " %-"+ fieldWidth +"s - %s\n", entry.getKey(), entry.getValue() ) );
}
OutputStreamWriter writer = new OutputStreamWriter( out, "UTF-8" ); OutputStreamWriter writer = new OutputStreamWriter( out, "UTF-8" );
config.store( writer, configComments ); config.store( writer, commentsBuf.toString() );
writer.flush(); writer.flush();
} }
finally { finally {