# Keeping track of how many characters written to an IO

**URL:** <https://discourse.julialang.org/t/keeping-track-of-how-many-characters-written-to-an-io/3935>\
**Category:** General Usage\
**Created:** [May 25, 2017, 8:35pm UTC](https://discourse.julialang.org/t/keeping-track-of-how-many-characters-written-to-an-io/3935 "2017-05-25T20:35:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 25, 2017, 8:35pm UTC](https://discourse.julialang.org/t/keeping-track-of-how-many-characters-written-to-an-io/3935/1 "2017-05-25T20:35:51Z")

</div>

I want to create my own `IO` that can also keep track of how many characters that has been written to it. AFAIU, everything written to an `IO` boils down to a call to `Base.unsafe_write(io::IOIndent, p::Ptr{UInt8}, n::UInt)`, however that is a bit too low level to count characters at.

Trying to intercept the writing at a higher level, by for example overloading something like `Base.write(io::MyIO, x)` doesn’t really work because it leads to ambiguities with e.g. `Base.write(io::IO, x::Int)`.

Perhaps just overloading `Base.write(io::MyIO, Union{String, Int64, Int32, ....})` where I list all the types that can be written in `Base` would work? Anyone has any better idea?

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [May 26, 2017, 5:43pm UTC](https://discourse.julialang.org/t/keeping-track-of-how-many-characters-written-to-an-io/3935/2 "2017-05-26T17:43:57Z")

</div>

Why wouldn’t you want to intercept the IO at the most primitive layer? For example, there’s an entire IO object (Core.STDOUT in base/coreio.jl) that consists of just that method. The number of bytes written at that level is simply `n`.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 26, 2017, 7:46pm UTC](https://discourse.julialang.org/t/keeping-track-of-how-many-characters-written-to-an-io/3935/3 "2017-05-26T19:46:46Z")

</div>

Because it is much harder to count **characters** at the lowest level.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [May 26, 2017, 8:57pm UTC](https://discourse.julialang.org/t/keeping-track-of-how-many-characters-written-to-an-io/3935/4 "2017-05-26T20:57:16Z")

</div>

er, yes, I saw that, but you also mentioned overloading `Base.write(io::MyIO, ::Int64)`, which is 8 bytes and no characters.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 26, 2017, 9:49pm UTC](https://discourse.julialang.org/t/keeping-track-of-how-many-characters-written-to-an-io/3935/5 "2017-05-26T21:49:11Z")

</div>

Okay, that was a mistake by me to mention it then. To give some context, I am trying to fix [https://github.com/KristofferC/IOIndents.jl](https://github.com/KristofferC/IOIndents.jl). It is bugged right now 😛

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 26, 2017, 11:28pm UTC](https://discourse.julialang.org/t/keeping-track-of-how-many-characters-written-to-an-io/3935/6 "2017-05-26T23:28:42Z")

</div>

Looking at IOIndents.jl (which looks interesting), I think you really need to keep track of the character widths, not the number of characters output, and also be careful of tab stops (which according to different standards are set to every 8 single width positions by default).  
The Unicode database has information with 6 different widths (full, half, narrow, wide, ambiguous, neutral).
