WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Clear chat slash command. (https://www.wowinterface.com/forums/showthread.php?t=52673)

Lesteryoung 08-31-15 01:16 AM

Clear chat slash command.
 
I'm trying to create a command function that simply clears the chat that you currently have selected, whether it be the combat log or the main chat or any other chat frame.

This is what I have, but the code is off.

Code:

SlashCmdList["CLEAR"] = function() SELECTED_CHAT_FRAME() end
        SLASH_CLEAR1    = "/clear"


Kkthnx 08-31-15 02:10 AM

Quote:

Originally Posted by Lesteryoung (Post 310641)
I'm trying to create a command function that simply clears the chat that you currently have selected, whether it be the combat log or the main chat or any other chat frame.

This is what I have, but the code is off.

Code:

SlashCmdList["CLEAR"] = function() SELECTED_CHAT_FRAME() end
        SLASH_CLEAR1    = "/clear"


Try this.
Code:

SlashCmdList.CLEAR_CHAT = function()
        for i = 1, NUM_CHAT_WINDOWS do
                _G[format("ChatFrame%d", i)]:Clear()
        end
end
SLASH_CLEAR_CHAT1 = "/clear"

If you want to see some other ones look here.
https://github.com/Kkthnx/KkthnxUI/b...e/Commands.lua

Petrah 09-01-15 02:23 AM

This is what I'm currently using. Someone shared it on a forum, I think it was the official forums, but I cannot recall who. I've been using it for quite a long time added in one of the files for ElvUI.

Code:

SlashCmdList["CLEAR"] = function()
    local s, i
    s = ""
    for i=1, 255, 1 do
        s = s..'\r'
    end
    SELECTED_CHAT_FRAME:AddMessage(s)
end


ObbleYeah 09-01-15 04:48 AM

seems kinda gross to add 255 blank messages rather than simply clearing them

MunkDev 09-01-15 05:54 AM

Quote:

Originally Posted by Petrah (Post 310652)
This is what I'm currently using. Someone shared it on a forum, I think it was the official forums, but I cannot recall who. I've been using it for quite a long time added in one of the files for ElvUI.

Code:

SlashCmdList["CLEAR"] = function()
    local s, i
    s = ""
    for i=1, 255, 1 do
        s = s..'\r'
    end
    SELECTED_CHAT_FRAME:AddMessage(s)
end


How about just doing this instead?
Lua Code:
  1. SlashCmdList["CLEAR"] = function()
  2.     SELECTED_CHAT_FRAME:Clear()
  3. end

Dridzt 09-01-15 07:00 AM

http://wow.curseforge.com/addons/cle...earing-method/ (couple years back since I submitted this but I expect it still works)

SDPhantom 09-01-15 02:47 PM

Quote:

Originally Posted by Dridzt (Post 310658)
Code:

SlashCmdList["CLEAR"] = SELECTED_CHAT_FRAME.Clear
SLASH_CLEAR1 = "/clear"
SLASH_CLEAR2 = "/chatclear"

http://wow.curseforge.com/addons/cle...earing-method/
(couple years back since I submitted this but I expect it still works)

That ends up passing the command arguments string as self, which should raise an error.

Dridzt 09-01-15 03:35 PM

I'm no longer playing, but have you tested it?
I'd been using it for quite a while with no issues, I don't think chatcode has changed significantly.

SDPhantom 09-01-15 04:10 PM

As expected:
Quote:

Message: Interface\FrameXML\ChatFrame.lua:4454: Attempt to find 'this' in non-table object (used '.' instead of ':' ?)
Time: 09/01/15 15:13:23
Count: 1
Stack: [C]: ?
[C]: in function `?'
Interface\FrameXML\ChatFrame.lua:4454: in function `ChatEdit_ParseText'
Interface\FrameXML\ChatFrame.lua:4108: in function `ChatEdit_SendText'
Interface\FrameXML\ChatFrame.lua:4147: in function `ChatEdit_OnEnterPressed'
[string "*:OnEnterPressed"]:1: in function <[string "*:OnEnterPressed"]:1>

Locals:
Honestly, it's a wonder how this code ever worked. This isn't something they change with an API update.

MunkDev 09-02-15 03:34 AM

Quote:

Originally Posted by Dridzt (Post 310672)
I'm no longer playing, but have you tested it?
I'd been using it for quite a while with no issues, I don't think chatcode has changed significantly.

It's not really a matter of chatcode. It's a matter of how the function works. Calling a function as owner:function() will pass owner as the hidden argument self, meaning any code within that function that refers to self will change the calling widget.

As an example, when you create a frame or button, it will inherit a lot of global functions which can be used to modify the widget, but these functions expect the widget to pass itself as the first argument. This is what the colon operator does in lua.

What you attempt to do in that snippet is call the function Clear() that is attached to SELECTED_CHAT_FRAME, without giving the function the actual frame that you intend to clear.

Example of difference between dot and colon operator:
Lua Code:
  1. function SetID(self, value)
  2.     self.ID = value
  3. end
  4.  
  5. owner.SetID = SetID -- attach function to widget
  6.  
  7. owner:SetID(1) -- passing itself as the hidden argument self
  8.  
  9. owner.SetID(self, 1) -- calling the function without hidden argument
  10.  
  11. owner.SetID(1) -- error, function will try to add nil value to an integer
  12.  
  13. owner.SetID(someOtherFrame, 1) -- uses the function SetID on owner to change someOtherFrame

Dridzt 09-02-15 10:47 AM

I have no reason to doubt my version is broken now, but it was absolutely working in 50200 (and for a while before that).

Lesteryoung 09-02-15 05:55 PM

Been using MunkDev's version and it works fine. Is this the preferable way to clear chat? Obviously creating 255 blank lines seem egregious.

Only complaint is that it doesn't clear the combat log when it's selected. There's a code floating around on arenajunkies but it doesn't work either. They must have changed chat API within the last few years.

Kkthnx 09-02-15 07:05 PM

Quote:

Originally Posted by Lesteryoung (Post 310707)
Been using MunkDev's version and it works fine. Is this the preferable way to clear chat? Obviously creating 255 blank lines seem egregious.

Only complaint is that it doesn't clear the combat log when it's selected. There's a code floating around on arenajunkies but it doesn't work either. They must have changed chat API within the last few years.

Have you even tried the one I posted? It does everything you've requested even the combat log?

Here I'll post it again and quote myself.

Quote:

Originally Posted by Kkthnx (Post 310643)
Try this.
Code:

SlashCmdList.CLEAR_CHAT = function()
        for i = 1, NUM_CHAT_WINDOWS do
                _G[format("ChatFrame%d", i)]:Clear()
        end
end
SLASH_CLEAR_CHAT1 = "/clear"

If you want to see some other ones look here.
https://github.com/Kkthnx/KkthnxUI/b...e/Commands.lua


Seerah 09-02-15 07:18 PM

Quote:

Originally Posted by Lesteryoung (Post 310707)
Been using MunkDev's version and it works fine. Is this the preferable way to clear chat? Obviously creating 255 blank lines seem egregious.

Only complaint is that it doesn't clear the combat log when it's selected. There's a code floating around on arenajunkies but it doesn't work either. They must have changed chat API within the last few years.

That's not MunkDev's code, that was Petrah's that he had quoted. And, no, creating 255 blank lines is not the best way. And the reason why it doesn't work in the combat log is because you can't add messages to it (to my knowledge, anyway).

Take another look at MunkDev's actual posted solution.


Quote:

Originally Posted by Kkthnx (Post 310709)
Have you even tried the one I posted? It does everything you've requested even the combat log?

Here I'll post it again and quote myself.

The OP asked for a script to clear the currently selected chat window, not all at once.

Kkthnx 09-02-15 07:32 PM

Quote:

Originally Posted by Seerah (Post 310710)
That's not MunkDev's code, that was Petrah's that he had quoted. And, no, creating 255 blank lines is not the best way. And the reason why it doesn't work in the combat log is because you can't add messages to it (to my knowledge, anyway).

Take another look at MunkDev's actual posted solution.



The OP asked for a script to clear the currently selected chat window, not all at once.

Sigh, again he needs to use the one I posted. :D Who mentioned all at once? :eek:

Lesteryoung 09-02-15 07:33 PM

Seerah, I did use MunkDev's code, and it work's fine on chat but doesn't clear the combat log.
The one kkthnx posted clears all chats, not the one you have selected.
And the one Petrah posted creates 255 blank lines which everyone has said is stupid.

Kkthnx 09-02-15 07:35 PM

Quote:

Originally Posted by Lesteryoung (Post 310714)
Seerah, I did use MunkDev's code, and it work's fine on chat but doesn't clear the combat log.
The one kkthnx posted clears all chats, not the one you have selected.
And the one Petrah posted creates 255 blank lines which everyone has said is stupid.

The one I posted will not clear the combat log unless you have that focused.

Lesteryoung 09-02-15 07:37 PM

It clears the combat for about a second, and then the data just repopulates instantly.

And if you have the combat log targeted and /clear, it clears all other windows as well.

So far MunkDev's code has worked the best, in that it only clears selected windows, but among them can't be the combat log for some reason.

Kkthnx 09-02-15 07:45 PM

Quote:

Originally Posted by Lesteryoung (Post 310717)
It clears the combat for about a second, and then the data just repopulates instantly.

And if you have the combat log targeted and /clear, it clears all other windows as well.

So far MunkDev's code has worked the best, in that it only clears selected windows, but among them can't be the combat log for some reason.

Here you go

Code:

SLASH_CLEARCHAT1 = "/clear"
SLASH_CLEARCHAT2 = "/clearchat"

SlashCmdList.CLEARCHAT = function(cmd)
        cmd = cmd and strtrim(strlower(cmd))
        for i = 1, NUM_CHAT_WINDOWS do
                local f = _G["ChatFrame"..i]
                if f:IsVisible() or cmd == "all" then
                        f:Clear()
                end
        end
end

I tested this window by window and this is what you are looking for 100%

Credits to Phanx / PhanxChat
https://github.com/Phanx/PhanxChat/b.../Core.lua#L273

Lesteryoung 09-02-15 07:55 PM

Thanks kkThnx, the one you posted works the same as Munk's code basically. It clears selected windows without clearing all windows.

For some reason, it clears the combat log fine, but if you tab between the combat log views (What happened to me?) thing it repopulates the data you previously cleared. If this is unavoidable then it's fine, mainly just wanted to clear chat anyway.


All times are GMT -6. The time now is 10:30 PM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI