Skip to content

Commit

Permalink
Merge pull request #7 from Sonicfury/feat/template-files-support
Browse files Browse the repository at this point in the history
feat: template files support
  • Loading branch information
0xJohnnyboy authored Feb 9, 2024
2 parents d4f19af + a87fdc8 commit 633eba5
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ https://user-images.githubusercontent.com/49813786/235376612-328159ff-4209-4bc0-

https://user-images.githubusercontent.com/49813786/235376626-6b23bf7e-def1-4d4b-bfa0-ea22e7ba3e61.mov

### Templates

You can save any buffer as a Scretch template. It'll be saved in the default directory if none is explicitely provided in the config.
You can also search and edit templates with Telescope or Fzf-Lua, and create a new Scretch from a template.
See [suggested mappings](#suggested-mappings).

# Installation

This plugin requires Telescope or fzf-lua and ripgrep to function.
Expand Down Expand Up @@ -46,6 +52,7 @@ Here are the default settings used in Scretch:
```lua
local config = {
scretch_dir = vim.fn.stdpath('config') .. '/scretch/', -- will be created if it doesn't exist
templte_dir = vim.fn.stdpath('data') .. '/scretch/templates', -- will be created if it doesn't exist
default_name = "scretch_",
default_type = "txt", -- default unnamed Scretches are named "scretch_*.txt"
split_cmd = "vsplit", -- vim split command used when creating a new Scretch
Expand All @@ -60,10 +67,13 @@ You can copy those settings, update them with your preferences and put them into
local scretch = require("scretch")
vim.keymap.set('n', '<leader>sn', scretch.new)
vim.keymap.set('n', '<leader>snn', scretch.new_named)
vim.keymap.set('n', '<leader>sft', scretch.new_from_template)
vim.keymap.set('n', '<leader>sl', scretch.last)
vim.keymap.set('n', '<leader>ss', scretch.search)
vim.keymap.set('n', '<leader>st', scretch.edit_template)
vim.keymap.set('n', '<leader>sg', scretch.grep)
vim.keymap.set('n', '<leader>sv', scretch.explore)
vim.keymap.set('n', '<leader>sat', scretch.save_as_template)
```

# Issues
Expand Down
108 changes: 104 additions & 4 deletions lua/scretch/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local config = {
scretch_dir = vim.fn.stdpath('config') .. '/scretch/',
template_dir = vim.fn.stdpath('data') .. '/scretch/templates/',
default_name = "scretch_",
default_type = "txt",
split_cmd = "vsplit",
Expand All @@ -9,6 +10,7 @@ local config = {
local function setup(user_config)
config = vim.tbl_deep_extend("force", config, user_config or {})
vim.fn.mkdir(config.scretch_dir, "p")
vim.fn.mkdir(config.template_dir, "p")
end

-- creates a new scretch file in the scretch directory.
Expand Down Expand Up @@ -48,10 +50,6 @@ local function search()
end
end

local function get_grep_args(backend, query)

end

-- performs a live grep accross scretch files
local function grep(query)
if config.backend == "telescope.builtin" then
Expand Down Expand Up @@ -106,9 +104,111 @@ local function last()
end
end

-- saves current buffer as scretch template
local function save_as_template()
local buffer = vim.api.nvim_get_current_buf()
local filename = vim.fn.input('Enter template name: ')
local template_path = config.template_dir .. filename
local lines = vim.api.nvim_buf_get_lines(buffer, 0, -1, false)

local file, err = io.open(template_path, 'w')
if not file then
print('Error opening file: ' .. err)
return
end
for _, line in ipairs(lines) do
file:write(line .. '\n')
end
file:close()
end

-- opens finder for editing templates
local function edit_template()
if config.backend == "telescope.builtin" then
return require('telescope.builtin').find_files({
prompt_title = "Scretch Templates",
cwd = config.template_dir,
})
elseif config.backend == "fzf-lua" then
return require('fzf-lua').files({
prompt = "Scretch Templates > ",
cwd = config.template_dir,
})
end
end

-- opens finder to create a new scretch from the given template
local function new_from_template()
if config.backend == "telescope.builtin" then
require('telescope.builtin').find_files({
prompt_title = "New Scretch from Template",
cwd = config.template_dir,
attach_mappings = function(_, map)
map('i', '<CR>', function()
local action_state = require('telescope.actions.state')
local new_scretch_name = vim.fn.input('Enter new Scretch name: ')
if new_scretch_name ~= '' then
local new_scretch_path = config.scretch_dir .. new_scretch_name
local template_path = action_state.get_selected_entry().path
local template_content = vim.fn.readfile(template_path)

if template_content then
local new_scretch_file = io.open(new_scretch_path, 'w')
if new_scretch_file then
for _, line in ipairs(template_content) do
new_scretch_file:write(line .. '\n')
end
new_scretch_file:close()
vim.api.nvim_command(":e! " .. new_scretch_path)
else
print('Error opening file for writing: ' .. new_scretch_path)
end
else
print('Error reading template file: ' .. template_path)
end
end
end)
return true
end,
})
elseif config.backend == "fzf-lua" then
require('fzf-lua').files({
prompt = "New Scretch from Template > ",
cwd = config.template_dir,
actions = {
["default"] = function(selected)
local new_scretch_name = vim.fn.input('Enter new Scretch name: ')
if new_scretch_name ~= '' then
local new_scretch_path = config.scretch_dir .. new_scretch_name
local template_content = vim.fn.readfile(selected[1].path)

if template_content then
local new_scretch_file = io.open(new_scretch_path, 'w')
if new_scretch_file then
for _, line in ipairs(template_content) do
new_scretch_file:write(line .. '\n')
end
new_scretch_file:close()
vim.api.nvim_command(':e! ' .. new_scretch_path)
else
print('Error creating new Scretch file: ' .. new_scretch_path)
end
else
print('Error reading template file: ' .. template_path)
end
end
end,
},
})
end
end

local module = {
new = new,
new_named = new_named,
save_as_template = save_as_template,
new_from_template = new_from_template,
edit_template = edit_template,
last = last,
search = search,
grep = grep,
Expand Down

0 comments on commit 633eba5

Please sign in to comment.