summaryrefslogtreecommitdiff
path: root/.config/nvim/lua/compile.lua
blob: 8343cfe0456ceca03c21f696624bb9c2a0e8e8e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
local openTerminal = function(command)
  local Terminal  = require('toggleterm.terminal').Terminal
  local term = Terminal:new({
      cmd = command,
      direction = 'horizontal'
  })
  term:toggle()
end

local compilePath = vim.fn.getcwd() .. '/.compile'
local runPath = vim.fn.getcwd() .. '/.run'

local checkGetFileContent = function(path, typ)
  local file_exists = os.rename(path, path)
  
  if file_exists then
    openTerminal(
      'echo "Directory: $(pwd)"; echo "' .. typ .. ' started at $(date +"%H:%M:%S")"; echo; '
      .. 'bash ' .. path ..
      '; echo; echo "' .. typ .. ' finished at $(date +"%H:%M:%S")"; read'
    )
    return
  end

  local file = io.open(path, 'w')

  file:seek('set')
  file:write([[#!/bin/sh
set -xe
]]
  )
  file:close()
  print('Created ' .. path)
  return nil
end

vim.keymap.set('n', 'zz', function()
  checkGetFileContent(
    compilePath,
    'Compilation'
  )
end)
vim.keymap.set('n', 'zx', function()
  checkGetFileContent(
    runPath,
    'Run'
  )
end)