-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added support for custom formatting! You can use the following placeholders: - %title - %artist - %date - %album There are multiple ways to use this feature: 1. using command-line arguments ```--top, -t``` "format string" ```--bottom, -b``` "format string" 2. using a configuration file Make sure it's called "cmusrp.conf", is in the same directory as the jar file and contains the following: ``` TOP_FORMAT="format string goes here" BOTTOM_FORMAT="format string goes here" ```
- Loading branch information
1 parent
1c7ae3c
commit ace393a
Showing
6 changed files
with
183 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
DEBUG=false | ||
QUIET=false | ||
INTERVAL=1000 | ||
TOP_FORMAT="%artist - %title" | ||
BOTTOM_FORMAT="from %album (%date)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package de.mineclashtv.utils; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ConfigurationFile { | ||
|
||
/* | ||
The procedure is as follows: | ||
1. Check if a config file already exists | ||
- if it does, use that; | ||
- otherwise, create one with the default values (DEFAULT_CONFIG string). | ||
2. Parse every line and search for supported keywords. | ||
- These are: DEBUG, QUIET, INTERVAL, TOP_FORMAT, BOTTOM_FORMAT. | ||
3. Put the results from step 2 into a HashMap (CONFIG_MAP) | ||
4. Use the values from the generated HashMap elsewhere | ||
*/ | ||
|
||
private final String DEFAULT_CONFIG = | ||
"DEBUG=false\n" + | ||
"QUIET=false\n" + | ||
"INTERVAL=1000\n" + | ||
"TOP_FORMAT=\"%artist - %title\"\n" + | ||
"BOTTOM_FORMAT=\"from %album (%date)\"\n" | ||
; | ||
private final Map<String, Object> CONFIG_MAP = new HashMap<>(); | ||
|
||
private final Path WORKING_DIR = Paths.get(System.getProperty("user.dir")); | ||
private final Path CONFIG_FILE = Paths.get(WORKING_DIR.toString() + "/cmusrp.conf"); | ||
|
||
public void initialize() throws IOException { | ||
if(!Files.exists(CONFIG_FILE)) | ||
createDefault(CONFIG_FILE); | ||
} | ||
|
||
public void createDefault(Path path) throws IOException { | ||
Files.write(path, DEFAULT_CONFIG.getBytes()); | ||
} | ||
|
||
public Object getValue(String key) throws IOException { | ||
if(this.CONFIG_MAP.isEmpty()) | ||
populateConfigMap(read(CONFIG_FILE)); | ||
|
||
return CONFIG_MAP.get(key); | ||
} | ||
|
||
public void replaceValue(String key, Object value) throws IOException { | ||
if(this.CONFIG_MAP.isEmpty()) | ||
populateConfigMap(read(CONFIG_FILE)); | ||
|
||
this.CONFIG_MAP.replace(key, value); | ||
} | ||
|
||
private String read(Path path) throws IOException { | ||
return new String(Files.readAllBytes(path)); | ||
} | ||
|
||
private void populateConfigMap(String data) { | ||
/* Maybe there is a better way to do this. */ | ||
for(String s : data.split("\n")) { | ||
if(s.startsWith("DEBUG=")) | ||
CONFIG_MAP.put("debug", Boolean.parseBoolean(s.substring(6))); | ||
else if(s.startsWith("QUIET=")) | ||
CONFIG_MAP.put("quiet", Boolean.parseBoolean(s.substring(6))); | ||
else if(s.startsWith("INTERVAL=")) | ||
CONFIG_MAP.put("interval", Integer.parseInt(s.substring(9))); | ||
else if(s.startsWith("TOP_FORMAT=")) | ||
CONFIG_MAP.put("top_format", s.substring(12, s.length() - 1)); | ||
else if(s.startsWith("BOTTOM_FORMAT=")) | ||
CONFIG_MAP.put("bottom_format", s.substring(15, s.length() - 1)); | ||
else | ||
System.out.println("Unknown config '" + s + "'."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters