From 53a9172bddbb83a1c570cc3fed794e7b73c1f1d7 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Sat, 7 Dec 2024 09:54:22 +0100 Subject: add default config --- src/init.lua | 406 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 406 insertions(+) create mode 100644 src/init.lua (limited to 'src/init.lua') diff --git a/src/init.lua b/src/init.lua new file mode 100644 index 0000000..6d396b6 --- /dev/null +++ b/src/init.lua @@ -0,0 +1,406 @@ +-- ┏┓╻┏━╸┏━┓┏━┓╻ ╻┏━╸┏━╸╺┳╸ ┏━╸┏━┓┏┓╻┏━╸╻┏━╸ +-- ┃┗┫┣╸ ┃ ┃┗━┓┣━┫┣╸ ┣╸ ┃ ┃ ┃ ┃┃┗┫┣╸ ┃┃╺┓ +-- ╹ ╹┗━╸┗━┛┗━┛╹ ╹┗━╸┗━╸ ╹ ┗━╸┗━┛╹ ╹╹ ╹┗━┛ +-- +-- This is the default neosheet config. Without any config +-- neosheet is *not* capable of anything, since its basically just +-- a display for sheets. +-- +-- All kinds of shortcuts and key presses and color schemes need +-- to be defined in this lua config file. +-- +-- Let's bind the neosheet environment to a local variable so +-- it's easier to read and use. +-- +local neosheet = require('neosheet') + + +-- ╺┳╸╻ ╻┏━╸┏┳┓┏━╸ +-- ┃ ┣━┫┣╸ ┃┃┃┣╸ +-- ╹ ╹ ╹┗━╸╹ ╹┗━╸ +-- +-- The following section defines the default theme of neosheet. +-- Every color value needs to have at least the following structure: +-- +-- ``` +-- { +-- fg = "#ffffff" -- or some other rgb color +-- bg = "#000000" -- same here. +-- } +-- ``` +-- +-- There are also some optional boolean values which ore by false by default: +-- +-- * bold +-- * italic +-- * underlined + +-- The following two function are just helper functions +-- to make our life easier. +-- +-- The first function (`highlight_type`) +-- Takes a type-string, a switch-table and a fallback as arguments. +-- The switch-table shall contain all possible type-strings you might expect +-- for the variable given. The fallback is used if the type is not in the table. +-- +-- The second function (`cell_fg`) is used to color the cells in the sheetview. +-- it only takes a type-string as argument and will return a color-string according +-- to the type given. +-- +local function highlight_type(t, tbl, fallback) + local value = tbl[t] + if value == nil then + return fallback + end + return value +end + +local function cell_fg(t) + return highlight_type( + t, + { + string = "#98971a", + number = "#458588", + }, + "#cc241d" + ) +end + +-- ╺┳╸╻ ╻┏━╸┏┳┓┏━╸ ┏━┓╻ ╻┏━╸┏━╸╺┳╸╻ ╻╻┏━╸╻ ╻ +-- ┃ ┣━┫┣╸ ┃┃┃┣╸ ┗━┓┣━┫┣╸ ┣╸ ┃ ┃┏┛┃┣╸ ┃╻┃ +-- ╹ ╹ ╹┗━╸╹ ╹┗━╸ ┗━┛╹ ╹┗━╸┗━╸ ╹ ┗┛ ╹┗━╸┗┻┛ +-- +-- In this section we are going to style the sheetview. +-- That is the part where the spreadsheet will be showed to you. +-- +-- It is important to know that every color value can be ether a +-- color value as described above or a function which returns such kind +-- of value (This applies also to all other kind of values as we will +-- see later in this file). +-- +neosheet.config.theme.view.cursor = function(cell) + return { + fg = cell_fg(type(cell.value)), + bg = "#ebdbb2", + bold = true + } +end + +-- Let's create a chessboard-pattern for the cells and selection. +-- such that it looks cleaner and we can see the columns and rows better. +neosheet.config.theme.view.cell = function(cell) + local theme = { + fg = cell_fg(type(cell.value)) + } + + if ((cell.row + cell.column) % 2 == 0) then + theme.bg = "#282828"; + else + theme.bg = "#3c3836"; + end + + return theme +end + +neosheet.config.theme.view.selection = function(cell) + local theme = { + fg = cell_fg(type(cell.value)), + } + + if ((cell.row + cell.column) % 2 == 0) then + theme.bg = "#665c54"; + else + theme.bg = "#504945"; + end + + return theme +end + +-- For the background where no cell is drawn we can also use just a static value. +neosheet.config.theme.view.background = { bg = "#32302f" } + + +-- ╺┳╸╻ ╻┏━╸┏┳┓┏━╸ ┏━┓╻ ╻┏━╸┏━╸╺┳╸╻ ╻╻┏━╸╻ ╻ ┏┓ ┏━┓┏━┓ +-- ┃ ┣━┫┣╸ ┃┃┃┣╸ ┗━┓┣━┫┣╸ ┣╸ ┃ ┃┏┛┃┣╸ ┃╻┃ ┣┻┓┣━┫┣┳┛ +-- ╹ ╹ ╹┗━╸╹ ╹┗━╸ ┗━┛╹ ╹┗━╸┗━╸ ╹ ┗┛ ╹┗━╸┗┻┛ ┗━┛╹ ╹╹┗╸ +-- +-- Now let's style the bar which is attached to the +-- bottom of the sheetview. +-- +-- As a side note: Every bar element in neosheet +-- will be styled the same way. +-- +neosheet.config.theme.view.bar.left = { + fg = "#ebdbb2", + bg = "#b16286" +} +neosheet.config.theme.view.bar.middle = { + fg = "#ebdbb2", + bg = "#1d2021" +} + +-- We can also display the current mode on the left side +-- of the bar with a function. +neosheet.state.view.bar.left = function(mode) + return " " .. string.upper(mode) .. " " +end + +neosheet.state.view.bar.middle = " " + +-- ╺┳╸╻ ╻┏━╸┏┳┓┏━╸ ┏━╸╺┳┓╻╺┳╸┏━┓┏━┓ +-- ┃ ┣━┫┣╸ ┃┃┃┣╸ ┣╸ ┃┃┃ ┃ ┃ ┃┣┳┛ +-- ╹ ╹ ╹┗━╸╹ ╹┗━╸ ┗━╸╺┻┛╹ ╹ ┗━┛╹┗╸ +-- +-- The editor is the part of neosheet were you can +-- edit your more complex formulas and transformation scripts. +-- +neosheet.config.theme.editor.background = { bg = "#282828" } +neosheet.config.theme.editor.cursor_line = { bg = "#32302f" } +neosheet.config.theme.editor.line_number = { fg = "#a89984" } +neosheet.config.theme.editor.active_line_number = { + fg = "#a89984", bg = "#504945", bold = true +} + +-- ╺┳╸╻ ╻┏━╸┏┳┓┏━╸ ┏━╸╺┳┓╻╺┳╸┏━┓┏━┓ ┏┓ ┏━┓┏━┓ +-- ┃ ┣━┫┣╸ ┃┃┃┣╸ ┣╸ ┃┃┃ ┃ ┃ ┃┣┳┛ ┣┻┓┣━┫┣┳┛ +-- ╹ ╹ ╹┗━╸╹ ╹┗━╸ ┗━╸╺┻┛╹ ╹ ┗━┛╹┗╸ ┗━┛╹ ╹╹┗╸ +-- +-- The editor also does have a bar on the bottom. +-- It is styled the same way as we did on sheetview. +-- +neosheet.config.theme.editor.bar.left = { + fg = "#ebdbb2", + bg = "#b16286" +} +neosheet.config.theme.editor.bar.middle = { + fg = "#ebdbb2", + bg = "#1d2021" +} + +-- ╺┳╸╻ ╻┏━╸┏┳┓┏━╸ ┏━╸╺┳┓╻╺┳╸┏━┓┏━┓ ╻ ╻╻┏━╸╻ ╻╻ ╻┏━╸╻ ╻╺┳╸ +-- ┃ ┣━┫┣╸ ┃┃┃┣╸ ┣╸ ┃┃┃ ┃ ┃ ┃┣┳┛ ┣━┫┃┃╺┓┣━┫┃ ┃┃╺┓┣━┫ ┃ +-- ╹ ╹ ╹┗━╸╹ ╹┗━╸ ┗━╸╺┻┛╹ ╹ ┗━┛╹┗╸ ╹ ╹╹┗━┛╹ ╹┗━╸╹┗━┛╹ ╹ ╹ +-- +-- In this section we can set the colors of the syntax highlighting +-- in the editor. +-- +neosheet.config.theme.editor.highlight = { + attribute = { fg = "#a89984" }, + boolean = { fg = "#690d6a" }, + comment = { fg = "#665c54" }, + conditional = { fg = "#fabdb2f" }, + constant = { fg = "#458588" }, + constant_builtin = { fg = "#458588" }, + constructor = { fg = "#ebdbb2" }, + field = { fg = "#a89984" }, + ['function'] = { fg = "#ebdbb2" }, + function_builtin = { fg = "#ebdbb2" }, + function_call = { fg = "#ebdbb2" }, + keyword = { fg = "#fabdb2f" }, + keyword_function = { fg = "#fabd2f" }, + keyword_operator = { fg = "#fabd2f" }, + keyword_return = { fg = "#fabd2f" }, + label = { fg = "#ebdbb2" }, + method = { fg = "#83a598" }, + method_call = { fg = "#83a598" }, + number = { fg = "#458588" }, + operator = { fg = "#a89984" }, + parameter = { fg = "#ebdbb2" }, + preproc = { fg = "#ebdbb2" }, + punctuation_bracket = { fg = "#a89984" }, + punctuation_delimiter = { fg = "#a89984" }, + ['repeat'] = { fg = "#fabdb2f" }, + string = { fg = "#8ec07c" }, + string_escape = { fg = "#fabdb2f" }, + variable = { fg = "#ebdbb2" }, + variable_builtin = { fg = "#ebdbb2" }, +} + + +-- ╻┏ ┏━╸╻ ╻┏┳┓┏━┓┏━┓ +-- ┣┻┓┣╸ ┗┳┛┃┃┃┣━┫┣━┛ +-- ╹ ╹┗━╸ ╹ ╹ ╹╹ ╹╹ +-- +-- Let's define some keymaps so we can actually navigate +-- in neosheet. +-- +-- The keymaps are configured under `neosheet.config.keymap` +-- there is always the `map` function which takes a key-combination +-- and a function as a argument. +-- + +-- ╻┏ ┏━╸╻ ╻┏┳┓┏━┓┏━┓ ┏━┓╻ ╻┏━╸┏━╸╺┳╸╻ ╻╻┏━╸╻ ╻ +-- ┣┻┓┣╸ ┗┳┛┃┃┃┣━┫┣━┛ ┗━┓┣━┫┣╸ ┣╸ ┃ ┃┏┛┃┣╸ ┃╻┃ +-- ╹ ╹┗━╸ ╹ ╹ ╹╹ ╹╹ ┗━┛╹ ╹┗━╸┗━╸ ╹ ┗┛ ╹┗━╸┗┻┛ + +neosheet.config.keymap.view.map('j', function() + neosheet.state.view.move_cursor('down') +end) + +neosheet.config.keymap.view.map('k', function() + neosheet.state.view.move_cursor('up') +end) + +neosheet.config.keymap.view.map('h', function() + neosheet.state.view.move_cursor('left') +end) + +neosheet.config.keymap.view.map('l', function() + neosheet.state.view.move_cursor('right') +end) + +neosheet.config.keymap.view.map('', function() + neosheet.state.view.move_cursor('begin') +end) + +neosheet.config.keymap.view.map('', function() + neosheet.state.view.move_cursor('end') +end) + +neosheet.config.keymap.view.map('', function() + neosheet.state.view.move_cursor('top') +end) + +neosheet.config.keymap.view.map('', function() + neosheet.state.view.move_cursor('bottom') +end) + +neosheet.config.keymap.view.map(':', function() + neosheet.state.view.mode = 'command' +end) + +neosheet.config.keymap.view.map('i', function() + neosheet.state.view.mode = 'insert' +end) + +neosheet.config.keymap.view.map('v', function() + if (neosheet.state.view.mode == 'visual') then + neosheet.state.view.mode = 'normal' + else + neosheet.state.view.mode = 'visual' + end +end) + +neosheet.config.keymap.view.map('s', function() + neosheet.state.active_window = 'editor' + neosheet.state.editor.lines = + [[require('neosheet').state.view.active:map(function(cell) + return '' +end)]] +end) + +neosheet.config.keymap.view.map('', function() + neosheet.state.view.mode = 'normal' +end) + +neosheet.config.keymap.global.map('', function() + neosheet.state.quit() +end) + +neosheet.config.keymap.global.map('', function() + neosheet.state.log.visible = not neosheet.state.log.visible +end) + +neosheet.config.keymap.global.map('', function() + local sheet = neosheet.sheet.open('./sample-large.csv') + neosheet.state.view.active = sheet +end) + +-- ╻┏ ┏━╸╻ ╻┏┳┓┏━┓┏━┓ ┏━╸╺┳┓╻╺┳╸┏━┓┏━┓ +-- ┣┻┓┣╸ ┗┳┛┃┃┃┣━┫┣━┛ ┣╸ ┃┃┃ ┃ ┃ ┃┣┳┛ +-- ╹ ╹┗━╸ ╹ ╹ ╹╹ ╹╹ ┗━╸╺┻┛╹ ╹ ┗━┛╹┗╸ +-- +-- Let's add vim-motions to the editor. + +local mode = nil +local function set_mode(m) + mode = m + if mode == 'normal' then + neosheet.state.editor.cursor_shape = 'block' + neosheet.config.keymap.editor.default = false + elseif mode == 'insert' then + neosheet.state.editor.cursor_shape = 'bar' + neosheet.config.keymap.editor.default = true + end + + neosheet.state.editor.bar.left = " "..string.upper(mode).." " +end + +set_mode('normal') + +neosheet.config.keymap.editor.map('', function() + neosheet.state.editor.move_cursor('up') +end) + +neosheet.config.keymap.editor.map('', function() + neosheet.state.editor.move_cursor('down') +end) + +neosheet.config.keymap.editor.map('', function() + neosheet.state.editor.move_cursor('left') +end) + +neosheet.config.keymap.editor.map('', function() + neosheet.state.editor.move_cursor('right') +end) + +neosheet.config.keymap.editor.map('', function() + neosheet.state.editor.visible = false; +end) + +neosheet.config.keymap.editor.map('', function() + set_mode('normal') +end) + +neosheet.config.keymap.editor.map('i', function() + if mode == 'normal' then + set_mode('insert') + else + return true + end +end) + +neosheet.config.keymap.editor.map('a', function() + if mode == 'normal' then + set_mode('insert') + neosheet.state.editor.move_cursor('right') + else + return true + end +end) + +neosheet.config.keymap.editor.map('k', function() + if mode == 'normal' then + neosheet.state.editor.move_cursor('up') + else + return true + end +end) + +neosheet.config.keymap.editor.map('j', function() + if mode == 'normal' then + neosheet.state.editor.move_cursor('down') + else + return true + end +end) + +neosheet.config.keymap.editor.map('h', function() + if mode == 'normal' then + neosheet.state.editor.move_cursor('left') + else + return true + end +end) + +neosheet.config.keymap.editor.map('l', function() + if mode == 'normal' then + neosheet.state.editor.move_cursor('right') + else + return true + end +end) + +neosheet.config.keymap.editor.map('', function() + neosheet.state.editor.run() +end) -- cgit v1.2.3-70-g09d2