diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-12-07 09:54:22 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-12-07 09:54:22 +0100 |
| commit | 53a9172bddbb83a1c570cc3fed794e7b73c1f1d7 (patch) | |
| tree | 19640c3d641ba999734043a476fe6e43ea2e0b0d /src | |
| parent | 80c511045ab4248a88d260ed0be6649196ad4e68 (diff) | |
add default config
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.rs | 10 | ||||
| -rw-r--r-- | src/init.lua | 406 | ||||
| -rw-r--r-- | src/lua/mod.rs | 4 | ||||
| -rw-r--r-- | src/state/view/mode.rs | 3 |
4 files changed, 416 insertions, 7 deletions
@@ -49,10 +49,12 @@ impl App { } pub fn run(mut self, terminal: &mut tui::Tui) -> io::Result<()> { - if let Err(e) = lua::source(&config::env::USER_RC_PATH) { - tui::restore()?; - println!("{}", e); - return Ok(()); + if let Err(_) = lua::source(&config::env::USER_RC_PATH) { + if let Err(e) = lua::source_default_config() { + tui::restore()?; + println!("{}", e); + return Ok(()); + } } while !{ GlobalState::get().exit } { 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('<s-h>', function() + neosheet.state.view.move_cursor('begin') +end) + +neosheet.config.keymap.view.map('<s-l>', function() + neosheet.state.view.move_cursor('end') +end) + +neosheet.config.keymap.view.map('<s-k>', function() + neosheet.state.view.move_cursor('top') +end) + +neosheet.config.keymap.view.map('<s-j>', 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('<esc>', function() + neosheet.state.view.mode = 'normal' +end) + +neosheet.config.keymap.global.map('<c-q>', function() + neosheet.state.quit() +end) + +neosheet.config.keymap.global.map('<c-l>', function() + neosheet.state.log.visible = not neosheet.state.log.visible +end) + +neosheet.config.keymap.global.map('<c-o>', 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('<up>', function() + neosheet.state.editor.move_cursor('up') +end) + +neosheet.config.keymap.editor.map('<down>', function() + neosheet.state.editor.move_cursor('down') +end) + +neosheet.config.keymap.editor.map('<left>', function() + neosheet.state.editor.move_cursor('left') +end) + +neosheet.config.keymap.editor.map('<right>', function() + neosheet.state.editor.move_cursor('right') +end) + +neosheet.config.keymap.editor.map('<c-c>', function() + neosheet.state.editor.visible = false; +end) + +neosheet.config.keymap.editor.map('<esc>', 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('<c-r>', function() + neosheet.state.editor.run() +end) diff --git a/src/lua/mod.rs b/src/lua/mod.rs index e7c3352..ae619b0 100644 --- a/src/lua/mod.rs +++ b/src/lua/mod.rs @@ -87,3 +87,7 @@ pub fn source(path: &str) -> LuaResult<()> { ))) } } + +pub fn source_default_config() -> LuaResult<()> { + get().load(include_str!("../init.lua")).exec() +} diff --git a/src/state/view/mode.rs b/src/state/view/mode.rs index 63e6e5b..c030197 100644 --- a/src/state/view/mode.rs +++ b/src/state/view/mode.rs @@ -5,7 +5,6 @@ pub enum Mode { #[default] Normal, Visual, - Insert, Command, } @@ -18,7 +17,6 @@ impl<'lua> FromLua<'lua> for Mode { match value.as_str().unwrap().to_lowercase().as_ref() { "normal" => Ok(Mode::Normal), "visual" => Ok(Mode::Visual), - "insert" => Ok(Mode::Insert), "command" => Ok(Mode::Command), _ => Err(mlua::Error::runtime( "mode needs to be 'normal', 'visual', 'insert' or 'command'", @@ -40,7 +38,6 @@ impl<'lua> IntoLua<'lua> for Mode { match self { Mode::Normal => "normal", Mode::Visual => "visual", - Mode::Insert => "insert", Mode::Command => "command", } .into_lua(lua) |