Skip to content

Instantly share code, notes, and snippets.

@mkroman
Created July 26, 2010 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkroman/491099 to your computer and use it in GitHub Desktop.
Save mkroman/491099 to your computer and use it in GitHub Desktop.
# mplextended (the equivalent of screen_away.pl for irssi) for weechat.
# Copyright (C) 2010, Mikkel Kroman
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Requires weechat-ruby (http://github.com/dominikh/weechat-ruby).
#
# 2010-09-03, mk <mk@maero.dk>
# version 1.0.3: minor changes, added timestamp format
# 2010-07-27, mk <mk@maero.dk>
# version 1.0.2: changed global variables to instance variables and added
# buffer clearing as soon as it has been printed
# 2010-07-23, mk <mk@maero.dk>
# version 1.0.1: refactored a tiny bit of the code
# 2010-07-22, mk <mk@maero.dk>
# version 1.0: screen and tmux (untested) support
require 'weechat'
include Weechat
@script = {
name: "mplext",
author: "Mikkel Kroman <mk@maero.dk>",
license: "GPL3",
version: "1.0.3",
description: "Set away status and log messages when detaching or reattaching from a terminal multiplexer."
}
@config = Script::Config.new({
"color" => [Color, "lightgreen"],
"format" => [String, "%H:%M:%S"],
"message" => [String, "not here.."],
"interval" => [Integer, 5]
})
@sock, @away, @buffer = nil, false, []
def setup
if ENV.include? 'STY'
if %x{env LC_ALL=C screen -ls} =~ %r{Sockets? in (/.+)\.}
@socket = File.join $1, ENV['STY']
end
elsif ENV.include? 'TMUX'
@socket = ENV['TMUX'].split(?,).first
end
Weechat::Hooks::Print.new &method(:check_highlight)
Weechat::Timer.new @config.interval.to_i * 1000, &method(:check_presence)
end
def check_presence remaining
attached = File.executable? @socket
if attached and @away
Weechat.puts "Terminal multiplexer attached. Clearing away status."
Weechat.exec "/away -all"
print_buffer
@away = false
elsif not attached and not @away
Weechat.puts "Terminal multiplexer detached. Setting away status."
Weechat.exec "/away -all #{@config.message}"
@away = true
end
end
def check_highlight buffer, date, tags, display, highlighted, prefix, message
if @away and highlighted
time, channel, nick = Time.now.to_i, buffer.short_name, prefix.chop
@buffer << [time, channel, nick, message]
end
end
def print_buffer
@buffer.each do |time, channel, nickname, message|
stamp = Time.at(time).strftime @config.format
format = @config.color, stamp, channel, nickname, message
Weechat.puts "%s %s [%s] %s: %s" % format
end.clear
end
@dominikh
Copy link

$sock, $away and $buffer could be instance variables instead, it would all behave the same (but would not pollute the global namespace)
(scripts are executed in the context of modules)

@mkroman
Copy link
Author

mkroman commented Jul 27, 2010

Ah, yes, of course! I completely forgot that. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment