Skip to content

Commit fdea4bd

Browse files
committed
fixed console trimming command
1 parent a4373c5 commit fdea4bd

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

imgui-core/src/main/kotlin/imgui/demo/showExampleApp/Console.kt

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import imgui.ImGui.setScrollHereY
3535
import imgui.ImGui.smallButton
3636
import imgui.ImGui.style
3737
import imgui.ImGui.textEx
38+
import imgui.ImGui.textUnformatted
3839
import imgui.ImGui.textWrapped
3940
import imgui.classes.InputTextCallbackData
4041
import imgui.classes.TextFilter
@@ -74,7 +75,7 @@ object Console {
7475
fun clearLog() = items.clear()
7576

7677
fun addLog(fmt: String, vararg args: Any) {
77-
items.add(fmt.format(*args))
78+
items += fmt.format(*args)
7879
}
7980

8081
fun draw(title: String, pOpen: KMutableProperty0<Boolean>) {
@@ -159,7 +160,7 @@ object Console {
159160
pushStyleColor(Col.Text, Vec4(1f, 0.8f, 0.6f, 1f))
160161
popColor = true
161162
}
162-
textEx(i)
163+
textUnformatted(i)
163164
if (popColor)
164165
popStyleColor()
165166
}
@@ -175,15 +176,10 @@ object Console {
175176

176177
var reclaimFocus = false
177178
if (inputText("Input", inputBuf, Itf.EnterReturnsTrue.i or Itf.CallbackCompletion.i or Itf.CallbackHistory.i, textEditCallbackStub, this)) {
178-
TODO()
179-
// val slen = inputBuf.textStr(inputBuf)
180-
// val s = String(inputBuf.copyOf(slen)).split(" ")[0]
181-
// if (s.isNotEmpty())
182-
// execCommand(s)
183-
// for (i in 0 until slen)
184-
// inputBuf[i] = NUL
185-
//
186-
// reclaimFocus = true
179+
val s = String(inputBuf).trimEnd()
180+
if(s.isNotEmpty())
181+
execCommand(s)
182+
reclaimFocus = true
187183
}
188184

189185
setItemDefaultFocus()
@@ -194,27 +190,29 @@ object Console {
194190
}
195191

196192
fun execCommand(cmdLine: String) {
197-
addLog("# %s\n", cmdLine)
193+
addLog("# $cmdLine\n")
198194

195+
// Insert into history. First find match and delete it so it can be pushed to the back. This isn't trying to be smart or optimal.
199196
historyPos = -1
200-
history.remove(cmdLine) //could be at any pos, we only want it to be last. so we remove the current instance
201-
history.add(cmdLine)
197+
history -= cmdLine
198+
history += cmdLine
202199

203-
when (cmdLine) {
200+
// Process command
201+
when (cmdLine.toUpperCase()) {
204202
"CLEAR" -> clearLog()
205203
"HELP" -> {
206204
addLog("Commands:")
207-
commands.forEach { addLog("- %s", it) }
205+
commands.forEach { addLog("- $it") }
208206
}
209207
"HISTORY" -> {
210208
val first = history.size - 10
211209
for (i in (if (first > 0) first else 0) until history.size)
212-
addLog("%3d: %s\n", i, history[i])
210+
addLog("%3d: ${history[i]}\n", i)
213211
}
214-
else -> addLog("Unknown command: '%s'\n", cmdLine)
212+
else -> addLog("Unknown command: '$cmdLine'\n")
215213
}
216214

217-
// On commad input, we scroll to bottom even if AutoScroll==false
215+
// On command input, we scroll to bottom even if AutoScroll==false
218216
scrollToBottom = true
219217
}
220218

imgui-core/src/main/kotlin/imgui/internal/api/widgets.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal interface widgets {
4242
* B) it's faster, no memory copy is done, no buffer size limits, recommended for long chunks of text. */
4343
fun textEx(text: String, textEnd: Int = -1, flag: TextFlag = TextFlag.None) {
4444
val bytes = text.toByteArray()
45-
textEx(bytes, if (textEnd != -1) textEnd else bytes.size)
45+
textEx(bytes, if (textEnd != -1) textEnd else bytes.strlen())
4646
}
4747

4848
/** Raw text without formatting. Roughly equivalent to text("%s", text) but:

0 commit comments

Comments
 (0)