Skip to content

Commit

Permalink
conf
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Feb 23, 2019
1 parent cdb1b88 commit 6e6f3b0
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 18 deletions.
4 changes: 3 additions & 1 deletion croissant-0.0.1-4.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ build = {
modules = {
["croissant"] = "croissant/init.lua",
["croissant.luaprompt"] = "croissant/luaprompt.lua",
["croissant.lexer"] = "croissant/lexer.lua"
["croissant.lexer"] = "croissant/lexer.lua",
["croissant.help"] = "croissant/help.lua",
["croissant.conf"] = "croissant/conf.lua",
},
type = "builtin",
install = {
Expand Down
125 changes: 125 additions & 0 deletions croissant/conf.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
local colors = require "term.colors"
local char = require "sirocco.char"

local merge
function merge(t1, t2, seen)
seen = seen or {}
local merged = {}

seen[t1] = true
seen[t2] = true

for k, v in pairs(t1) do
merged[k] = v
end

for k, v in pairs(t2) do
if type(v) == "table" and not seen[v] then
seen[v] = true

if type(merged[k]) ~= "table" then
merged[k] = {}
end

merged[k] = merge(merged[k], v, seen)
else
merged[k] = v
end
end

return merged
end

local default = {
-- TODO: unused
keybinding = {
command_get_next_history = {
"key_down",
"C-n",
},

command_get_previous_history = {
"key_up",
"C-p",
},

command_exit = {
"C-c"
},

command_abort = {
"C-g"
},

command_help = {
"C- ",
"M- ",
},
},

prompt = "",
continuationPrompt = ".... ",

historyLimit = 1000,

syntaxColors = {
constant = { "bright", "yellow" },
string = { "green" },
comment = { "dim", "cyan" },
number = { "yellow" },
operator = { "yellow" },
keywords = { "bright", "magenta" },
identifier = { "blue" },
},

help = "croissant.help"
}

-- Read from ~/.croissantrc
local user = {}
local file, _ = io.open(os.getenv "HOME" .. "/.croissantrc", "r")

if file then
local rc = file:read("*all")

file:close()

rc = load(rc) or load("return " .. rc)

if rc then
user = rc()
end
end

-- Merge default and user
local conf = merge(default, user)

-- Convert colors to escape codes
for k, v in pairs(conf.syntaxColors) do
local color = ""

for _, c in ipairs(v) do
color = color .. colors[c]
end

conf.syntaxColors[k] = color
end

-- Convert keybding to escape codes
for command, bindings in pairs(conf.keybinding) do
local bds = {}

for _, key in ipairs(bindings) do
local prefix, suffix = key:match "^([CM])-(.*)"

if prefix then
table.insert(bds, char[prefix](suffix))
else
table.insert(bds, key)
end
end

conf.keybinding[command] = bds
end

return conf
10 changes: 7 additions & 3 deletions croissant/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local colors = require "term.colors"
local conf = require "croissant.conf"

local LuaPrompt = require "croissant.luaprompt"

Expand Down Expand Up @@ -64,16 +65,19 @@ return function()
local history = {}
local multiline = false
local finished = false
local help = require(conf.help)

_G.quit = function()
finished = true
end

while not finished do
local code = LuaPrompt {
prompt = multiline and ".... ",
multiline = multiline,
history = history
prompt = multiline and conf.continuationPrompt or conf.prompt,
multiline = multiline,
history = history,
tokenColors = conf.syntaxColors,
help = help
}:ask()

table.insert(history, code)
Expand Down
20 changes: 6 additions & 14 deletions croissant/luaprompt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,9 @@ LuaPrompt = Class {
self.tokens = {}
self.lexer = Lexer()

self.tokenColors = options.colors or {
constant = colors.bright .. colors.yellow,
string = colors.green,
comment = colors.dim .. colors.cyan,
number = colors.yellow,
operator = colors.yellow,
keywords = colors.bright .. colors.magenta,
identifier = colors.blue,
}
self.tokenColors = options.tokenColors

self.help = options.help
end

}
Expand Down Expand Up @@ -69,7 +63,8 @@ function LuaPrompt:registerKeybinding()
}

self.keybinding.command_help = {
C " "
C " ",
Esc " ",
}
end

Expand Down Expand Up @@ -230,13 +225,10 @@ local function trim(str)
end

function LuaPrompt:command_help()
-- Could be expensive do it only when help is used
local help = require "croissant.help"

local currentToken = self:getCurrentToken()

if currentToken.kind == "identifier" then
local doc = help[currentToken.text]
local doc = self.help[currentToken.text]

if doc then
self.message =
Expand Down

0 comments on commit 6e6f3b0

Please sign in to comment.