Created
June 7, 2014 03:53
-
-
Save knewter/7c464443933c74efa483 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
defmodule AudioPlayground.Visualization.AsciiBarMeter do | |
# Expects a half-width and a value between -1 and 1 | |
# FIXME: Typespecs | |
def output(half_width, value) do | |
inner = for x <- 1..width(half_width), into: "" do | |
cond do | |
x == string_position(half_width, value) -> "x" | |
true -> " " | |
end | |
end | |
"[#{inner}]" | |
end | |
def width(half_width), do: (half_width * 2) + 1 | |
def string_position(half_width, value) do | |
position = :erlang.round(value * half_width) | |
half_width + position + 1 | |
end | |
end | |
defmodule AudioPlayground.Visualization.AsciiBarMeterTest do | |
use ExUnit.Case | |
alias AudioPlayground.Visualization.AsciiBarMeter | |
test "it places the x in the center for a value of 0" do | |
assert AsciiBarMeter.output(2, 0) == "[ x ]" | |
end | |
test "it places the x on the right for a value of 1" do | |
assert AsciiBarMeter.output(2, 1) == "[ x]" | |
end | |
test "it places the x on the left for a value of -1" do | |
assert AsciiBarMeter.output(2, -1) == "[x ]" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment