It is currently April 24th, 2024, 12:39 pm

Word Count

Discuss the use of Lua in Script measures.
newuserguy1
Posts: 7
Joined: January 22nd, 2013, 9:35 pm

Word Count

Post by newuserguy1 »

I am planning to use a word count lua, yet I don't really know how to. I would like it to search the text file, and if there is more than one word, then load the red counter, but if there are less than one word, load the green one. below is the coding i tried, I will also post the Lua script.

Code: Select all

[h/w-check]
Measure=Script
ScriptFile=wc.lua
function wc(C:\Users\Sam\Documents\Rainmeter\Skins\Mine\Blank2\homework.txt)
if words= >1 then
Excecute [!ShowMeter Checkn] [!HideMeter Checkp] [!Redraw]
else 
Excecute [!ShowMeter Checkp] [!HideMeter Checkn] [!Redraw]
return
end
and the lua:

Code: Select all

#!/usr/bin/env lua
-- Copyright (c) 2013 the authors listed at the following URL, and/or
-- the authors of referenced articles or incorporated external code:
-- http://en.literateprograms.org/Word_count_(Lua)?action=history&offset=20070621060029
-- 
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject to
-- the following conditions:
-- 
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
-- 
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-- 
-- Retrieved from: http://en.literateprograms.org/Word_count_(Lua)?oldid=10564


tot_chars=0
tot_words=0
tot_lines=0
opts=""

function printwc(lines, words, chars, fname)
	if(string.find(opts, "l")) then io.write(string.format("% 8d", lines)) end
	if(string.find(opts, "w")) then io.write(string.format("% 8d", words)) end
	if(string.find(opts, "c")) then io.write(string.format("% 8d", chars)) end
	
	if(fname~="-") then io.write(" "); print(fname)
	else print() end
end

function wc(fname)
	chars=0
	words=0
	lines=0
	
	if(fname=="-") then it=io.lines()
	else it=io.lines(fname) end

	for line in it do
		lines=lines+1
		for word in string.gfind(line, "[^%s]+") do words=words+1 end
		chars=chars+string.len(line)+1
	end
	printwc(lines, words, chars, fname)

	tot_chars=tot_chars+chars
	tot_words=tot_words+words
	tot_lines=tot_lines+lines
end



nfiles=0

for i=1,table.getn(arg) do 
	if(nfiles==0 and string.sub(arg[i], 1, 1)=='-') then
		opts=opts .. arg[i]
	else
		if(string.len(opts)==0) then opts="lwc" end
		wc(arg[i])
		nfiles=nfiles+1
	end
end


if(string.len(opts)==0) then opts="lwc" end
if nfiles<1 then wc("-") end
if nfiles>1 then printwc(tot_lines, tot_words, tot_chars, "total") end
thanks in advance!
Last edited by newuserguy1 on January 24th, 2013, 5:44 pm, edited 2 times in total.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Word Count

Post by smurfier »

The code you have above will not work as that's just not valid Rainmeter skin code. I've also included a small script that should work. The one you posted seems to be used to write the number or words at the end of the provided file.

The UpdateDivider=-1 will make it so that the file is only checked once. This is because the script opens, reads and closes the file every update.

Code: Select all

[h/w-check]
Measure=Script
ScriptFile=wc.lua
File=C:\Users\Sam\Documents\Rainmeter\Skins\Mine\Blank2\homework.txt
IfAboveValue=1
IfAboveAction=[!ShowMeter Checkn][!HideMeter Checkp][!Redraw]
IfBelowValue=1
IfBelowAction=[!ShowMeter Checkp][!HideMeter Checkn][!Redraw]
UpdateDivider=-1
wc.lua

Code: Select all

function Update()
	local hFile = io.open(SELF:GetOption('File'), 'r')
	if hFile then
		local _, words = hFile:read('*all'):gsub('[^%s]+', '')
		hFile:close()
		return words
	else
		SKIN:Bang('!Log', 'Cannot open file', 'Error')
		return ''
	end
end -- Update
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
newuserguy1
Posts: 7
Joined: January 22nd, 2013, 9:35 pm

Re: Word Count

Post by newuserguy1 »

You do not have the required permissions to view the files attached to this post.