aboutsummaryrefslogtreecommitdiff
path: root/lua/startup/utils.lua
blob: b8b7a825319183fcb587c41378716f1816dd8dd8 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
U = {}
local flag = false
local settings = require "startup.config"
local function start_timeout()
  flag = true
  vim.defer_fn(function()
    flag = false
  end, 150)
end

-- local colors = require("startup.config").colors
local colors = {
  background = "#1f2227",
  heading_fg = "#009900",
  tools_fg = "#009900",
}

U.cursor_pos = vim.api.nvim_win_get_cursor(0)

function U.spaces(amount)
  return string.rep(" ", amount)
end

function U.key_help()
  local buf = vim.api.nvim_create_buf(false, true)
  vim.api.nvim_buf_set_option(buf, "bufhidden", "wipe")
  vim.api.nvim_buf_set_keymap(
    buf,
    "n",
    "<ESC>",
    "<cmd>q<CR>",
    { noremap = true, silent = true, nowait = true }
  )
  vim.api.nvim_buf_set_keymap(
    buf,
    "n",
    "q",
    "<cmd>q<CR>",
    { noremap = true, silent = true, nowait = true }
  )
  local lines = {
    "startup.nvim mapping:",
    "",
    "Execute command: " .. settings.mappings.execute_command,
    "Open file: " .. settings.mappings.open_file,
    "Open file in split: " .. settings.mappings.open_file_split,
    "Open section: " .. settings.mappings.open_section,
  }
  vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
  local win = vim.api.nvim_open_win(buf, true, {
    relative = "cursor",
    width = 30,
    height = 6,
    col = 1,
    row = 1,
    border = "shadow",
    style = "minimal",
  })
  vim.api.nvim_win_set_option(win, "winblend", 20)
  vim.api.nvim_buf_set_option(buf, "modifiable", false)
end

function U.default_header()
  local header = {
    "                                          /$$              ",
    "                                         |__/              ",
    " /$$$$$$$   /$$$$$$   /$$$$$$  /$$    /$$ /$$ /$$$$$$/$$$$ ",
    "| $$__  $$ /$$__  $$ /$$__  $$|  $$  /$$/| $$| $$_  $$_  $$",
    "| $$  \\ $$| $$$$$$$$| $$  \\ $$ \\  $$/$$/ | $$| $$ \\ $$ \\ $$",
    "| $$  | $$| $$_____/| $$  | $$  \\  $$$/  | $$| $$ | $$ | $$",
    "| $$  | $$|  $$$$$$$|  $$$$$$/   \\  $/   | $$| $$ | $$ | $$",
    "|__/  |__/ \\_______/ \\______/     \\_/    |__/|__/ |__/ |__/",
  }
  return header
end

function U.get_oldfiles(amount)
  local oldfiles = { "Last files", "" }
  local oldfiles_raw = vim.fn.execute "oldfiles"
  local oldfiles_amount = 0
  for file in oldfiles_raw:gmatch "[^\n]+" do
    if oldfiles_amount >= amount then
      break
    end
    table.insert(oldfiles, (string.sub(file, 4, -1)))
    oldfiles_amount = oldfiles_amount + 1
  end
  local length = U.longest_line(oldfiles) + 2
  local oldfiles_aligned = {}
  for _, file in ipairs(oldfiles) do
    table.insert(oldfiles_aligned, file .. U.spaces(length - #file))
  end
  return oldfiles_aligned
end

function U.get_oldfiles_directory(amount)
  local oldfiles_raw = vim.fn.execute "oldfiles"
  local oldfiles_amount = 0
  local directory = vim.api.nvim_exec([[pwd]], true)
  local oldfiles = { "Last files in " .. directory, " " }
  for file in oldfiles_raw:gmatch(directory .. "[^\n]+") do
    if oldfiles_amount >= amount then
      break
    end
    table.insert(oldfiles, (string.sub(file, 1, -1)))
    oldfiles_amount = oldfiles_amount + 1
  end
  local length = U.longest_line(oldfiles) + 2
  local oldfiles_aligned = {}
  for _, file in ipairs(oldfiles) do
    table.insert(oldfiles_aligned, file .. U.spaces(length - #file))
  end
  return oldfiles_aligned
end

-- BUG: weird behavior when displaywidth != strlen
function U.reposition_cursor()
  if vim.o.filetype ~= "startup" or flag then
    return
  end
  local column = math.floor(vim.o.columns / 2)
  local new_cursor_pos = vim.api.nvim_win_get_cursor(0)
  if
      -- current line is not empty and cursor is on correct column
      -- moved straight up or down
    vim.trim(vim.api.nvim_get_current_line())
      ~= ""
    and new_cursor_pos[2] == column
  then
    start_timeout()
    return
  elseif
      -- moved to the right
    vim.trim(vim.api.nvim_get_current_line())
      ~= ""
    and new_cursor_pos[2] > column
    and new_cursor_pos[1] == U.cursor_pos[1]
  then
    if new_cursor_pos[1] == vim.api.nvim_buf_line_count(0) then
      vim.api.nvim_win_set_cursor(0, { new_cursor_pos[1], column })
      start_timeout()
      return
    end
    local i = 1
    vim.api.nvim_win_set_cursor(0, { new_cursor_pos[1] + i, column })
    while vim.trim(vim.api.nvim_get_current_line()) == "" do
      vim.api.nvim_win_set_cursor(0, { new_cursor_pos[1] + i, column })
      i = i + 1
    end
    start_timeout()
  elseif
      -- moved to the left
    vim.trim(vim.api.nvim_get_current_line())
      ~= ""
    and new_cursor_pos[2] < column
    and new_cursor_pos[1] == U.cursor_pos[1]
  then
    local i = 1
    if new_cursor_pos[1] == 1 then
      vim.api.nvim_win_set_cursor(0, { new_cursor_pos[1], column })
      start_timeout()
      return
    end
    vim.api.nvim_win_set_cursor(0, { new_cursor_pos[1] - i, column })
    while vim.trim(vim.api.nvim_get_current_line()) == "" do
      if new_cursor_pos[1] - i == 1 then
        vim.api.nvim_win_set_cursor(0, { new_cursor_pos[1], column })
        start_timeout()
        return
      end
      vim.api.nvim_win_set_cursor(0, { new_cursor_pos[1] - i, column })
      i = i + 1
    end
    start_timeout()
  elseif
      -- moved up to empty line
    vim.trim(vim.api.nvim_get_current_line())
      == ""
    and new_cursor_pos[1] < U.cursor_pos[1]
  then
    if new_cursor_pos[1] == 1 then
      vim.api.nvim_win_set_cursor(0, { new_cursor_pos[1], column })
      start_timeout()
      return
    end
    local i = 1
    vim.api.nvim_win_set_cursor(0, { new_cursor_pos[1] - i, column })
    while vim.trim(vim.api.nvim_get_current_line()) == "" do
      vim.api.nvim_win_set_cursor(0, { new_cursor_pos[1] - i, column })
      i = i + 1
    end
    start_timeout()
  elseif
      -- moved down to empty line
    vim.trim(vim.api.nvim_get_current_line())
      == ""
    and new_cursor_pos[1] > U.cursor_pos[1]
  then
    local i = 1
    vim.api.nvim_win_set_cursor(0, { new_cursor_pos[1] + i, column })
    while vim.trim(vim.api.nvim_get_current_line()) == "" do
      vim.api.nvim_win_set_cursor(0, { new_cursor_pos[1] + i, column })
      i = i + 1
    end
    start_timeout()
  end

  U.cursor_pos = vim.api.nvim_win_get_cursor(0)
end

function U.longest_line(lines)
  local longest = 0
  for _, line in ipairs(lines) do
    if vim.fn.strdisplaywidth(line) > longest then
      longest = vim.fn.strdisplaywidth(line)
    end
  end
  return longest
end

function U.create_hls()
  vim.cmd("hi StartupHeading guifg=" .. colors.heading_fg)
  vim.cmd("hi StartupTools guifg=" .. colors.tools_fg)
end

function U.set_buf_options()
  vim.api.nvim_buf_set_option(0, "bufhidden", "wipe")
  vim.api.nvim_buf_set_option(0, "buftype", "nofile")
  vim.cmd [[set nowrap]]
  vim.api.nvim_buf_set_option(0, "filetype", "startup")
  vim.api.nvim_buf_set_option(0, "swapfile", false)
  vim.cmd [[setlocal nonu nornu]]
end

function U.validate_settings(options)
  -- NOTE: vim.validate
  vim.validate {
    type = {
      options.type,
      function(arg)
        for _, v in ipairs { "mapping", "oldfiles", "text" } do
          if v == arg then
            return true
          end
        end
        return false
      end,
      '"mapping" "text" or "oldfiles"',
    },
    oldfiles_directory = {
      options.oldfiles_directory,
      "boolean",
    },
    align = {
      options.align,
      function(arg)
        for _, v in ipairs { "right", "left", "center" } do
          if v == arg then
            return true
          end
        end
        return false
      end,
      '"center" "left" or "right"',
    },
    fold_section = { options.fold_section, "boolean" },
    title = { options.title, "string" },
    margin = { options.margin, "number" },
    command = { options.command, "string" },
    content = {options.content,
    function(content)
      if options.type =="text" and (type(content)== "table" or type(content == "function")) then
        return true
      elseif options.type == "mapping" and type(content) == "table" then
        return true
      end
    end},
    default_color = { options.default_color, "string" },
    highlight = { options.highlight, "string" },
    oldfiles_amount = {
      options.oldfiles_amount,
      "number",
    },
  }
end

return U