Skip to content

Commit

Permalink
docs(README): add befefits section
Browse files Browse the repository at this point in the history
  • Loading branch information
pysan3 committed Jan 29, 2024
1 parent 606f36d commit 0979cd1
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 4 deletions.
71 changes: 70 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,56 @@ mutliple OSs in neovim. The plugin API is heavily inspired by Python's
[neo-tree.nvim](https://github.com/nvim-neo-tree/neo-tree.nvim) but it
is very simple and portable to be used in any plugin.

# Usage Example
# ✨ Benefits

## 📦 Intuitive and Useful Methods

``` lua
local Path = require("pathlib")
local dir = Path("~/Documents") -- Same as `Path.home() / "Documents"`
local foo = dir / "foo.txt"

print(foo:basename(), foo:stem(), foo:suffix()) -- foo.txt, foo, .txt
print(foo:parent()) -- "/home/user/Documents"
```

## 📋 Git Integration

``` lua
local git_root = Path("/path/to/git/workdir")
-- assert(git_root:child(".git"):exists(), string.format("%s is not a git repo.", git_root))

require("pathlib.git").fill_git_state({ file_a, file_b, ... })

file_a.git_state.ignored -- is git ignored
file_a.git_state.status -- git status (modified, added, staged, ...)
file_a.git_state.git_root -- root directory of the repo
```

## ⏱️ Sync / Async Operations

The API is designed so it is very easy to switch between sync and async
operations. Call them inside [nvim-nio
task](https://github.com/nvim-neotest/nvim-nio) without any change, and
the operations are converted to be async (does not block the main
thread).

``` lua
local foo = Path("~/Documents/foo.txt")
local content = "File Content\n"

-- # sync
local sync_bytes = foo:fs_write(content)
assert(sync_bytes == content:len(), foo.error_msg)

-- # async
require("nio").run(function()
local async_bytes = foo:fs_write(content)
assert(async_bytes == content:len(), foo.error_msg)
end)
```

# 🚀 Usage Example

## Create Path Object

Expand All @@ -51,6 +100,26 @@ local bar = foo .. "bar.txt"
assert(tostring(bar) == "folder/bar.txt")
```

### Path object is stored with `string[]`.

- Very fast operations to work with parents / children / siblings.

- No need to worry about path separator =\> OS Independent.

- `/`: Unix, `\`: Windows

### Nicely integrated with vim functions.

There are wrappers around vim functions such as `fnamemodify`, `stdpath`
and `getcwd`.

``` lua
path:modify(":p:t:r") -- vim.fn.fnamemodify

-- Define child directory of stdpaths
Path.stdpath("data", "mason", "bin") -- vim.fn.stdpath("data") .. "/mason/bin"
```

## Create and Manipulate Files / Directories

``` lua
Expand Down
62 changes: 59 additions & 3 deletions README.norg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description:
authors: takuto
categories:
created: 2023-11-14
updated: 2024-01-26T19:32:04+0900
updated: 2024-01-29T17:41:30+0900
version: 1.1.1
@end

Expand Down Expand Up @@ -35,7 +35,49 @@ version: 1.1.1
It is mainly used in {https://github.com/nvim-neo-tree/neo-tree.nvim}[neo-tree.nvim]
but it is very simple and portable to be used in any plugin.

* Usage Example
* ✨ Benefits
** 📦 Intuitive and Useful Methods
@code lua
local Path = require("pathlib")
local dir = Path("~/Documents") -- Same as `Path.home() / "Documents"`
local foo = dir / "foo.txt"

print(foo:basename(), foo:stem(), foo:suffix()) -- foo.txt, foo, .txt
print(foo:parent()) -- "/home/user/Documents"
@end

** 📋 Git Integration
@code lua
local git_root = Path("/path/to/git/workdir")
-- assert(git_root:child(".git"):exists(), string.format("%s is not a git repo.", git_root))

require("pathlib.git").fill_git_state({ file_a, file_b, ... })

file_a.git_state.ignored -- is git ignored
file_a.git_state.status -- git status (modified, added, staged, ...)
file_a.git_state.git_root -- root directory of the repo
@end

** ⏱️ Sync / Async Operations
The API is designed so it is very easy to switch between sync and async operations.
Call them inside {https://github.com/nvim-neotest/nvim-nio}[nvim-nio task] without any change,
and the operations are converted to be async (does not block the main thread).
@code lua
local foo = Path("~/Documents/foo.txt")
local content = "File Content\n"

-- # sync
local sync_bytes = foo:fs_write(content)
assert(sync_bytes == content:len(), foo.error_msg)

-- # async
require("nio").run(function()
local async_bytes = foo:fs_write(content)
assert(async_bytes == content:len(), foo.error_msg)
end)
@end

* 🚀 Usage Example
** Create Path Object
@code lua
local Path = require("pathlib")
Expand All @@ -57,6 +99,20 @@ version: 1.1.1
assert(tostring(bar) == "folder/bar.txt")
@end

*** Path object is stored with `string[]`.
- Very fast operations to work with parents / children / siblings.
- No need to worry about path separator => OS Independent.
-- `/`: Unix, `\\`: Windows

*** Nicely integrated with vim functions.
There are wrappers around vim functions such as `fnamemodify`, `stdpath` and `getcwd`.
@code lua
path:modify(":p:t:r") -- vim.fn.fnamemodify

-- Define child directory of stdpaths
Path.stdpath("data", "mason", "bin") -- vim.fn.stdpath("data") .. "/mason/bin"
@end

** Create and Manipulate Files / Directories
@code lua
local luv = vim.loop
Expand Down Expand Up @@ -125,7 +181,7 @@ version: 1.1.1

* TODO
- ( ) API documentation.
- ( ) Git operation integration.
- (-) Git operation integration.
- ( ) Windows implementation, test environment.

* Contributions
Expand Down

0 comments on commit 0979cd1

Please sign in to comment.