Skip to content

Commit

Permalink
Add one line mode to TextEditor and use it in notes
Browse files Browse the repository at this point in the history
  • Loading branch information
wiktor-obrebski committed Sep 4, 2024
1 parent 0da7101 commit 7fd812c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
46 changes: 31 additions & 15 deletions internal/journal/text_editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ TextEditor.ATTRS{
select_pen = COLOR_CYAN,
on_text_change = DEFAULT_NIL,
on_cursor_change = DEFAULT_NIL,
one_line_mode = false,
debug = false
}

Expand All @@ -104,25 +105,27 @@ function TextEditor:init()
TextEditorView{
view_id='text_area',
frame={l=0,r=3,t=0},
text = self.init_text,
text=self.init_text,

text_pen = self.text_pen,
ignore_keys = self.ignore_keys,
select_pen = self.select_pen,
debug = self.debug,
text_pen=self.text_pen,
ignore_keys=self.ignore_keys,
select_pen=self.select_pen,
debug=self.debug,
one_line_mode=self.one_line_mode,

on_text_change = function (val)
on_text_change=function (val)
self:updateLayout()
if self.on_text_change then
self.on_text_change(val)
end
end,
on_cursor_change = self:callback('onCursorChange')
on_cursor_change=self:callback('onCursorChange')
},
widgets.Scrollbar{
view_id='scrollbar',
frame={r=0,t=1},
on_scroll=self:callback('onScrollbar')
on_scroll=self:callback('onScrollbar'),
visible=not self.one_line_mode
}
}
self:setFocus(true)
Expand Down Expand Up @@ -251,6 +254,7 @@ TextEditorView.ATTRS{
on_cursor_change = DEFAULT_NIL,
enable_cursor_blink = true,
debug = false,
one_line_mode = false,
history_size = 10,
}

Expand All @@ -273,6 +277,8 @@ function TextEditorView:init()
bold=true
})

self.text = self:normalizeText(self.text)

self.wrapped_text = wrapped_text.WrappedText{
text=self.text,
wrap_width=256
Expand All @@ -281,6 +287,14 @@ function TextEditorView:init()
self.history = TextEditorHistory{history_size=self.history_size}
end

function TextEditorView:normalizeText(text)
if self.one_line_mode then
return text:gsub("\r?\n", "")
end

return text
end

function TextEditorView:setRenderStartLineY(render_start_line_y)
self.render_start_line_y = render_start_line_y
end
Expand Down Expand Up @@ -410,7 +424,7 @@ end

function TextEditorView:setText(text)
local changed = self.text ~= text
self.text = text
self.text = self:normalizeText(text)

self:recomputeLines()

Expand Down Expand Up @@ -782,12 +796,14 @@ end
function TextEditorView:onTextManipulationInput(keys)
if keys.SELECT then
-- handle enter
self.history:store(
HISTORY_ENTRY.WHITESPACE_BLOCK,
self.text,
self.cursor
)
self:insert(NEWLINE)
if not self.one_line_mode then
self.history:store(
HISTORY_ENTRY.WHITESPACE_BLOCK,
self.text,
self.cursor
)
self:insert(NEWLINE)
end

return true

Expand Down
10 changes: 8 additions & 2 deletions notes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ local overlay = require('plugins.overlay')
local guidm = require('gui.dwarfmode')
local text_editor = reqscript('internal/journal/text_editor')

local green_pin = dfhack.textures.loadTileset('hack/data/art/note-green-pin.png', 32, 32, true)
local green_pin = dfhack.textures.loadTileset(
'hack/data/art/note_green_pin_map.png',
32,
32,
true
)

NotesOverlay = defclass(NotesOverlay, overlay.OverlayWidget)
NotesOverlay.ATTRS{
Expand Down Expand Up @@ -171,7 +176,8 @@ function NoteManager:init()
frame={t=1,h=3},
frame_style=gui.FRAME_INTERIOR,
init_text=self.note and self.note.point.name or '',
init_cursor=1
init_cursor=1,
one_line_mode=true
},
widgets.HotkeyLabel {
key='CUSTOM_ALT_C',
Expand Down
5 changes: 4 additions & 1 deletion test/gui/journal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ local function arrange_empty_journal(options)
gui_journal.main({
save_prefix='test:',
save_on_change=options.save_on_change or false,
save_layout=options.allow_layout_restore or false
save_layout=options.allow_layout_restore or false,
})

local journal = gui_journal.view
Expand Down Expand Up @@ -3068,3 +3068,6 @@ function test.show_tutorials_on_first_use()
expect.str_find('Section 1\n', read_rendered_text(toc_panel));
journal:dismiss()
end

-- TODO: separate journal tests from TextEditor tests
-- add "one_line_mode" tests

0 comments on commit 7fd812c

Please sign in to comment.