blob: 4a1e6c83e46c0a780533e11dbdc86c306cc9c64f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
#!/usr/bin/ruby
#
# @file Vitag
#
# @copyright (c) 2010-2011, Christoph Kappel <unexist@dorfelite.net>
# @version $Id$
#
# This program can be distributed under the terms of the GNU GPLv2.
# See the file COPYING for details.
#
# Vitag is a helper to edit window/view tagging with any $EDITOR
#
# http://subforge.org/projects/subtle-contrib/wiki/Vitag
#
require 'tempfile'
require 'digest/md5'
begin
require 'subtle/subtlext'
rescue LoadError
puts ">>> ERROR: Couldn't find subtlext"
exit
end
# Check if $EDITOR is set
if ENV['EDITOR'].nil?
puts <<-EOF
>>> ERROR: Couldn't find $EDITOR envorinment variable
>>> Please set it like this: export EDITOR=vim
EOF
exit
end
# Check whether subtle is running
unless(Subtlext::Subtle.running?)
puts ">>> ERROR: Couldn't find running subtle"
exit
end
# Check for subtlext version
major, minor, teeny = Subtlext::VERSION.split('.').map(&:to_i)
if major == 0 and minor == 10 and 3104 > teeny
puts ">>> ERROR: vitag needs at least subtle `0.10.3104' (found: %s)" % [
Subtlext::VERSION
]
exit
end
# Collect views and clients
views = Subtlext::View.all
clients = Subtlext::Client.all
# Create temp file
temp = Tempfile.new('vitag-')
# Fill in tags
temp.puts('# Views')
views.each do |v|
temp.puts('@%s %s' % [
v.name,
v.tags.map { |t| '#%s' % [ t ] }.join(' ')
])
end
# Fill in tags
temp.puts('')
temp.puts('# Clients')
clients.each do |c|
# Remove hashes from string
name = c.to_str.split('#').first
temp.puts('%s (%s) %s' % [
name, c.instance, c.tags.map { |t| '#%s' % [ t ] }.join(' ')
])
end
temp.flush
# Store checksum for check
md5 = Digest::MD5.file(temp.path)
# Start editor
system('$EDITOR %s' % [ temp.path ])
temp.rewind
# Check for changes
if md5 != Digest::MD5.file(temp.path)
# Read temp file
temp.readlines.each do |line|
# Handle lines
case line[0]
when '@' then cur = views.shift
when '#', ' ', "\n" then next
else cur = clients.shift
end
# Select tags and sanitize
tags = line.split('#')[1..-1].map(&:rstrip)
# Check for valid object
if cur and tags
# Find or create tags
tags.map! do |name|
tag = Subtlext::Tag.first(name) || Subtlext::Tag.new(name)
tag.save
tag
end
# Finally assign tags
cur.tags = tags
cur = nil
end
end
end
temp.close
# vim:ts=2:bs=2:sw=2:et:fdm=marker
|