From e55f25045d0db2ac44ed1b269b954273ac453a88 Mon Sep 17 00:00:00 2001 From: siduck Date: Sun, 26 Jan 2025 16:23:58 +0530 Subject: [PATCH 1/2] feat: phrases toggle #20 --- lua/typr/api.lua | 29 ++++++++++++++------ lua/typr/constants/phrases.lua | 48 ++++++++++++++++++++++++++++++++++ lua/typr/mappings.lua | 1 + lua/typr/state.lua | 4 ++- lua/typr/ui/init.lua | 3 ++- lua/typr/utils.lua | 38 +++++++++++++++++++++++++-- 6 files changed, 111 insertions(+), 12 deletions(-) create mode 100644 lua/typr/constants/phrases.lua diff --git a/lua/typr/api.lua b/lua/typr/api.lua index fe6122a..44127f3 100644 --- a/lua/typr/api.lua +++ b/lua/typr/api.lua @@ -3,25 +3,38 @@ local volt = require "volt" local state = require "typr.state" local utils = require "typr.utils" -M.toggle_symbols = function() - state.config.symbols = not state.config.symbols +M.redraw_words_header = function(mode) + state.config.mode = mode or "words" volt.redraw(state.buf, "headerbtns") utils.gen_default_lines() volt.redraw(state.buf, "words") end +M.toggle_symbols = function() + state.config.symbols = not state.config.symbols + M.redraw_words_header() +end + M.toggle_numbers = function() state.config.numbers = not state.config.numbers - volt.redraw(state.buf, "headerbtns") - utils.gen_default_lines() - volt.redraw(state.buf, "words") + M.redraw_words_header() end M.toggle_random = function() state.config.random = not state.config.random - volt.redraw(state.buf, "headerbtns") - utils.gen_default_lines() - volt.redraw(state.buf, "words") + M.redraw_words_header() +end + +M.toggle_phrases = function() + state.config.mode = state.config.mode == "phrases" and "words" or "phrases" + + if state.config.mode == "phrases" then + state.config.numbers = false + state.config.symbols = false + state.config.random = false + end + + M.redraw_words_header(state.config.mode) end M.set_linecount = function(x) diff --git a/lua/typr/constants/phrases.lua b/lua/typr/constants/phrases.lua new file mode 100644 index 0000000..9c0739f --- /dev/null +++ b/lua/typr/constants/phrases.lua @@ -0,0 +1,48 @@ +return { + "On a chilly autumn evening the sound of leaves rustling in the wind filled the air as the neighborhood prepared for war.", + "After a long and grueling day at work Jane finally sat down on her favorite armchair sipping her hot tea while enjoying the warmth of the crackling fireplace", + "The library was a sanctuary for those seeking knowledge with its towering shelves filled with books from every genre imaginable and the faint scent of old paper lingering in the air", + "Under the vast starlit sky the group of hikers set up their tents and gathered around a campfire sharing stories and laughter while the wilderness enveloped them", + "The bustling city was alive with energy as the sound of car horns chatter and birds blended together creating a symphony of urban life that never seemed to sleep", + "Through the winding mountain paths a lone traveler carried a heavy backpack filled with essentials navigating the rugged terrain with determination and grit", + "As the waves crashed onto the shore the beach was dotted with children building sandcastles and families enjoying the warm sunlight and cool ocean breeze", + "The small village nestled in the valley was surrounded by rolling hills covered in a patchwork of green and gold fields stretching as far as the eye could see", + "In the depths of the forest the air was filled with the chirping of birds and the soft rustle of leaves as squirrels darted from tree to tree gathering food", + "The old train station stood as a relic of the past its wooden benches worn smooth by time and its walls echoing with the memories of countless arrivals and departures", + "The golden rays of the morning sun bathed the landscape in a warm glow as dew glistened on the grass and birds greeted the day with melodic chirping", + "The massive oak tree in the center of the park provided shade to countless visitors over the years its roots deeply entwined in the history of the town", + "The bakery on the corner was famous for its freshly baked bread and pastries filling the air with an aroma that tempted passersby to stop and indulge", + "The clear blue waters of the lake reflected the towering mountains surrounding it creating a picturesque scene that felt like it belonged in a painting", + "The cobblestone streets of the old town were lined with quaint shops and cafes bustling with people enjoying the charming atmosphere", + "The garden was a burst of colors with vibrant flowers blooming in every shade imaginable attracting bees and butterflies that danced from petal to petal", + "The old wooden bridge creaked underfoot as the hiker crossed it carefully marveling at the serene river flowing gently beneath", + "The summer fair was a lively celebration with rides games and food stalls scattered across the field drawing families from neighboring towns", + "The quiet beach at sunset was a perfect place for reflection as the waves gently lapped at the shore and the sky turned shades of orange and pink", + "The mountain cabin was a cozy retreat surrounded by snow-covered trees offering a peaceful escape from the hustle and bustle of everyday life", + "The bustling marketplace was a sensory delight with vibrant stalls selling fresh produce handmade crafts and an array of exotic spices", + "The train ride through the countryside was a scenic journey offering glimpses of lush green fields grazing cattle and quaint farmhouses", + "The grand old mansion stood at the end of the road its towering windows and intricate architecture hinting at a bygone era of elegance", + "The rolling hills were blanketed in wildflowers swaying gently in the breeze as the sun cast a golden hue across the landscape", + "The narrow alleyway was alive with activity as street vendors called out to passersby offering everything from fresh fruit to handmade jewelry", + "The serene forest trail was lined with tall trees their leaves forming a natural canopy that filtered the sunlight into soft patterns on the ground", + "The coastal cliffs offered breathtaking views of the vast ocean below with seagulls gliding effortlessly through the salty breeze", + "The charming village square was the heart of the community with its fountain surrounded by benches where residents gathered to share stories", + "The quiet library offered a peaceful escape with its rows of books and comfortable seating inviting visitors to lose themselves in a story", + "The old lighthouse stood steadfast against the crashing waves its beacon of light guiding ships safely through the treacherous waters", + "The quaint coffee shop on the corner was a favorite spot for locals to catch up over steaming cups of freshly brewed coffee", + "The vibrant street art on the walls of the urban neighborhood added a splash of color and creativity to the otherwise grey surroundings", + "The gentle stream meandered through the meadow its crystal-clear waters reflecting the blue sky and fluffy white clouds above", + "The bustling harbor was a hub of activity with fishermen unloading their catch and merchants haggling over prices", + "The sprawling vineyard stretched as far as the eye could see its rows of grapevines promising a bountiful harvest", + "The tranquil lake was a haven for wildlife with ducks gliding across the water and dragonflies flitting about in the sunlight", + "The ancient ruins were a testament to a once-great civilization with crumbling walls and intricate carvings hinting at its former glory", + "The peaceful hiking trail wound its way through the mountains offering breathtaking views of the valleys below", + "The small fishing village was a picturesque scene with colorful boats bobbing in the harbor and nets drying in the sun", + "The quaint bookstore was a treasure trove for bibliophiles with its shelves packed with rare and fascinating titles waiting to be discovered", + "The riverside picnic was a delightful affair with friends sharing food laughter and the beauty of the surrounding nature", + "The historic castle on the hill stood as a reminder of times long past its stone walls echoing with the stories of generations", + "The charming garden gathering was filled with laughter as guests mingled under strings of twinkling lights and enjoyed delicious treats", + "The peaceful countryside was a welcome escape from the noise of the city with rolling hills and endless skies stretching into the horizon", + "The bustling amusement park was filled with laughter and excitement as children ran from ride to ride clutching balloons and cotton candy", + "The tranquil mountain lake was a serene escape its glassy surface reflecting the surrounding peaks and the occasional ripple from a jumping fish", +} diff --git a/lua/typr/mappings.lua b/lua/typr/mappings.lua index 137d614..cda87a4 100644 --- a/lua/typr/mappings.lua +++ b/lua/typr/mappings.lua @@ -17,6 +17,7 @@ return function() map("n", "s", myapi.toggle_symbols, { buffer = state.buf }) map("n", "n", myapi.toggle_numbers, { buffer = state.buf }) map("n", "r", myapi.toggle_random, { buffer = state.buf }) + map("n", "p", myapi.toggle_phrases, { buffer = state.buf }) for _, v in ipairs { 3, 6, 9 } do map("n", tostring(v), function() diff --git a/lua/typr/state.lua b/lua/typr/state.lua index f243db1..0a77843 100644 --- a/lua/typr/state.lua +++ b/lua/typr/state.lua @@ -3,7 +3,7 @@ local M = { xpad = 2, w = 80, h = 20, - linecount = 1, + linecount = 2, default_lines = {}, ui_lines = {}, lastchar = nil, @@ -21,12 +21,14 @@ local M = { ----------------------- user config ----------------------- config = { + mode = "phrases", -- words, phrases winlayout = "responsive", kblayout = "qwerty", wpm_goal = 120, numbers = false, symbols = false, random = false, + phrases = nil, -- can be a table of strings insert_on_start = false, stats_filepath = vim.fn.stdpath "data" .. "/typrstats", mappings = nil, diff --git a/lua/typr/ui/init.lua b/lua/typr/ui/init.lua index a321fd7..211c107 100644 --- a/lua/typr/ui/init.lua +++ b/lua/typr/ui/init.lua @@ -13,8 +13,9 @@ M.headerbtns = function() { " Symbols ", config.symbols and "exgreen" or "normal" }, { "  Numbers ", config.numbers and "exgreen" or "normal" }, { "  Random ", config.random and "exgreen" or "normal" }, + { "  Phrases ", config.mode == 'phrases' and "exgreen" or "normal" }, { "_pad_" }, - { " Lines ", "exred" }, + { "Lines ", "exred" }, { " 3 *", state.linecount == 3 and "" or "commentfg" }, { " 6 *", state.linecount == 6 and "" or "commentfg" }, { " 9", state.linecount == 9 and "" or "commentfg" }, diff --git a/lua/typr/utils.lua b/lua/typr/utils.lua index 385a81c..c693964 100644 --- a/lua/typr/utils.lua +++ b/lua/typr/utils.lua @@ -1,6 +1,7 @@ local M = {} local state = require "typr.state" local words = require "typr.constants.words" +local phrases = require "typr.constants.phrases" local volt = require "volt" local symbols = { @@ -56,11 +57,12 @@ M.gen_word = function() local word local frequency = math.random(1, 4) local config = state.config + local wordslen = #words if frequency == 4 and state.config.numbers then word = tostring(math.random(1, 1000)) else - word = config.random and gen_random_word() or words[math.random(1, #words)] + word = config.random and gen_random_word() or words[math.random(1, wordslen)] end if state.config.symbols then @@ -95,8 +97,40 @@ M.words_to_lines = function() return lines end +M.phrases_to_lines = function() + local data = state.config.phrases or phrases + local maxw = state.w_with_pad + local datalen = #data + local phrase = data[math.random(1, datalen)] + local lines = { "" } + + while state.linecount >= #lines do + for _, v in ipairs(vim.split(phrase, " ")) do + local arrlen = #lines + local lastline_len = #lines[arrlen] + + if lastline_len + #v + 1 > maxw then + table.insert(lines, v) + else + local space = lastline_len == 0 and "" or " " + lines[arrlen] = lines[arrlen] .. space .. v + end + end + + phrase = data[math.random(1, datalen)] + end + + return vim.list_slice(lines, 1, state.linecount) +end + +M.mode_funcs = { + words = M.words_to_lines, + phrases = M.phrases_to_lines, +} + M.gen_default_lines = function() - state.default_lines = M.words_to_lines() + state.default_lines = M.mode_funcs[state.config.mode]() + local ui_lines = {} for _, v in ipairs(state.default_lines) do From b17287a47c0c95ce815820b2b38c59698b1cf64c Mon Sep 17 00:00:00 2001 From: siduck Date: Mon, 27 Jan 2025 08:15:44 +0530 Subject: [PATCH 2/2] fix: use rawpm to check if test is finished #34 --- lua/typr/api.lua | 2 +- lua/typr/state.lua | 2 +- lua/typr/ui/init.lua | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lua/typr/api.lua b/lua/typr/api.lua index 44127f3..ef3c234 100644 --- a/lua/typr/api.lua +++ b/lua/typr/api.lua @@ -51,7 +51,7 @@ M.set_linecount = function(x) end M.restart = function() - if state.stats.wpm == 0 then + if state.stats.rawpm == 0 then return end diff --git a/lua/typr/state.lua b/lua/typr/state.lua index 0a77843..c6719a9 100644 --- a/lua/typr/state.lua +++ b/lua/typr/state.lua @@ -21,7 +21,7 @@ local M = { ----------------------- user config ----------------------- config = { - mode = "phrases", -- words, phrases + mode = "words", -- words, phrases winlayout = "responsive", kblayout = "qwerty", wpm_goal = 120, diff --git a/lua/typr/ui/init.lua b/lua/typr/ui/init.lua index 211c107..0432c81 100644 --- a/lua/typr/ui/init.lua +++ b/lua/typr/ui/init.lua @@ -33,7 +33,7 @@ M.stats = function() return { {}, {} } end - if stats.wpm == 0 then + if stats.rawpm == 0 then return { { { string.rep(" ", (state.w_with_pad / 2) - 3) }, { " " .. state.secs .. "s " } }, }