How can I get Mocha's Unicode output to display properly in a Windows console?

前端 未结 2 1784
予麋鹿
予麋鹿 2021-02-07 22:45

When I run Mocha, it tries to show a check mark or an X for a passing or a failing test run, respectively. I\'ve seen great-looking screenshots of Mocha\'s output. But those scr

2条回答
  •  花落未央
    2021-02-07 23:29

    By pasting the characters into LinqPad, I was able to figure out that they were 'HEAVY CHECK MARK' (U+2714) and 'HEAVY MULTIPLICATION X' (U+2716). It looks like neither character is supported in any of the console fonts (Consolas, Lucida Console, or Raster Fonts) that are available in Windows 7. In fact, out of all the fonts that ship with Windows 7, only a handful support these characters (Meiryo, Meiryo UI, MS Gothic, MS Mincho, MS PGothic, MS PMincho, MS UI Gothic, and Segoe UI Symbol). The ones starting with "MS" are all fixed-width (monospace) fonts, but they all look awful at the font sizes typical of a console. And the others are out, since the console requires fixed-width fonts.

    So you'll need to download a font. I like DejaVu Sans Mono -- it's free, it looks good at console sizes, it's easy to tell the 0 from the O and the 1 from the I from the l, and it's got all kinds of fancy Unicode symbols, including the check and X that Mocha uses.

    Sample of the DejaVu Sans Mono font

    Unfortunately, it's a bit of a pain to install a new console font, but it's doable. (Steps adapted from this post by Scott Hanselman, but extended to include the non-obvious subtleties of 000.)

    Steps:

    1. Download the DejaVu fonts. Unzip the files. Go into the "ttf" directory you just unzipped, select all the files, right-click and "Install".
    2. Run Regedit, and go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont.
    3. Add a new string value. Give it a name that's a string of zeroes one longer than the longest string of zeroes that's already there. For example, on my Windows 7 install, there's already a value named 0 and one named 00, so I had to name the new one 000.
    4. Double-click on your new value, and set its value to DejaVu Sans Mono.
    5. Reboot. (Yes, this step is necessary, at least on OSes up to and including Windows 7.)
    6. Now you can open a console window, open the window menu, go to Defaults > Font tab, and "DejaVu Sans Mono" should be available in the Font list box. Select it and OK.

    Now Mocha's output will display in all its glory.

    Screenshots of Mocha output displaying correctly in a Windows console

提交回复
热议问题