ETee.Tail (e_tee v0.1.0)

Copy Markdown View Source

The last limit lines of a stream, kept as they arrive.

tail = ETee.Tail.new(1000)
tail = ETee.Tail.push(tail, "some output\n")
ETee.Tail.to_binary(tail)

The limit is a line count. A limit of 0 keeps nothing and discards every push; :infinity keeps everything. Bytes are stored exactly as pushed, so a line split across pushes is still one line and carriage returns do not end one.

Pushing appends and does not touch what is already held. The buffer is trimmed back to limit lines once it has grown well past the limit, and again on every read, so to_binary/1, lines/1 and bytes/1 all report the trimmed buffer.

Summary

Functions

How many bytes are held.

Whether the buffer has reached its limit and is dropping lines.

How many lines are held, counting an unfinished one.

An empty buffer keeping at most limit lines.

Append bytes, discarding them entirely when the limit is 0.

Everything held, oldest first.

Types

t()

@type t() :: %ETee.Tail{
  bytes: non_neg_integer(),
  chunks: [binary()],
  limit: non_neg_integer() | :infinity,
  newlines: non_neg_integer()
}

Functions

bytes(tail)

@spec bytes(t()) :: non_neg_integer()

How many bytes are held.

full?(tail)

@spec full?(t()) :: boolean()

Whether the buffer has reached its limit and is dropping lines.

lines(tail)

@spec lines(t()) :: non_neg_integer()

How many lines are held, counting an unfinished one.

new(limit \\ 1000)

@spec new(non_neg_integer() | :infinity) :: t()

An empty buffer keeping at most limit lines.

push(tail, bytes)

@spec push(t(), binary()) :: t()

Append bytes, discarding them entirely when the limit is 0.

to_binary(tail)

@spec to_binary(t()) :: binary()

Everything held, oldest first.