-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Owl.Data.from_ansidata/1
#11
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
59789c7
(WIP) First pass at `Owl.Data.from_ansidata/1`
zachallaun 9a6c9df
Inline helper only being used in one place
zachallaun d91f51d
More tests for `Owl.Data.from_ansidata/1`
zachallaun 604c982
Update doc for `Owl.Data.from_ansidata/1`
zachallaun 034314c
Test `from_ansidata` with fragments and concatenated sequences
zachallaun f0eb35f
Refactor special cases in Owl.Data.Sequence
zachallaun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
defmodule Owl.Data.Sequence.Helper do | ||
@moduledoc false | ||
|
||
defmacro defsequence_type(name, type, define_name_by_sequence? \\ true) do | ||
quote bind_quoted: [ | ||
name: name, | ||
type: type, | ||
define_name_by_sequence?: define_name_by_sequence? | ||
] do | ||
if define_name_by_sequence? do | ||
seq = apply(IO.ANSI, name, []) | ||
defp name_by_sequence(unquote(seq)), do: unquote(name) | ||
end | ||
|
||
defp type_by_name(unquote(name)), do: unquote(type) | ||
end | ||
end | ||
end | ||
|
||
defmodule Owl.Data.Sequence do | ||
@moduledoc false | ||
|
||
import Owl.Data.Sequence.Helper | ||
|
||
@doc """ | ||
Get the sequence name of an escape sequence or `nil` if not a sequence. | ||
""" | ||
def ansi_to_name(binary) when is_binary(binary) do | ||
case binary do | ||
"\e[38;5;" <> _ -> binary | ||
"\e[48;5;" <> _ -> binary | ||
_ -> name_by_sequence(binary) | ||
end | ||
end | ||
|
||
@doc """ | ||
Get the sequence type of an escape sequence or `nil` if not a sequence. | ||
""" | ||
def ansi_to_type(binary) when is_binary(binary) do | ||
if name = ansi_to_name(binary) do | ||
fuelen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type!(name) | ||
end | ||
end | ||
|
||
@doc """ | ||
Get the sequence type of a sequence name. | ||
""" | ||
def type!(sequence) when is_atom(sequence), do: type_by_name(sequence) | ||
def type!("\e[38;5;" <> _), do: :foreground | ||
def type!("\e[48;5;" <> _), do: :background | ||
|
||
@doc """ | ||
Get the default value of a sequence type. | ||
""" | ||
def default_value_by_type!(:foreground), do: :default_color | ||
def default_value_by_type!(:background), do: :default_background | ||
def default_value_by_type!(:blink), do: :blink_off | ||
def default_value_by_type!(:intensity), do: :normal | ||
def default_value_by_type!(:underline), do: :no_underline | ||
def default_value_by_type!(:italic), do: :not_italic | ||
def default_value_by_type!(:overlined), do: :not_overlined | ||
def default_value_by_type!(:inverse), do: :inverse_off | ||
def default_value_by_type!(:reverse), do: :reverse_off | ||
|
||
defsequence_type(:reset, :reset) | ||
|
||
for color <- [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white] do | ||
defsequence_type(color, :foreground) | ||
defsequence_type(:"light_#{color}", :foreground) | ||
defsequence_type(:"#{color}_background", :background) | ||
defsequence_type(:"light_#{color}_background", :background) | ||
end | ||
|
||
defsequence_type(:default_color, :foreground) | ||
defsequence_type(:default_background, :background) | ||
|
||
defsequence_type(:blink_off, :blink) | ||
defsequence_type(:blink_slow, :blink) | ||
defsequence_type(:blink_rapid, :blink) | ||
|
||
defsequence_type(:normal, :intensity) | ||
defsequence_type(:faint, :intensity) | ||
defsequence_type(:bright, :intensity) | ||
|
||
defsequence_type(:inverse, :inverse) | ||
defsequence_type(:reverse, :reverse, false) | ||
|
||
defsequence_type(:underline, :underline) | ||
defsequence_type(:no_underline, :underline) | ||
|
||
defsequence_type(:italic, :italic) | ||
defsequence_type(:not_italic, :italic) | ||
|
||
defsequence_type(:overlined, :overlined) | ||
defsequence_type(:not_overlined, :overlined) | ||
|
||
defp name_by_sequence(_), do: nil | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at coverage and this line is red. This is for char list, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it’s just to handle individual character literals in ansidata.
[“hello”, \?]
Should’ve added a test, sorry!