From aeaa7d150cacb5f858f1b516ca3e2234c68b8e55 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 23 Aug 2022 15:04:30 -0700 Subject: helper: file_exists --- file.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/file.lua b/file.lua index 9440ae8..6b36b0f 100644 --- a/file.lua +++ b/file.lua @@ -1,4 +1,14 @@ -- primitives for saving to file and loading from file +function file_exists(filename) + local infile = App.open_for_reading(filename) + if infile then + infile:close() + return true + else + return false + end +end + function load_from_disk(State) local infile = App.open_for_reading(State.filename) State.lines = load_from_file(infile) -- cgit 1.4.1-2-gfad0 From 43dfa184d6e63e17efcd0c20a1090632eba88141 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 23 Aug 2022 15:06:18 -0700 Subject: helper: trimming whitespace from strings --- text.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/text.lua b/text.lua index 6f76442..61ead17 100644 --- a/text.lua +++ b/text.lua @@ -990,3 +990,15 @@ function Text.clear_screen_line_cache(State, line_index) State.line_cache[line_index].fragments = nil State.line_cache[line_index].screen_line_starting_pos = nil end + +function trim(s) + return s:gsub('^%s+', ''):gsub('%s+$', '') +end + +function ltrim(s) + return s:gsub('^%s+', '') +end + +function rtrim(s) + return s:gsub('%s+$', '') +end -- cgit 1.4.1-2-gfad0 From 89222f86a0dc3dd774ce46afaa1ba791ed65dba9 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 23 Aug 2022 15:09:14 -0700 Subject: set color for each fragment In general it seems like good practice to minimize assumptions about the current color. --- text.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text.lua b/text.lua index 61ead17..2e3aa32 100644 --- a/text.lua +++ b/text.lua @@ -9,7 +9,6 @@ require 'text_tests' -- draw a line starting from startpos to screen at y between State.left and State.right -- return the final y, and position of start of final screen line drawn function Text.draw(State, line_index, y, startpos) - App.color(Text_color) local line = State.lines[line_index] local line_cache = State.line_cache[line_index] line_cache.starty = y @@ -20,6 +19,7 @@ function Text.draw(State, line_index, y, startpos) local screen_line_starting_pos = startpos Text.compute_fragments(State, line_index) for _, f in ipairs(line_cache.fragments) do + App.color(Text_color) local frag, frag_text = f.data, f.text local frag_len = utf8.len(frag) --? print('text.draw:', frag, 'at', line_index,pos, 'after', x,y) -- cgit 1.4.1-2-gfad0