Skip to content

Instantly share code, notes, and snippets.

@glguy
Created September 9, 2019 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glguy/a802eaaac5a00ab41e3c3ca33507634b to your computer and use it in GitHub Desktop.
Save glguy/a802eaaac5a00ab41e3c3ca33507634b to your computer and use it in GitHub Desktop.
Terminal character widths
module Main where
import System.Console.ANSI
import System.IO
import Data.List
import Data.Traversable
import Graphics.Text.Width (wcwidth)
import Data.Char
import Data.Foldable
import Text.Printf
isAssigned c =
case generalCategory c of
NotAssigned -> False
Surrogate -> False
Control -> False
_ -> True
main :: IO ()
main =
withFile "output.txt" WriteMode $ \h ->
do hSetBuffering stdout NoBuffering
for_ ['\0'..] $ \i ->
hPrint h =<<
if isAssigned i
then charWidth i
else return 1
charWidth :: Char -> IO Int
charWidth c =
do printf "\r%06x: " (ord c)
putChar c
Just (_row, col) <- getCursorPosition0
return (col - 8)
cabal-version: 2.4
name: widths
version: 0.1.0.0
license: ISC
license-file: LICENSE
author: Eric Mertens
maintainer: emertens@gmail.com
extra-source-files: CHANGELOG.md
executable widths
main-is: Main.hs
build-depends: base ^>=4.12.0.0, ansi-terminal ^>=0.10, vty ^>=5.25.1
default-language: Haskell2010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment