Skip to content

Commit

Permalink
Add function Escape.length/1
Browse files Browse the repository at this point in the history
  • Loading branch information
NickNeck committed Feb 15, 2025
1 parent 5540916 commit baca2e8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.3.0 - 2025/02/15

+ Add function `Escape.length/1`.

## 0.2.0 - 2024/12/13

+ Add function `Escape.color_doc/2`.
Expand Down
45 changes: 39 additions & 6 deletions lib/escape.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ defmodule Escape do

import Escape.Sequence
import Inspect.Algebra, only: [is_doc: 1]
import Kernel, except: [length: 1]

alias Inspect.Algebra
alias IO.ANSI
Expand Down Expand Up @@ -262,10 +263,10 @@ defmodule Escape do
* `:theme` a map that adds ANSI codes usable in the chardata-like argument.
The searching in the theme performs a deep search.
* `:reset` appends an `IO.ANSI.reset/0` when true.
* `:reset` appends an `IO.ANSI.reset/0` when true.
Defaults to `true`.
* `:emit` enables or disables emitting ANSI codes.
* `:emit` enables or disables emitting ANSI codes.
Defaults to `IO.ANSI.enabled?/0`.
## Examples
Expand Down Expand Up @@ -364,18 +365,18 @@ defmodule Escape do
end

@doc """
Colors a `Inspect.Algebra` document if the `color_key` has a color in the
Colors a `Inspect.Algebra` document if the `color_key` has a color in the
`theme`.
This function is similar to `Inspect.Algebra.color/3` but has a different
This function is similar to `Inspect.Algebra.color/3` but has a different
options argument.
## Options
* `:theme` a map of ANSI codes. The searching in the theme performs a deep
* `:theme` a map of ANSI codes. The searching in the theme performs a deep
search.
* `:emit` enables or disables emitting ANSI codes.
* `:emit` enables or disables emitting ANSI codes.
Defaults to `IO.ANSI.enabled?/0`.
"""
@spec color_doc(Algebra.t(), ansicode(), keyword()) :: Algebra.t()
Expand All @@ -391,4 +392,36 @@ defmodule Escape do
doc
end
end

@doc """
Returns the length of a string or ansidata without ANSI escape sequences.
## Examples
iex> String.length("Hello, world!")
13
iex> [:green, "Hello", :reset, ", ", :blue, "world!"]
...> |> Escape.format()
...> |> Escape.length()
13
iex> [:green, "Hello", :reset, ", ", :blue, "world!"]
...> |> Escape.format()
...> |> IO.iodata_to_binary()
...> |> Escape.length()
13
"""
@spec length(String.t() | ansidata()) :: non_neg_integer
def length(string_or_ansidata)

def length(string) when is_binary(string) do
string
|> String.replace(~r/\e\[[0-9;]*m/, "")
|> String.length()
end

def length(ansidata) do
ansidata
|> IO.iodata_to_binary()
|> length()
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Escape.MixProject do
def project do
[
app: :escape,
version: "0.2.0",
version: "0.3.0",
elixir: "~> 1.11",
source_url: @source_url,
description: @description,
Expand Down

0 comments on commit baca2e8

Please # to comment.