AwesomeWM client created/removed callback - linux

I am using awesome WM and I want to run a lua function after a client is created/deleted. Specifically, I want to change the name of a tag to the name of one of the clients that are on the tag.
I do this with a timer, but I think the best way to do this would be to register a callback function to awesomeWM that it will invoke when a client is created/removed.
Are there some hooks/callbacks that I can implement to tell awesome to do this for me?
---------------------------------------------UPDATE----------------------------------------
I tried using the signals, but i cant find the correct signal that changes calls my function AFTER the window is created and attached to the tag. I tried this with manage/unmanage tagged/untagged, and tag.new, etc, but no one helps.
Any ideas?
here is the code:
override_name_char = "<"
function tag_name_from_client(c)
if string.match(c.name, "Mozilla Firefox") then
return "Firefox"
end
if string.match(c.name, "Sublime Text") then
return "Sublime"
end
if string.match(c.name, "/bin/bash") then
return "Shell"
end
return ""
end
function tag_name_from_tag(tag)
if tag.name:match(override_name_char) then
return tag.name
end
for _, c in pairs(tag:clients()) do
return " "..tostring(awful.tag.getidx(tag)).." "..tag_name_from_client(c)
end
return tostring(awful.tag.getidx(tag))
end
function refresh_tag_name()
for s = 1, screen.count() do
for _,tag in pairs(awful.tag.gettags(s)) do
tag.name = tag_name_from_tag(tag)
end
end
end
client.connect_signal("tagged", refresh_tag_name)
client.connect_signal("untagged", refresh_tag_name)
--tag_timer = timer({timeout = 0.5})
--tag_timer:connect_signal("timeout", function()
--refresh_tag_name()
--end)
--tag_timer:start()
Thanks in advance for any help regarding this.

One of possible ways for v3.5.6, try this in your rc.lua
local naughty = require("naughty")
client.connect_signal("manage", function (c)
--filter client by class name
if c.class:lower() == "gedit" then
-- do things on client start
naughty.notify({text = "Gedit launched!"})
-- add exit signal for this client
c:connect_signal("unmanage", function() naughty.notify({text = "Gedit closed!"}) end)
end
end)

"A new client is created" is the manage signal.
"A new client was destroyed" is the unmanage signal.
So, something like the following (untested):
local function choose_name_for_tag(t)
for _, c in ipairs(t:clients() do
return "has client: " .. tostring(c.name or "unknown")
end
return "has no clients"
end
local function update_state()
for _, t in pairs(root.tags()) do
t.name = choose_name_for_tag(t)
end
end
client.connect_signal("manage", update_state)
client.connect_signal("unmanage", update_state)
tag.connect_signal("tagged", function(t)
t.name = choose_name_for_tag(t)
end)
tag.connect_signal("untagged", function(t)
t.name = choose_name_for_tag(t)
end)

Related

ServerScriptService.leaderstats:41: attempt to index nil with 'leaderstats' in roblox

This is my code
game:BindToClose(function(player)
for _, Player in pairs(game.Players:GetPlayers()) do
local playerUserId = "player_"..Player.UserId
local clicksValue = player.leaderstats.Clicks.Value
local success, errormessage = pcall(function()
clicksDataStore:SetAsync(playerUserId, clicksValue)
end)
end
end)
Please help me becouse I disconnect the game, console get me this error:
Check if player.leaderstats or clicksValue is not nil before calling:
local success, errormessage = pcall(function()
clicksDataStore:SetAsync(playerUserId, clicksValue)
DataModel:BindToClose doesn't pass any arguments to the callback. I suggest you remove the player parameter in the callback and change the player in player.leaderstats.Clicks.Value to Player

Smiles bot in Lua: matching text

This is my bot, who copies smilies in game chat.
The smiles it copies are saved in the table called "emoticons". If someone writes ":)", the bot writes ":)" and so on
The code inside the loop is for this: if someone writes, for example, ">:)", you have to make the script copy ">:)" and not just ":)"
CreateFrame, RegisterEvent, SetScript and SendChatMessage are in-game built Lua API
local emoticons = {
":)", "(:", ":))", ">:)", "0:)", ":D", ":]", ":)))", "=]", "?_?", "+.+", ":P", ":3", "^^", "roar", ":V", "D:", ":C", ".D", ".)", "o_o",
"^-^", ":PPP", ":DDD", ":D:D:D", ":DDDD", ":D:D:D:D", ":DDDDD", ":d", ":L", "<O>_<O>", "o/", "+_+", "?_?", "*0*", ":}", ";)", ":))))", "o.o", "<.<''", ":|",
":-)", "^^^^", ":D:D:D:D:D:D", ":D :D :D", "^^^", ":c", ";]", ":9", ">:|", ">.<", ";3", ";P", "T_T", ":3c", ":)))))",
"^^^^^" }
local f = CreateFrame("Frame")
f:RegisterEvent("CHAT_MSG_GUILD")
f:SetScript("OnEvent", function(self, event, text, playerName, _, channelName, playerName2, _, _, channelIndex)
local msg
local n = 0
for x, key in ipairs(emoticons) do
local l = string.len(emoticons[x])
if (string.sub(text, -l) == emoticons[x]) then
if (l > n) then
msg = emoticons[x]
n = l
end
end
end
if (msg) and (playerName ~= UnitName("player")) then
if (event == "CHAT_MSG_GUILD") then SendChatMessage(msg, "GUILD", nil, channelIndex) end
end
end)
Is there any way to improve it? For example, if someone writes
"^^^^^^"
the bot copies
"^^^^^"
which would be the same with one less "^" as it was stored in the table
My goal is that if someone writes, for example, "^^^^^^" and it is not registered in the table, the script will not respond
You're probably better off learning how to use string.gmatch
As an example, let's say you only store one instance of ':D' in your emoticons table. You can iterate through the matches and respond in kind. Here's a small example:
local text = ':D:D:D'
local count = 0
for w in string.gmatch(text, ':D') do
count = count + 1
end
if count > 0 then
local response = ''
for i = 1, count do
response = response .. ':D'
end
print(response) -- prints ':D:D:D'
end
This doesn't handle every case, but hopefully it can help you get started
:D

How to ask for user input when running ruby code on remote server with capistrano?

I want to confirm the action when running a capistrano task on a remote server:
task :do_someting do
on roles(:primary) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rails, :runner,
%Q['require "do_something"; Do::Something.()']
end
end
end
end
Where `DoSomethig looks like this:
require "highline/import"
class DoSomething
def self.call
query_db_for_objects.each do |obj|
answer = ask "Are you sure to do something with #{obj}? (y/n)"
rerun unless answer == 'y'
do_something
end
end
end
Method ask from highline gem doesn't seem to work when asking from a remote server and the command bundle exec cap production do_something hangs forever.
How can I ask for a user input from a remote server when running this capistrano task?
I was able to read the user answer from a remote server with the following ruby code
task :do_someting do
class ConfirmHandler
def on_data(command, stream_name, data, channel)
if data.to_s =~ /\?$/
prompt = Net::SSH::Prompt.default.start(type: 'confirm')
response = prompt.ask "Please enter your response (y/n)"
channel.send_data "#{response}\n"
end
end
end
on roles(:primary) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rails, :runner,
%Q['require "do_something"; Do::Something.()']
end
end
end
end
where Do::Something has ask_user method which looks the following way:
class Do::Something
def self.call
answer = ask_user
puts "Answer is: #{answer}"
end
def self.ask_user
puts 'Do something?'
`read response; echo $response`
end
end

how to stub a method that takes and argument

I'm kind of stuck (Maybe because this is my first time using Minitest) wherein I want to stub a method that takes an argument.
def self.bonsai_site_configuration(since=nil)
data = []
Site.all.reduce(data) do |data, site|
begin
data << JSON.parse(site.bonsai_desc)
rescue StandardError => exception
notify_exception(exception)
end
data
end
return {data: data`}
end
All I'm looking over here is that if test cases wherein if the site.bonsai_desc fails(i.e throw an exception). It should invoke notify_exception with given exception
Here how my minitest looks like
test "should return bonsai_site_configuration even on exception" do
site = Site.first
raises_exception = -> { raise 'boom' }
Site.stub(:all, [site]) do
site.stub(:bonsai_desc, raises_exception) do
Site.stub(:notify_exception) do
bonsai_information = Site.bonsai_site_configuration
assert_equal(0, bonsai_information[:data].count)
end
end
end
end
Upon running the minitest the test passes but I want to ensure that argument check is also included.
What am I suppose to do?

Ruby XMPP4R bot and Threads - trouble

I want my bot sends and receives messages in parallel threads. I also want my bot sends message back to user when receives any message from user. But now he sends it back to user every 5 seconds. I understand that it's because i used "loop do" but without infinity loop i cant use callbacks.
So how to send and receive messages in parallel threads? And how to overcome my "loop problem" when receiving messages?
require 'xmpp4r'
class Bot
include Jabber
def initialize jid,jpassword
#jid = jid
#jpassword = jpassword
#client = Client.new(JID::new(#jid))
#client.connect
#client.auth(#jpassword)
#client.send(Presence.new.set_type(:available))
end
def wait4msg
loop do
#client.add_message_callback do |msg|
send_message(msg.from,msg.body)
sleep 5
end
end
end
def send_message to,message
msg = Message::new(to,message)
msg.type = :chat
#client.send(msg)
end
def add_user jid
adding = Presence.new.set_type(:subscribe).set_to(jid)
#client.send(adding)
end
end
bot = Bot.new('from#example.xmpp','123456')
t1 = Thread.new do
bot.wait4msg
end
t2 = Thread.new do
bot.send_message('to#example.xmpp',Random.new.rand(100).to_s)
end
Thread.list.each { |t| t.join if t != Thread.main }
Good day. You can use callbacks without loop, see an examples. For example: in initialize add
#client.add_message_callback do |m|
if m.type != :error
m2 = Message.new(m.from, "You sent: #{m.body}")
m2.type = m.type
#client.send(m2)
end
end

Resources