thqby / ahk2_lib

MIT License
233 stars 32 forks source link

[child_process.ahk] Strange line wrapping bug #38

Closed skygate2012 closed 9 months ago

skygate2012 commented 9 months ago

This is strange because I was unable to reproduce it with other command line outputs, only that of adb, and it seems to occur randomly. For example, when I call ls for a folder through adb shell, the directory names are wrapped to the next line randomly.

#Requires AutoHotkey v2.0
#SingleInstance Force
Proc := child_process("adb shell ls /sdcard/Android/media", , true)
Proc.Call()
Result := RTrim(Proc.GetLastStdout(), "`r`n")
Msgbox Result

The results below are all produced with the same code above: 1.

br.com.escolhatecnologia.vozdonarrador
ch.blinkenlights.android.vanilla
com.cxinventor.file.explorer
com.foxdebug.acode
com.google.android.gms
com.kiriengine.app
com.lyonbros.turtl
com.nextcloud.client
com.vson.lexie
dk.tacit.android.foldersync.lite
is.xyz.mpv
org.telegram.messenger

2.

br.com.escolha
tecnologia.vozdonarrador
ch.blinkenlights.android.vanilla
com.cxinventor.file.explorer
com.foxdebug.acode
com.google.android.gms
com.kiriengine.app
com.lyonbros.turtl
com.nextcloud.client
com.vson.lexie
dk.tacit.android.foldersync.lite
is.xyz.mpv
org.telegram.messenger

3.

br.com.escolha
tecnologia.vozdonarrador
ch.blinkenlights.android.va
nilla
com.cxinventor.file.explorer
com.foxdebug.acode
com.google.android.gms
com.kiriengine.app
com.lyonbros.turtl
com.nextcloud.client
com.vson.lexie
dk.tacit.android.foldersync.lite
is.xyz.mpv
org.telegram.messenger
skygate2012 commented 8 months ago

@thqby Thank you so much for the fix. :) Can you also have a look at Socket.ahk? It appears to have a similar problem when receiving text, though I'm not 100% sure yet if it's server-side issue.

thqby commented 8 months ago

Socket messages have no boundaries, and when you receive a message, there may be a part of it that arrives later.

skygate2012 commented 8 months ago

Perhaps add the option to define boundaries like \r\n?

thqby commented 8 months ago

Use RecvLine to receive the message

https://www.autohotkey.com/boards/viewtopic.php?p=503678#p503678 This is the jsonrpc implementation

Each message is formatted like Content-Length: 9\r\n{"key":1}.

skygate2012 commented 8 months ago

Thanks. :)

skygate2012 commented 5 months ago

@thqby Hi again, I just noticed regarding child_process.ahk, it's not wrapping text randomly now but sometimes it cuts off one or two characters in the beginning.

thqby commented 5 months ago

what?

skygate2012 commented 5 months ago

After some further testing, it seems whenever the 0 character is present it will sometimes be dropped. It probably has something to do with relying on truthy/falsy for getting stdout?

Try this:

#Requires AutoHotkey v2.0
#SingleInstance Force
Loop {
    Proc := child_process("adb shell ls /sdcard/test", , true)
    Proc.Call()
    Result := Proc.GetLastStdout()
    Tooltip Result
}

If there is a file with a name like 0001000 under /sdcard/test, the zero will be randomly missing either from the beginning or the end, but the 1 always stays.

thqby commented 5 months ago

I don't have adb, so use cmd.exe /c dir /b, but can't reproduce it.

skygate2012 commented 5 months ago

Yes this one only happens in adb as well. There might be others sharing the same problem but I haven't found it yet. I can confirm though that adb is working properly if I use ComObject("WScript.Shell").

thqby commented 5 months ago

Maybe it's fixed. https://github.com/thqby/ahk2_lib/commit/4ad45656027efc7581774a6117a42f2990313980

skygate2012 commented 5 months ago

I thought that would fix it, but it did not. Then I found there is another check relying on a falsy condition:

else if !line
    break

Changing this to also check for blank string finally solved it.