3

I wrote this tiny script to swap out colors on the Numix theme for Ubuntu Gnome:

<?php
$oldColor = $argv[1];
$newColor = $argv[2];
// defaults
// $oldColor = 'd64937';
// $newColor = 'f66153';

$path = '/usr/share/themes/Numix/gtk-3.0/gtk-dark.css';
$fileRead =  fopen($path, 'r');
$contents = fread($fileRead, filesize($path));
$newContents = str_replace($oldColor, $newColor, $contents);
$fileWrite =  fopen($path, 'w');
fwrite($fileWrite, $newContents);
fclose($fileWrite);
?>

The script works as intended so long as I pass the two arguments.

  1. How do I set defaults for the arguments?
  2. Should I refactor maybe making use of file_put_contents()?
RichardForrester
  • 1,048
  • 11
  • 19
  • You can check with `empty()` if the value is set and if not set a default one, also `file_get_contents()` and `file_put_contents()` aren't a bad way, so try it! Try to solve it yourself, if you're stuck say it. – Rizier123 Jan 03 '15 at 09:13
  • Please explain your second question. Why is this an option? – honk Jan 03 '15 at 09:16
  • @pandiatonicism Nice! If you want, you can write your answer here and share what you came up with! – Rizier123 Jan 03 '15 at 09:47
  • @Rizier123 I was trying to paste it in, but it looks weird in the comments and I didn't know if I should answer my own question... seems a little funny. – RichardForrester Jan 03 '15 at 09:55
  • @honk Now that I've done a bit of refactoring it seem obvious that file_get_contents() and file_put_contents() are the way to go. As stated in the PHP documentation for file_put_contents(): "This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file." – RichardForrester Jan 03 '15 at 10:10
  • @DelicateMonster: I'm glad that you could solve your problem. I asked you that in my comment, because your second question looks to be perfect to be [asked to your rubber duck](http://blog.codinghorror.com/rubber-duck-problem-solving/) ;) – honk Jan 03 '15 at 10:29

3 Answers3

3
<?php 
// How do I set defaults for the arguments?
$oldColor = !empty($argv[1]) ? $argv[1] : 'd64937';
$newColor = !empty($argv[2]) ? $argv[2] : 'f66153';
$file = '/usr/share/themes/Numix/gtk-3.0/gtk-dark.css';

// Your choice whether its cleaner, I think so.
file_put_contents(
    $file, 
    str_replace(
        $oldColor, 
        $newColor, 
        file_get_contents($file)
    )
);
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
1

I'm going to study Loz Cherone's answer which is a little advanced for me (this is my first script), but I did come up with something better:

<?php
if (empty($argv[1])) {
    $oldColor = 'd64937';
    $newColor = 'f66153';
} elseif (empty($argv[2])) {
    echo "Please supply new color";
    return false;
} else {
    $oldColor = $argv[1];
    $newColor = $argv[2];
}
$path = '/usr/share/themes/Numix/gtk-3.0/gtk-dark.css';
$oldContents = file_get_contents($path);
$newContents = str_replace($oldColor, $newColor, $oldContents);
file_put_contents($path, $newContents);
?>
RichardForrester
  • 1,048
  • 11
  • 19
0

It seems only fair to share the final product for anyone out there running the Numix theme on Ubuntu. Just copy script into a .php file and run as sudo. Make a back up of the two files first.

<?php 

if (!empty($argv[1]) && empty($argv[2])) {
    echo "Please supply two colors for your very own custom color swap or zero colors for a slight improvement";
    return false;
}

$oldColor = !empty($argv[1]) ? $argv[1] : 'd64937';
$newColor = !empty($argv[2]) ? $argv[2] : 'f66153';
$file_1 = '/usr/share/themes/Numix/gtk-3.0/gtk-dark.css';
$file_2 = '/usr/share/themes/Numix/gtk-2.0/gtkrc';

file_put_contents(
    $file_1, 
    str_replace(
        $oldColor, 
        $newColor, 
        file_get_contents($file_1)
    )
);

file_put_contents(
    $file_2, 
    str_replace(
        $oldColor, 
        $newColor, 
        file_get_contents($file_2)
    )
);
?>
RichardForrester
  • 1,048
  • 11
  • 19