function Refresh()
	vim.cmd("checktime")
end

local function run_current_file()
	local file = vim.fn.expand("%:p")
	local filename = vim.fn.expand("%:t:r")
	local dir = vim.fn.expand("%:p:h")
	local ext = vim.fn.fnamemodify(file, ":e")
	local cmd

	if ext == "cpp" then
		local executable = dir .. "/" .. filename
		local compile_cmd = 'g++ -o "' .. executable .. '" -DLOCAL -fno-asm -O2 -Wall -lm --static "' .. file .. '"'
		local run_cmd = 'time "' .. executable .. '"'
		cmd = 'bash -c "' .. compile_cmd .. " && " .. run_cmd .. '; echo Press ENTER to exit...; read"'
	elseif ext == "rs" then
		cmd = 'bash -c "cargo run --release; echo Press ENTER to exit...; read"'
	elseif ext == "py" then
		cmd = 'bash -c "python3 \\"' .. file .. '\\"; echo Press ENTER to exit...; read"'
	elseif ext == "md" then
		print("Markdown preview command is not configured")
		return
	else
		print("Unsupported file type: " .. ext)
		return
	end

	vim.cmd("split term://" .. cmd)
	vim.cmd("resize 15")
end

function Run()
	run_current_file()
end

vim.api.nvim_create_user_command("RunFile", run_current_file, {})
vim.keymap.set("n", "<leader>rr", run_current_file, { noremap = true, silent = true, desc = "Run current file" })
vim.keymap.set("n", "<leader>rR", ":restart<CR>", { noremap = true, silent = true, desc = "Restart Neovim" })
