local tele_status_ok, _ = pcall(require, "telescope") if not tele_status_ok then return end local plen_status_ok, _ = pcall(require, "plenary") if not plen_status_ok then return end local term_status_ok, _ = pcall(require, "toggleterm") if not term_status_ok then return end local M = {} M.terminals = {} local utils = require "telescope.utils" local pickers = require "telescope.pickers" local finders = require "telescope.finders" local make_entry = require "telescope.make_entry" local conf = require("telescope.config").values local Path = require "plenary.path" local toggleterm = require "toggleterm" local terms = require "toggleterm.terminal" local function GetBaseDir(filePath) local sep = Path.path.sep if filePath:sub(1, #sep) == sep then return "" else return filePath:sub(1, string.find(filePath, sep)) end end local function file_exists(name) local f = io.open(name, "r") if f ~= nil then io.close(f) return true else return false end end function GetBaseName(filePath) if filePath:sub(-1) == Path.path.sep then filePath = filePath:sub(1, -2) end local pos = string.find(filePath, Path.path.sep) while pos ~= nil do filePath = string.sub(filePath, pos + 1) pos = string.find(filePath, Path.path.sep) end return filePath end local function buf_in_cwd(bufname, cwd) if cwd:sub(-1) ~= Path.path.sep then cwd = cwd .. Path.path.sep end local bufname_prefix = bufname:sub(1, #cwd) return bufname_prefix == cwd end M.oldfiles = function(opts) local current_buffer = vim.api.nvim_get_current_buf() local current_file = vim.api.nvim_buf_get_name(current_buffer) local results = {} for _, buffer in ipairs(vim.split(vim.fn.execute ":buffers! t", "\n")) do local match = tonumber(string.match(buffer, "%s*(%d+)")) local open_by_lsp = string.match(buffer, "line 0$") if match and not open_by_lsp then local file = vim.api.nvim_buf_get_name(match) if vim.loop.fs_stat(file) and match ~= current_buffer then table.insert(results, file) end end end for _, file in ipairs(vim.v.oldfiles) do local file_stat = vim.loop.fs_stat(file) if file_stat and file_stat.type == "file" and not vim.tbl_contains(results, file) and file ~= current_file then if GetBaseName(file) ~= ".env" then table.insert(results, file) end end end local cwd = vim.loop.cwd() cwd = cwd .. utils.get_separator() results = vim.tbl_filter(function(file) return buf_in_cwd(file, cwd) end, results) pickers .new(opts, { prompt_title = "Oldfiles from " .. GetBaseName(cwd), finder = finders.new_table { results = results, entry_maker = make_entry.gen_from_file(opts), }, sorter = conf.file_sorter(opts), previewer = conf.file_previewer(opts), }) :find() end function M.select_terminal() local terminals = terms.get_all() if #terminals > 0 then vim.ui.select(terminals, { prompt = "Please select a terminal to open (or focus): ", format_item = function(term) return term.id .. ": " .. term:_display_name() end, }, function(term) if not term then return end if term:is_open() then term:focus() else term:open() end end) end end function M.toggle_term() if #terms.get_all() > 0 then terms.get_last_focused():toggle() end end function M.term_horizontal() local term, _ = terms.get_or_create_term(8, "horizontal", "horizontal") term:toggle(8, "horizontal") end function M.term_vertical() local term, _ = terms.get_or_create_term(9, "vertical", "vertical") term:toggle(80, "vertical") end function M.term_float() local term, _ = terms.get_or_create_term(11, "float", "float") term:toggle(0, "float") end function M.openMDfile(executable) local file = vim.api.nvim_buf_get_name(0) if executable then M.MDexecutable = executable end vim.api.nvim_command("terminal flatpak run " .. M.MDexecutable .. " " .. file) end function M.compile_sass() local term_num = 12 local term = terms.get(term_num, true) local dist = "dist/css" local file_name = "compile_at" if file_exists(file_name) then local file_ = io.open(file_name, "r") if file_ ~= nil then dist = file_:read() file_:close() end end local cmd = "node ~/.local/lib/node_modules/sass/sass.js --watch sass:" .. dist .. " -s compressed --no-source-map" if term then term:toggle() else toggleterm.exec(cmd, term_num, 100, ".", "float", "compile_sass") end end function M.go_to_definition() vim.cmd("vsplit") vim.lsp.buf.definition() end return M -- vim: ts=4 sts=4 sw=4 expandtab