Skip to content

Commit ce00623

Browse files
committed
widget: configure: tokenize instead of splitting with ' '
1 parent 2d926c1 commit ce00623

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

app/src/main/java/org/toni/customfetch_android_lib/Main.kt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,43 @@ fun mainRenderStr(context: Context, argsStr: String): String {
187187
return parseArgs(context, args, config)
188188
}
189189

190+
private fun tokenizeUserCommand(input: String): Array<String> {
191+
val tokens = mutableListOf<String>()
192+
val current = StringBuilder()
193+
var inSingleQuote = false
194+
var inDoubleQuote = false
195+
var escaped = false
196+
197+
for (char in input) {
198+
when {
199+
escaped -> {
200+
current.append(char) // Always keep the escaped character
201+
escaped = false
202+
}
203+
char == '\\' -> escaped = true
204+
char == '\'' && !inDoubleQuote -> inSingleQuote = !inSingleQuote
205+
char == '"' && !inSingleQuote -> inDoubleQuote = !inDoubleQuote
206+
char.isWhitespace() && !inSingleQuote && !inDoubleQuote -> {
207+
if (current.isNotEmpty()) {
208+
tokens.add(current.toString())
209+
current.clear()
210+
}
211+
}
212+
else -> current.append(char)
213+
}
214+
}
215+
216+
if (current.isNotEmpty()) {
217+
tokens.add(current.toString())
218+
}
219+
220+
return tokens.toTypedArray()
221+
}
222+
190223
fun mainRender(context: Context, appWidgetId: Int, argsStr: String): List<SpannableStringBuilder> {
191224
createConfig()
192-
val args = argsStr.split(' ').toTypedArray()
225+
226+
val args = tokenizeUserCommand(argsStr)
193227
val toml = Toml {
194228
ignoreUnknownKeys = true
195229
}

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<resources>
22
<string name="app_name">Customfetch Widget</string>
33
<string name="appwidget_text">EXAMPLE</string>
4-
<string name="configure">Configure\n(insert command line options)</string>
4+
<string name="configure">Configure (insert command-line like options)\nThese options are space divided, thus use quotes \"\" for strings with spaces and escape with \\</string>
55
<string name="additional_truncate">Additional truncate text width\n(insert number bigger than <b>0.20</b>, or smaller to ignore)</string>
66
<string name="add_widget">Add widget</string>
77
<string name="app_widget_description">Neofetch like program, which its focus point is the performance and customizability</string>

0 commit comments

Comments
 (0)