new fork: carousel shell

This commit is contained in:
Kartik K. Agaram 2023-11-15 06:06:56 -08:00
parent 331d5eda40
commit 01a8e0bb9f
14 changed files with 134 additions and 4 deletions

1
0009-Editor_state Normal file
View File

@ -0,0 +1 @@
Editor_state = nil

1
0010-Line_height Normal file
View File

@ -0,0 +1 @@
Line_height = 0

7
0011-on.initialize Normal file
View File

@ -0,0 +1,7 @@
on.initialize = function()
love.graphics.setFont(love.graphics.newFont(20))
Line_height = math.floor(love.graphics.getFont():getHeight()*1.3)
Line_number_padding = Line_number_width*App.width('m')
Editor_state = edit.initialize_state(100, 100, 400, love.graphics.getFont():getHeight(), Line_height)
Text.redraw_all(Editor_state)
end

4
0012-on.draw Normal file
View File

@ -0,0 +1,4 @@
on.draw = function()
love.graphics.rectangle('line', 100-5-Line_number_padding,100-5, 400+10, 200+10, 5,5)
edit.draw(Editor_state)
end

3
0013-on.keychord_press Normal file
View File

@ -0,0 +1,3 @@
on.keychord_press = function(chord, key)
edit.keychord_press(Editor_state, chord, key)
end

3
0014-on.text_input Normal file
View File

@ -0,0 +1,3 @@
on.text_input = function(t)
edit.text_input(Editor_state, t)
end

3
0015-on.key_release Normal file
View File

@ -0,0 +1,3 @@
on.key_release = function(key, scancode)
edit.key_release(Editor_state, key, scancode)
end

3
0016-on.mouse_press Normal file
View File

@ -0,0 +1,3 @@
on.mouse_press = function(x,y, mouse_button)
edit.mouse_press(Editor_state, x,y, mouse_button)
end

3
0017-on.mouse_release Normal file
View File

@ -0,0 +1,3 @@
on.mouse_release = function(x,y, mouse_button)
edit.mouse_release(Editor_state, x,y, mouse_button)
end

1
0019-Line_number_padding Normal file
View File

@ -0,0 +1 @@
Line_number_padding = 0

82
colorize.lua Normal file
View File

@ -0,0 +1,82 @@
-- State transitions while colorizing a single line.
-- Just for comments and strings.
-- Limitation: each fragment gets a uniform color so we can only change color
-- at word boundaries.
Next_state = {
normal={
{prefix='--[[', target='block_comment'}, -- only single-line for now
{prefix='--', target='comment'},
-- these don't mostly work well until we can change color within words
-- {prefix='"', target='dstring'},
-- {prefix="'", target='sstring'},
{prefix='[[', target='block_string'}, -- only single line for now
},
dstring={
{suffix='"', target='normal'},
},
sstring={
{suffix="'", target='normal'},
},
block_string={
{suffix=']]', target='normal'},
},
block_comment={
{suffix=']]', target='normal'},
},
-- comments are a sink
}
Comment_color = {r=0, g=0, b=1}
String_color = {r=0, g=0.5, b=0.5}
Divider_color = {r=0.7, g=0.7, b=0.7}
Colors = {
normal=Text_color,
comment=Comment_color,
sstring=String_color,
dstring=String_color,
block_string=String_color,
block_comment=Comment_color,
}
Current_state = 'normal'
function initialize_color()
--? print('new line')
Current_state = 'normal'
end
function select_color(frag)
--? print('before', '^'..frag..'$', Current_state)
switch_color_based_on_prefix(frag)
--? print('using color', Current_state, Colors[Current_state])
App.color(Colors[Current_state])
switch_color_based_on_suffix(frag)
--? print('state after suffix', Current_state)
end
function switch_color_based_on_prefix(frag)
if Next_state[Current_state] == nil then
return
end
frag = rtrim(frag)
for _,edge in pairs(Next_state[Current_state]) do
if edge.prefix and starts_with(frag, edge.prefix) then
Current_state = edge.target
break
end
end
end
function switch_color_based_on_suffix(frag)
if Next_state[Current_state] == nil then
return
end
frag = rtrim(frag)
for _,edge in pairs(Next_state[Current_state]) do
if edge.suffix and ends_with(frag, edge.suffix) then
Current_state = edge.target
break
end
end
end

View File

@ -2,6 +2,7 @@
Text_color = {r=0, g=0, b=0}
Cursor_color = {r=1, g=0, b=0}
Highlight_color = {r=0.7, g=0.7, b=0.9} -- selected text
Line_number_color = {r=0.4, g=0.4, b=0.4}
Margin_top = 15
Margin_left = 25
@ -11,6 +12,7 @@ utf8 = require 'utf8'
require 'file'
require 'text'
require 'colorize'
edit = {}

View File

@ -34,6 +34,8 @@ end
function App.initialize_globals()
-- tests currently mostly clear their own state
Line_number_width = 3 -- in ems
-- blinking cursor
Cursor_time = 0
@ -64,7 +66,7 @@ function App.initialize(arg)
-- TODO: app-specific stuff goes here
-- keep a few blank lines around: https://merveilles.town/@akkartik/110084833821965708
love.window.setTitle('TODO')
love.window.setTitle('Carousel Shell')
@ -141,6 +143,8 @@ function initialize_window_geometry()
App.screen.width, App.screen.height, App.screen.flags = App.screen.size()
App.screen.flags.resizable = true
App.screen.resize(App.screen.width, App.screen.height, App.screen.flags)
App.screen.width = love.window.fromPixels(App.screen.width)
App.screen.height = love.window.fromPixels(App.screen.height)
end
function App.resize(w, h)

View File

@ -16,6 +16,11 @@ function Text.draw(State, line_index, y, startpos, fg, hide_cursor)
-- wrap long lines
local final_screen_line_starting_pos = startpos -- track value to return
Text.populate_screen_line_starting_pos(State, line_index)
App.color(Line_number_color)
love.graphics.print(line_index, State.left-Line_number_width*App.width('m')+10,y)
if fg == nil then
initialize_color()
end
assert(#line_cache.screen_line_starting_pos >= 1)
for i=1,#line_cache.screen_line_starting_pos do
local pos = line_cache.screen_line_starting_pos[i]
@ -62,9 +67,17 @@ function Text.draw(State, line_index, y, startpos, fg, hide_cursor)
end
end
end
-- render fragment
App.color(fg)
App.screen.print(screen_line, State.left,y)
-- render colorized text
local x = State.left
for frag in screen_line:gmatch('%S*%s*') do
if fg then
App.color(fg)
else
select_color(frag)
end
App.screen.print(frag, x,y)
x = x+App.width(frag)
end
y = y + State.line_height
if y >= App.screen.height then
break