aboutsummaryrefslogtreecommitdiff
path: root/lua/startup.lua
blob: d8954330e09f199fca27d12da2308974ea9ab39d (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
local M = {}
local ns = vim.api.nvim_create_namespace "startup"

local limited_space = false

local opts = { noremap = true, silent = true }
local settings = require "startup.config"

local utils = require "startup.utils"
local spaces = utils.spaces

local function create_mappings()
  vim.api.nvim_buf_set_keymap(
    0,
    "n",
    "<CR>",
    ":lua require'startup'.check_line()<CR>",
    opts
  )
  for _, cmd in pairs(settings.tools) do
    vim.api.nvim_buf_set_keymap(
      0,
      "n",
      cmd[2],
      "<cmd>" .. cmd[1] .. "<CR>",
      opts
    )
  end
end

function M.new_file()
  local name = vim.fn.input "Filename: > "
  vim.cmd("e " .. name)
end

function M.check_line()
  local line = vim.api.nvim_get_current_line()
  for name, command in pairs(settings.tools) do
    if line:match(name) then
      vim.cmd(command[1])
    end
  end
end

local function align(dict)
  local padding = 0
  if settings.options.padding < 1 then
    padding = vim.o.columns * padding
  else
    padding = settings.options.padding
  end
  local aligned = {}
  local max_len = utils.longest_line(dict)
  if settings.options.align == "center" then
    local space_left = vim.o.columns - max_len
    for _, line in ipairs(dict) do
      table.insert(aligned, spaces(space_left / 2) .. line)
    end
  elseif settings.options.align == "left" then
    for _, line in ipairs(dict) do
      table.insert(aligned, spaces(settings.options.padding) .. line)
    end
  elseif settings.options.align == "right" then
    for _, line in ipairs(dict) do
      table.insert(
        aligned,
        spaces(vim.o.columns - max_len - settings.options.padding - 10) .. line
      )
    end
  end
  return aligned
end

local count = 1
local function set_lines(len, text, hi, pass)
  vim.api.nvim_buf_set_lines(0, count, count + len, false, align(text))
  vim.api.nvim_win_set_cursor(0, { count, 0 })
  if pass then
    vim.g.section_length = count
  end
  for i = count, count + len do
    vim.api.nvim_buf_add_highlight(0, ns, hi, i, 1, -1)
  end
  count = count + len
end

local function empty()
  set_lines(1, { " " }, "StartupTools")
end

local function body()
  local toolnames = {}
  for name, cmd in pairs(settings.tools) do
    if not limited_space then
      table.insert(toolnames, " ")
    end
    if settings.options.mapping_names then
      table.insert(toolnames, name .. "  " .. cmd[2])
    else
      table.insert(toolnames, name)
    end
  end

  return toolnames
end

function M.display()
  local rly_limited_space = false

  if vim.o.lines < (#settings.header + (#settings.tools * 2) + 20) then
    limited_space = true
  end

  create_mappings()
  utils.create_hls()
  -- vim.api.nvim_buf_set_keymap(0, "n", "j", "2j", opts)
  -- vim.api.nvim_buf_set_keymap(0, "n", "k", "2k", opts)
  if not limited_space then
    empty()
  end
  set_lines(#settings.header, settings.header, "StartupHeading")

  local toolnames = body()
  empty()
  set_lines(#toolnames, toolnames, "StartupTools")

  vim.cmd [[silent! %s/\s\+$//]] -- clear trailing whitespace
  U.set_buf_options()
  if limited_space then
    vim.api.nvim_win_set_cursor(
      0,
      { #settings.header + 3, math.floor(vim.o.columns / 2) }
    )
  end
end

function M.setup(update)
  if vim.g.startup_nvim_loaded then
    return
  end
  vim.g.startup_nvim_loaded = true

  settings = vim.tbl_deep_extend("force", settings, update or {})
  vim.cmd [[
  autocmd VimEnter * lua if vim.fn.argc() == 0 then require"startup".display() end
  ]]
end

return M