Project

General

Profile

unexist.dev

subtle

Assorted tidbits and projects

migrate.rb

, 04/19/2011 04:41 PM

 
1
# coding: utf-8
2
#
3
# @package subtle
4
#
5
# @file Migrate config
6
# @copyright Copyright (c) 2011 Christoph Kappel <unexist@dorfelite.net>
7
# @version $Id$
8
#
9
# This program can be distributed under the terms of the GNU GPL.
10
# See the file COPYING.
11
#
12

    
13
require "pp"
14

    
15
def format_value(value)
16
  case value
17
    when String then "\"#{value}\""
18
    when Array  then value.join(", ")
19
    when Fixnum then value
20
    else value.to_s
21
  end
22
end
23

    
24
begin
25
  config  = nil
26
  styles  = {}
27
  skip    = []
28
  nline   = 0
29
  colline = nil
30
  ignore  = false
31
  lines   = nil
32

    
33
  # Init
34
  [ :title, :focus, :urgent, :occupied, :views,
35
    :sublets, :separator, :clients, :subtle
36
  ].each { |s| styles[s] = {} }
37

    
38
  # Check arguments
39
  if(0 == ARGV.size)
40
    # Get XDG path
41
    xdg_config_home = File.join((ENV["XDG_CONFIG_HOME"] ||
42
      File.join(ENV["HOME"], ".config")), "subtle")
43
    xdg_config_dir = File.join((ENV["XDG_CONFIG_DIRS"].split(":").first rescue
44
      File.join("/etc", "xdg")), "subtle")
45

    
46
    # Find config
47
    unless(File.exist?(config = File.join(xdg_config_home, "subtle.rb")))
48
      puts ">>> ERROR: Couldn't find config file"
49
      exit
50
    end
51
  elsif([ "--help", "-h", "help" ].include?(ARGV[0]))
52
    puts "Usage: %s [CONFIG]" % [ $0 ]
53
    puts "\nConvert color definitions to styles"
54
    puts "\nPlease report bugs at http://subforge.org/projects/subtle/issues"
55
    exit
56
  else
57
    config = ARGV[0]
58
  end
59

    
60
  puts ">>> Reading config `#{config}'"
61

    
62
  # Load config
63
  lines = IO.readlines(config)
64

    
65
  # Parse config
66
  lines.each do |line|
67
    case line
68
      # Block comments
69
      when /^=begin/ then ignore = true
70
      when /^=end/   then ignore = false
71

    
72
      # Options
73
      when /^\s*set\s*:border,\s*(\d+)/
74
        next if(ignore)
75
        value = $~[1].to_i
76
        styles[:clients][:active]   = value
77
        styles[:clients][:inactive] = value
78

    
79
        skip << nline
80
      when /^\s*set\s*:outline,\s*(\d+)/
81
        next if(ignore)
82
        value = $~[1].to_i
83
        styles[:title][:border]     = value
84
        styles[:focus][:border]     = value
85
        styles[:urgent][:border]    = value
86
        styles[:occupied][:border]  = value
87
        styles[:views][:border]     = value
88
        styles[:sublets][:border]   = value
89
        styles[:separator][:border] = value
90

    
91
        skip << nline
92
      when /^\s*set\s*:gap,\s*(\d+)/
93
        next if(ignore)
94
        styles[:clients][:margin] = $~[1].to_i
95

    
96
        skip << nline
97
      when /^\s*set\s*:padding,\s*\[((?:\s*(?:\d+)\s*,?)*)\]/
98
        next if(ignore)
99
        value = $~[1].split(",").map(&:to_i)
100

    
101
        styles[:title][:padding]     = value
102
        styles[:focus][:padding]     = value
103
        styles[:urgent][:padding]    = value
104
        styles[:occupied][:padding]  = value
105
        styles[:views][:padding]     = value
106
        styles[:sublets][:padding]   = value
107
        styles[:separator][:padding] = value
108

    
109
        skip << nline
110
      when /^\s*set\s*:strut,\s*\[((?:\s*(?:\d+)\s*,?)*)\]/
111
        next if(ignore)
112
        styles[:subtle][:padding] = $~[1].split(",").map(&:to_i)
113

    
114
        skip << nline
115

    
116
      # Colors
117
      when /^\s*color\s*:(\w+),\s*"(#[0-9a-fA-F]{6})/
118
        next if(ignore)
119
        name  = $~[1]
120
        sym   = name.split("_").first.to_sym
121
        color = $~[2]
122

    
123
        colline     ||= nline
124
        styles[sym] ||= {} if(styles.keys.include?(sym))
125

    
126
        case name
127
          when /^\w+_fg/     then styles[sym][:foreground]        = color
128
          when /^\w+_bg/     then styles[sym][:background]        = color
129
          when /^background/ then styles[:subtle][:background]    = color
130
          when /^stipple/    then styles[:subtle][:stipple]       = color
131
          when /^separator/  then styles[:separator][:foreground] = color
132
          when /^client_((?:in)?active)/
133
            sym = $~[1].to_sym
134

    
135
            # Set or append
136
            if(styles[:clients][sym].nil?)
137
              styles[:clients][sym] = color
138
            else
139
              styles[:clients][sym] = [
140
                format_value(color), styles[:clients][sym]
141
              ]
142
            end
143
          when /^panel/
144
            styles[:subtle][:panel]         = color
145
            styles[:separator][:background] = color
146
          when /^\w+_border/
147
            # Set or append
148
            if(styles[sym][:border].nil?)
149
              styles[sym][:border] = color
150
            else
151
              styles[sym][:border] = [
152
                format_value(color), styles[sym][:border]
153
              ]
154
            end
155
        end
156

    
157
        skip << nline
158
    end
159

    
160
    nline += 1
161
  end
162

    
163
  # Output
164
  filename = File.join(ENV["HOME"], "subtle.rb")
165
  File.open(filename, "w") do |f|
166
    nline = 0
167

    
168
    # Puts old config into new file
169
    lines.each do |line|
170
      # Replace colors by styles
171
      if(nline == colline)
172
        styles.each do |k, v|
173
          f.puts "style :#{k} do"
174

    
175
          # Arguments
176
          v.each do |k2, v2|
177
            value = format_value(v2)
178

    
179
            if(value.is_a?(String))
180
              f.puts "  %-10s  %s" % [ k2, value ]
181
            else
182
              f.puts "  %-10s  %d" % [ k2, value ]
183
            end
184
          end
185

    
186
          f.puts "end"
187
          f.puts
188
        end
189
      end
190

    
191
      # Puts line of not in skip list
192
      f.puts line unless(skip.include?(nline))
193

    
194
      nline += 1
195
    end
196
  end
197

    
198
  puts ">>> Created config `#{filename}'"
199

    
200
rescue => err
201
  puts ">>> ERROR: #{err}"
202
  puts err.backtrace
203
end