2019-02-17 19:25:23 +00:00
|
|
|
module Spectator
|
|
|
|
# Define the file and line number something originated from.
|
|
|
|
struct Source
|
|
|
|
# Absolute file path.
|
|
|
|
getter file : String
|
|
|
|
|
|
|
|
# Line number in the file.
|
|
|
|
getter line : Int32
|
|
|
|
|
|
|
|
# Creates the source.
|
|
|
|
def initialize(@file, @line)
|
|
|
|
end
|
|
|
|
|
2019-02-18 04:23:57 +00:00
|
|
|
# The relative path to the file from the current directory.
|
|
|
|
# If the file isn't in the current directory or a sub-directory,
|
|
|
|
# then the absolute path is provided.
|
|
|
|
def path
|
|
|
|
# Add the path separator here.
|
|
|
|
# Otherwise, things like:
|
|
|
|
# `spectator/foo.cr` and `spectator.cr` overlap.
|
|
|
|
# It also makes the substring easier.
|
|
|
|
cwd = Dir.current + File::SEPARATOR
|
|
|
|
if file.starts_with?(cwd)
|
|
|
|
# Relative to the current directory.
|
|
|
|
# Trim the current directory path from the beginning.
|
|
|
|
file[cwd.size..-1]
|
|
|
|
else
|
|
|
|
# Not trivial to find the file.
|
|
|
|
# Return the absolute path.
|
|
|
|
file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-17 19:25:23 +00:00
|
|
|
# String representation of the source.
|
|
|
|
# This is formatted as `FILE:LINE`.
|
|
|
|
def to_s(io)
|
2019-02-18 04:23:57 +00:00
|
|
|
io << path
|
2019-02-17 19:25:23 +00:00
|
|
|
io << ':'
|
|
|
|
io << line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|