Adam shares Windows console secrets - DO NOT USE chcp!!
Posted 2019-11-25
Blog
Articles- terminal.d gets clipboard functions, ldc 1.20 out.
- DConf keynote speaker announced: Lua architect Roberto Ierusalimschy, Named args DIP discussed
- February 3, 2020
- Adam's terminal suite explained
- Understanding mixin templates, terminal.d improvements
- My attribute-by-default proposal. Also dmd 2.090 came out.
- DConf 2020 announced: June 17-20 in London. @safe by default debated. Adam did: Android, JNI, WebSocket in arsd libs
- tar.xz, --DRT tip, dom bug fixes, more Android and JNI, link to old phobos docs
- LDC 1.19 - Android, AVR. My rant on tests, update on JNI and COM.
- Walter's string interpolation proposal is OK but not great. My Android thing nearing beta release. dub downtime explained.
- Android project update, introduction to arsd.jni
- New pattern about interface contracts
- Adam shares Windows console secrets - DO NOT USE chcp!!
- Adam's rant on benchmarks
- Socket tutorial
- November 4, 2019
- October 28, 2019
- arsd package updates, forum nonsense
- Update on Android
- Adam does iOS "goodbye world"
- September 30, 2019
- D turns 20, Adam rants on software freedom
- Named arg DIPs and my thoughts on code organization
- September 9, 2019
- I wrote about mixin templates vs string mixins on Stack Overflow
- August 26, 2019
- Bug bounty in D again - my hot take, on reusing code, a fun picture, my tentative plan for the next month
- Time invested is worth a lot
- cgi.d's new scheduler, static this tricks
- July 29, 2019
- July 22, 2019
- Solving vs managing problems
- A big week in the arsd repo
- July 1, 2019
- June 24, 2019
- June 17, 2019
- CRTP thoughts, named arguments DIP review, DConf videos now on youtube
- musings on hybrid CT/RT tests, some more progress on new web framework
- a little more webassembly
- May 20, 2019
- Adam's string interpolation proposal
- DMD 2.086 live, GCC 9 with D support formally released, DConf coming soon, links to posts on builder pattern and disallowing implicit conversions with templates, and 2d array op overloads
- template constraint error improvements coming?
- dmd 2.086 beta, dstep 1.0 released, Adam works on memory usage
- obj-c and webassembly report, tips on is expressions linked.
- new ldc, new dmd, dpp on the blog
- D's future discussed in forums
- LDC beta, DConf blog link, Adam introduces gamehelpers.d
- March 18, 2019
- LDC 1.15.0-beta1, responsive design rant
- dmd 2.085.0 released
- Obj-C interop and D without druntime code to copy/paste
- dmd beta, more info coming next time, demo of new web framework initial prototype
- automatic web interface discussion, reflection tips and tricks
- Adam busy with weather and a move, lots of community announcements
- January 28, 2019
- Working on official blog 2018 retro, C++ new wrapped, dmd reading zips?
- dmd obj-c growing, Adam static foreaches an interface to RPC
- dmd 2.084, hope for future, but busy non-D week for me
- IDE tools released, my cgi.d gets new features
- DConf announced, tip, Adam rants: mouse trap
- This Week in D is back!
The community announcement about a webassembly proposal is solid, and the author has done some good work toward it already. I'm excited to see where he goes with it!
Meanwhile, I am using an older technology and want to clear up some common misconceptions about it - how to properly program input and output on the Windows (Vista+) console API.
Core D Development Statistics
In the community
Community announcements
See more at the announce forum.
Tip of the Week
Unicode
A common piece of advice about using the Windows console is to chcp 65001 (or its API equivalent, SetConsoleOutputCP) to use UTF-8 on it. While this works to some extent, it brings with it some strange bugs, especially in conjunction with other programs. I worked on a project last week that saw random font changes because it would spawn another process that interact badly with the console state. I do not recommend doing this.
A better way to use it is to sidestep the code page question entirely by using the wide character APIs that Windows provides. Then it will work no matter the console's settings without side effects. The tricky part is these APIs only work on consoles, so if you want console as well as pipe capability, you will need to test and branch.
Let me show you some code:
I actually suggest Phobos do something like this internally since it is a little more sane for the users. My terminal.d uses these wide char functions (though it ALSO sets the utf-8 codepage, I think I am going to remove that in my next commit since it is unnecessary).
Tab completion
ReadConsoleW, since Windows Vista, also has an interesting feature that isn't very well documented (one of the top search results is now a Stack Overflow answer I wrote up myself about 2 1/2 years ago! https://stackoverflow.com/a/43836992/1457000 ) that has some built-in support for tab completion.
ReadConsoleW also uses Windows' built in command history. cmd.exe uses this stuff and your programs can too. You can also modify this, see: https://docs.microsoft.com/en-us/windows/console/setconsolehistoryinfo
Anyway, when using these features, you should also do a isConsole check, but I'll leave that out of the next sample program for brevity.
Windows' built in line editing, history, and completion is a little awkward on the API side as well as on the user side, especially if you are used to things like GNU readline, but it is functional and can be done with relatively little code. There's very close to zero documentation for this on the 'net, so hopefully you find this useful.
Or of course, you can use my arsd.terminal which has a getline function built in which is even easier to use :)
But, to be fair, the Windows function currently works better with some forms of input. I still need to fix some handling of alt+nnnn input and double-wide unicode characters in my library. Such things work automatically in Windows using the native API.