mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add #path method
Update #to_s to use relative path if possible.
This commit is contained in:
parent
dc7d5fbe25
commit
2c7cd9b728
1 changed files with 21 additions and 1 deletions
|
@ -11,10 +11,30 @@ module Spectator
|
||||||
def initialize(@file, @line)
|
def initialize(@file, @line)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
# String representation of the source.
|
# String representation of the source.
|
||||||
# This is formatted as `FILE:LINE`.
|
# This is formatted as `FILE:LINE`.
|
||||||
def to_s(io)
|
def to_s(io)
|
||||||
io << file
|
io << path
|
||||||
io << ':'
|
io << ':'
|
||||||
io << line
|
io << line
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue