Full refactor of codebase and usage of lazyvim opts setting. Also split code in custom plugins

This commit is contained in:
2025-08-29 12:30:41 +02:00
parent 524673abfc
commit e37215ae97
33 changed files with 1075 additions and 1381 deletions

View File

@@ -22,6 +22,9 @@ M.open_today = function()
-- If the file does not exist, create and write the header
local header = string.format("# Journal Entry - [[date:%s]]\n\n", os.date("%d.%m.%y"))
file = io.open(full_path, "w")
if file == nil then
return
end
file:write(header)
file:close()
end

View File

@@ -22,9 +22,6 @@ function M.remove_priority()
vim.api.nvim_set_current_line(line)
end
local function strip_ansi(s)
return s:gsub("\27%[[0-9;]*m", "")
end
function M.mark_done()
if not is_todo_file() then return end

98
lua/custom/typst.lua Normal file
View File

@@ -0,0 +1,98 @@
local functions = require("utils.functions")
local watch_job_id = nil
local watch_buf_id = nil
local function watch_and_open()
local input = vim.fn.expand("%:p")
if not input:match("%.typ$") then
vim.notify("Not a Typst file", vim.log.levels.WARN)
return
end
local dir = vim.fn.fnamemodify(input, ":h") -- directory of the Typst file
local filename = vim.fn.expand("%:t:r") .. ".pdf" -- filename without extension + .pdf
-- Get the underlying unicourse dir
local one_up = vim.fn.fnamemodify(dir, ":h")
print(one_up)
local pdf_dir = nil
if vim.fn.filereadable(one_up .. "/.unicourse") == 1 then
pdf_dir = one_up .. "/../../pdfs"
else
pdf_dir = dir
end
vim.fn.mkdir(pdf_dir, "p")
local output = pdf_dir .. "/" .. filename
-- Check if a watcher is already running for this file
if watch_job_id then
vim.notify("Typst watcher already running - please close zathura", vim.log.levels.INFO)
return
end
-- Start typst watch
local cwd = vim.fn.getcwd() -- set the starting directory
-- TODO: root setting does not work
local watch_cmd = { "typst", "watch", "--root", cwd, input, output }
watch_job_id = vim.fn.jobstart(watch_cmd, {
stdout_buffered = false,
stderr_buffered = false, -- Ensure stderr is unbuffered for real-time error output
on_stderr = function(_, data)
if data then
if not watch_tab_id then
pre_tab = vim.api.nvim_get_current_tabpage() -- Get the current tab ID
vim.cmd('tabnew') -- Open a new tab
watch_tab_id = vim.api.nvim_get_current_tabpage() -- Get the current tab ID
watch_buf_id = vim.api.nvim_get_current_buf() -- Get the buffer ID of the new tab
vim.api.nvim_buf_set_option(watch_buf_id, "swapfile", false)
vim.api.nvim_buf_set_name(watch_buf_id, "/tmp/TypstLog")
vim.api.nvim_buf_set_lines(watch_buf_id, 0, 0, false, { "Watching: " .. input }) -- Insert at the top
vim.cmd('write!')
vim.api.nvim_set_current_tabpage(pre_tab)
end
-- Write stdout data to the same buffer
for _, line in ipairs(data) do
if line ~= "" then
vim.api.nvim_buf_set_lines(watch_buf_id, -1, -1, false, { "[LOG] " .. line })
end
end
end
end,
on_exit = function(_, exit_code)
-- Ensure to close the tab that holds the logs
--if watch_tab_id then
-- -- Switch to the tab holding the log buffer and close it
-- vim.api.nvim_set_current_tabpage(watch_tab_id)
-- vim.cmd('tabclose') -- Close the tab holding the log buffer
--end
if exit_code == 0 then
vim.notify("Typst watch stopped successfully", vim.log.levels.INFO)
else
vim.notify("Typst watch stopped with errors", vim.log.levels.ERROR)
end
watch_job_id = nil
end,
})
vim.notify("Started Typst watch", vim.log.levels.INFO)
-- Start sioyek with the --new-window flag and stop watch when it exits
-- ensure that there is no sioyek
vim.fn.system("killall .zathura-wrapped")
functions.Sleep(0.5)
vim.fn.jobstart({ "zathura", output }, {
on_exit = function()
if watch_job_id then
vim.fn.jobstop(watch_job_id)
end
watch_job_id = nil
end,
})
end
return {
Watch_and_open = watch_and_open
}

View File

@@ -1,151 +1,149 @@
local fzf = require("fzf-lua")
local fn = vim.fn
local uv = vim.loop
local current_season = "S3"
-- Function to scan for .unicourse files and get their course directories
function get_course_directories()
local dirs = {}
local function scan_dir(dir)
for _, entry in ipairs(fn.glob(dir .. "/*", true, true)) do
if fn.isdirectory(entry) == 1 then
local unicourse_file = entry .. "/.unicourse"
if fn.filereadable(unicourse_file) == 1 then
local course_info = {}
for line in io.lines(unicourse_file) do
if line:match("^name: ") then
course_info.name = line:sub(7)
elseif line:match("^short: ") then
course_info.short = line:sub(8)
end
end
if course_info.name and course_info.short then
table.insert(dirs, { name = course_info.name, short = course_info.short, path = entry })
end
end
end
end
end
local dirs = {}
local function scan_dir(dir)
for _, entry in ipairs(fn.glob(dir .. "/*", true, true)) do
if fn.isdirectory(entry) == 1 then
local unicourse_file = entry .. "/.unicourse"
if fn.filereadable(unicourse_file) == 1 then
local course_info = {}
for line in io.lines(unicourse_file) do
if line:match("^name: ") then
course_info.name = line:sub(7)
elseif line:match("^short: ") then
course_info.short = line:sub(8)
end
end
if course_info.name and course_info.short then
table.insert(dirs, { name = course_info.name, short = course_info.short, path = entry })
end
end
end
end
end
scan_dir("/home/jonas/projects/university/" .. current_season) -- Assuming all your courses are under the S2 folder
return dirs
scan_dir("/home/jonas/projects/university/" .. current_season) -- Assuming all your courses are under the S2 folder
return dirs
end
-- Function to show the fzf menu for selecting a course directory
function select_course_directory()
local courses = get_course_directories()
local course_names = {}
local courses = get_course_directories()
local course_names = {}
for _, course in ipairs(courses) do
table.insert(course_names, course.name .. " (" .. course.short .. ")")
end
for _, course in ipairs(courses) do
table.insert(course_names, course.name .. " (" .. course.short .. ")")
end
fzf.fzf_exec(course_names, {
prompt = "Select a course > ",
actions = {
["default"] = function(selected)
for _, course in ipairs(courses) do
if selected[1] == (course.name .. " (" .. course.short .. ")") then
show_course_menu(course)
break
end
end
end,
},
})
fzf.fzf_exec(course_names, {
prompt = "Select a course > ",
actions = {
["default"] = function(selected)
for _, course in ipairs(courses) do
if selected[1] == (course.name .. " (" .. course.short .. ")") then
show_course_menu(course)
break
end
end
end,
},
})
end
-- Function to show the fzf menu for actions on a selected course folder
function show_course_menu(course)
local files = {}
-- Collect all VL files in the Vorlesungen directory
for _, file in ipairs(fn.glob(course.path .. "/VL/*", true, true)) do
if file:match("%.typ$") then
table.insert(files, file)
end
end
local files = {}
-- Collect all VL files in the Vorlesungen directory
for _, file in ipairs(fn.glob(course.path .. "/VL/*", true, true)) do
if file:match("%.typ$") then
table.insert(files, file)
end
end
-- Collect options
-- Collect options
-- TODO: implement zettel (Hausaufgaben) management
-- For example creation of new ones and quick opening of the pdfs
local options = {
"Open the newest VL file",
"Create a new VL",
"Open the course folder",
"Open a specific file",
}
local options = {
"Open the newest VL file",
"Create a new VL",
"Open the course folder",
"Open a specific file",
}
fzf.fzf_exec(options, {
prompt = "Choose an action > ",
actions = {
["default"] = function(selected)
if selected[1] == "Open the newest VL file" then
local newest_file = get_newest_vl_file(files)
vim.cmd("edit " .. newest_file)
elseif selected[1] == "Create a new VL" then
create_new_vl(course)
elseif selected[1] == "Open the course folder" then
vim.cmd("edit " .. course.path)
elseif selected[1] == "Open a specific file" then
fzf.fzf_exec(fn.glob(course.path .. "/*", true, true), {
prompt = "Pick a file to open > ",
actions = {
["default"] = function(file)
vim.cmd("edit " .. file[1])
end,
},
})
end
end,
},
})
fzf.fzf_exec(options, {
prompt = "Choose an action > ",
actions = {
["default"] = function(selected)
if selected[1] == "Open the newest VL file" then
local newest_file = get_newest_vl_file(files)
vim.cmd("edit " .. newest_file)
elseif selected[1] == "Create a new VL" then
create_new_vl(course)
elseif selected[1] == "Open the course folder" then
vim.cmd("edit " .. course.path)
elseif selected[1] == "Open a specific file" then
fzf.fzf_exec(fn.glob(course.path .. "/*", true, true), {
prompt = "Pick a file to open > ",
actions = {
["default"] = function(file)
vim.cmd("edit " .. file[1])
end,
},
})
end
end,
},
})
end
-- Function to get the newest VL file based on modification time
function get_newest_vl_file(files)
local newest_file = nil
local newest_time = 0
for _, file in ipairs(files) do
local stat = fn.getftime(file)
if stat > newest_time then
newest_time = stat
newest_file = file
end
end
return newest_file
local newest_file = nil
local newest_time = 0
for _, file in ipairs(files) do
local stat = fn.getftime(file)
if stat > newest_time then
newest_time = stat
newest_file = file
end
end
return newest_file
end
-- Function to create a new VL file based on the template and incrementing the number
function create_new_vl(course)
local vl_dir = course.path .. "/VL"
local vl_dir = course.path .. "/VL"
local success, _ = pcall(function()
vim.fn.mkdir(vl_dir)
end)
-- Hard coded this
local template_path = vim.fn.expand("~/projects/university/data/template.typ")
if fn.filereadable(template_path) == 1 then
-- Find the latest VL number in the folder
local latest_num = 0
for _, file in ipairs(fn.glob(vl_dir .. "/*", true, true)) do
if file:match(course.short .. "VL(%d+).typ$") then
local num = tonumber(file:match(course.short .. "VL(%d+).typ$"))
if num > latest_num then
latest_num = num
end
end
end
local template_path = vim.fn.expand("~/projects/university/data/template.typ")
if fn.filereadable(template_path) == 1 then
-- Find the latest VL number in the folder
local latest_num = 0
for _, file in ipairs(fn.glob(vl_dir .. "/*", true, true)) do
if file:match(course.short .. "VL(%d+).typ$") then
local num = tonumber(file:match(course.short .. "VL(%d+).typ$"))
if num > latest_num then
latest_num = num
end
end
end
-- Create new VL file with incremented number
local new_vl_name = string.format("%sVL%d.typ", course.short, latest_num + 1)
local new_vl_path = vl_dir .. "/" .. new_vl_name
-- Create new VL file with incremented number
local new_vl_name = string.format("%sVL%d.typ", course.short, latest_num + 1)
local new_vl_path = vl_dir .. "/" .. new_vl_name
-- Copy the template if it exists
vim.fn.system({"cp", template_path, new_vl_path})
-- Copy the template if it exists
vim.fn.system({ "cp", template_path, new_vl_path })
-- Open the new VL file
vim.cmd("edit " .. new_vl_path)
else
print("Template file (template.typ) not found!")
end
-- Open the new VL file
vim.cmd("edit " .. new_vl_path)
else
print("Template file (template.typ) not found!")
end
end