forked from dbb/scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxdeftheme.pl
executable file
·104 lines (90 loc) · 2.13 KB
/
xdeftheme.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use File::Copy;
# dbbolton
# danielbarrettbolton@gmail.com
# terminal to use
my $term = "xterm";
# Path to your Xdefaults themes
my $path = "$ENV{HOME}/Documents/xdefaults/";
# .Xdefaults file location
my $config = "$ENV{HOME}/.Xdefaults";
my $backup = "$path/xdefaults_backup";;
# the terminal's process ID, if running
my $termpid = `pidof $term`;
# Find available files
opendir(my $dh, $path) || die "can't opendir $path: $!";
my @files = sort(readdir($dh));
my $i = 0;
print "Files found: \n";
foreach (@files) {
unless (/^\.$/ || /^\.\.$/) {
print "$i\t$_\n";
$i += 1;
}
}
# Ask which theme file to use
my $newtheme = '';
sub getnew {
print "\nEnter theme number: ";
my $n = <>;
chomp($n);
$n += 2;
return $n;
}
# Make sure the name is ok
my $usenew = "n";
sub checkfile {
if (-e "$path/$newtheme" && -r "$path/$newtheme" ) {
print "File is OK.\n";
}
else {
print "\nEither the specified file does not exist in the theme directory, or you do
not have access to it.\n";
$newtheme = getnew();
checkfile();
}
}
sub verify {
print "Apply theme \"$newtheme\" ? (y/n) ";
$usenew = <>;
chomp($usenew);
if ($usenew eq "y" || $usenew eq "Y") {
writefile();
}
elsif ($usenew eq "n" || $usenew eq "N") {
print "Aborting.\n";
}
else {
print "Invalid response. Type 'y' or 'n'.\n";
verify();
}
}
sub starttint {
if ($termpid) {
system "kill $termpid";
}
defined(my $pid = fork) || die "Cannot fork: $!";
unless ($pid) {
close(STDIN) or die "Can't close STDIN: $!\n";
close(STDOUT) or die "Can't close STDOUT: $!\n";
close(STDERR) or die "Can't close STDERR: $!\n";
exec "xrdb -load $config && $term";
exit 0;
}
}
sub writefile {
if (-e $config) {
print "Backing up ~/.Xdefaults to \n $backup ...\n";
move($config, $backup) || die "Error moving: $!";
}
print "Writing $newtheme to ~/.Xdefaults ...\n";
copy("$path/$newtheme", $config) || die "File cannot be copied: $!";
print "(Re)starting $term ... \n";
starttint();
}
my $index = getnew();
$newtheme = $files[$index];
checkfile();
verify();