This commit is contained in:
ge
2026-01-21 20:58:29 +03:00
commit 184144aa2c
9 changed files with 288 additions and 0 deletions

116
pathstr.v Normal file
View File

@@ -0,0 +1,116 @@
module pathstr
import os
pub type Path = string
// join joins the path parts with platform specific path separator.
pub fn (p Path) join(path ...string) Path {
return Path(os.join_path(p, ...path))
}
// dir returns a directory path from path e.g. `/a/b` from `/a/b/c`.
pub fn (p Path) dir() Path {
return Path(os.dir(p))
}
// name return a file or directory base name from path e.g. `c` from `/a/b/c/`, `b.txt` from `/a/b.txt`.
pub fn (p Path) name() string {
if p == '' {
return '.'
}
if p.contains_only(os.path_separator) {
return os.path_separator
}
if p.ends_with(os.path_separator) {
return os.base(p)
}
return os.file_name(p)
}
// ext returns a file extension from path e.g. `.txt` from `/a/b.txt`
pub fn (p Path) ext() string {
return Path(os.file_ext(p))
}
// split splits path to dir, file name and extension (with dot).
pub fn (p Path) split() (string, string, string) {
return os.split_path(p)
}
// expand_tilde expands tilde to absolute path of user home directory.
pub fn (p Path) expand_tilde() Path {
return Path(os.expand_tilde_to_home(p))
}
// normalized returns the path normalized by resolving backlinks (..), turning forward slashes
// into back slashes on a Windows system and eliminating: references to current directories (.),
// redundant path separators, the last path separator.
pub fn (p Path) normalized() Path {
return Path(os.norm_path(p))
}
// quoted returns a quoted version of the path, depending on the platform.
pub fn (p Path) quoted() string {
return os.quoted_path(p)
}
// abs returns the absolute path.
pub fn (p Path) abs() Path {
return Path(os.abs_path(p))
}
// real returns the full absolute path for path, with all relative '../../', symlinks and so on resolved.
pub fn (p Path) real() Path {
return Path(os.real_path(p))
}
// exists returns true if file or directory exists in filesystem.
pub fn (p Path) exists() bool {
return os.exists(p)
}
// existing returns the existing part of the given path.
pub fn (p Path) existing() !Path {
return Path(os.existing_path(p))
}
// is_abs returns true if path is absolute.
pub fn (p Path) is_abs() bool {
return os.is_abs_path(p)
}
// is_file returns true if the path is file.
pub fn (p Path) is_file() bool {
return os.is_file(p)
}
// is_file returns true if the path is symbolic link.
pub fn (p Path) is_symlink() bool {
return os.is_link(p)
}
// is_dir returns true if the path is directory.
pub fn (p Path) is_dir() bool {
return os.is_dir(p)
}
// is_dir_empty returns true if the directory in path is empty.
pub fn (p Path) is_dir_empty() bool {
return os.is_dir_empty(p)
}
// is_readable returns true if the path can be accessed with read access mode.
pub fn (p Path) is_readable() bool {
return os.is_readable(p)
}
// is_writable returns true if the path can be accessed with write access mode.
pub fn (p Path) is_writable() bool {
return os.is_writable(p)
}
// is_executable returns true if the path can be accessed with execute access mode.
pub fn (p Path) is_executable() bool {
return os.is_executable(p)
}