Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(base): Implement posix methods #10

Merged
merged 9 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/lua_ls-typecheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ on:
pull_request: ~
push:
branches:
- '*'
- 'main'
- 'v*'

jobs:
build:
Expand Down
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ earliest.**
Status](https://img.shields.io/github/actions/workflow/status/pysan3/pathlib.nvim/lua_ls-typecheck.yml?style=for-the-badge)](https://github.com/pysan3/pathlib.nvim/actions/workflows/lua_ls-typecheck.yml)
[![LuaRocks](https://img.shields.io/luarocks/v/pysan3/pathlib.nvim?logo=lua&color=purple&style=for-the-badge)](https://luarocks.org/modules/pysan3/pathlib.nvim)

# TL;DR
# Usage Example

## Create Path Object

``` lua
local Path = require("pathlib.base")
Expand All @@ -44,6 +46,46 @@ local bar = foo .. "bar.txt" -- create siblings (just like `./<foo>/../bar.txt`)
assert(tostring(bar) == "folder/bar.txt")
```

## Create and Manipulate Files / Directories

``` lua
local luv = vim.loop
local Path = require("pathlib.base")

local new_file = Path.new("./new/folder/foo.txt")
new_file:parent():mkdir(Path.permission("rwxr-xr-x"), true) -- (permission, recursive)

-- You don't need above line if you specify recursive = true in `open`; all parents will be created
local fd, err_name, err_msg = new_file:open("w", Path.permission("rw-r--r--"), true)
assert(fd ~= nil, "File creation failed. " .. err_name .. err_msg)
luv.fs_write(fd, "File Content\n")
luv.fs_close(fd)

local content = new_file:read(0)
assert(content == "File Content\n")

new_file:copy(new_file .. "bar.txt")
new_file:symlink_to(new_file .. "baz.txt")
```

## Scan Directories

``` lua
-- Continue from above
for path in new_file:parent():iterdir() do
-- path will be [Path("./new/folder/foo.txt"), Path("./new/folder/bar.txt"), Path("./new/folder/baz.txt")]
end

-- fs_scandir-like usage
new_file:parent():iterdir_async(function(path, fs_type) -- callback on all files
vim.print(tostring(path), fs_type)
end, function(error) -- on error
vim.print("Error: " .. error)
end, function(count) -- on exit
vim.print("Scan Finished. " .. count .. " files found.")
end)
```

# TODO

- API documentation
Expand Down
41 changes: 40 additions & 1 deletion README.norg
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ version: 1.1.1
{https://github.com/pysan3/pathlib.nvim/actions/workflows/lua_ls-typecheck.yml}[!{https://img.shields.io/github/actions/workflow/status/pysan3/pathlib.nvim/lua_ls-typecheck.yml?style=for-the-badge}[Build Status]]
{https://luarocks.org/modules/pysan3/pathlib.nvim}[!{https://img.shields.io/luarocks/v/pysan3/pathlib.nvim?logo=lua&color=purple&style=for-the-badge}[LuaRocks]]

* TL;DR
* Usage Example
** Create Path Object
@code lua
local Path = require("pathlib.base")

Expand All @@ -51,6 +52,44 @@ version: 1.1.1
assert(tostring(bar) == "folder/bar.txt")
@end

** Create and Manipulate Files / Directories
@code lua
local luv = vim.loop
local Path = require("pathlib.base")

local new_file = Path.new("./new/folder/foo.txt")
new_file:parent():mkdir(Path.permission("rwxr-xr-x"), true) -- (permission, recursive)

-- You don't need above line if you specify recursive = true in `open`; all parents will be created
local fd, err_name, err_msg = new_file:open("w", Path.permission("rw-r--r--"), true)
assert(fd ~= nil, "File creation failed. " .. err_name .. err_msg)
luv.fs_write(fd, "File Content\n")
luv.fs_close(fd)

local content = new_file:read(0)
assert(content == "File Content\n")

new_file:copy(new_file .. "bar.txt")
new_file:symlink_to(new_file .. "baz.txt")
@end

** Scan Directories
@code lua
-- Continue from above
for path in new_file:parent():iterdir() do
-- path will be [Path("./new/folder/foo.txt"), Path("./new/folder/bar.txt"), Path("./new/folder/baz.txt")]
end

-- fs_scandir-like usage
new_file:parent():iterdir_async(function(path, fs_type) -- callback on all files
vim.print(tostring(path), fs_type)
end, function(error) -- on error
vim.print("Error: " .. error)
end, function(count) -- on exit
vim.print("Scan Finished. " .. count .. " files found.")
end)
@end

* TODO
- API documentation
- Windows implementation, test environment.
Expand Down
Loading