Skip to content

Instantly share code, notes, and snippets.

@eoinkelly
Created July 13, 2022 20:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eoinkelly/5580ffea49c9f53454819b1fa66589c2 to your computer and use it in GitHub Desktop.
Save eoinkelly/5580ffea49c9f53454819b1fa66589c2 to your computer and use it in GitHub Desktop.
Choose a random VSCode theme for your current project
#!/usr/bin/env ruby
# Purpose:
#
# * Edits .vscode/settings.json file to set a new theme for the project.
# * If no arg passed then a theme is chosen randomly.
# * You must already have the themes installed
# * You should edit the list of themes to match your favourites.
#
# Installation:
#
# 1.Download this file and save as 'retheme' in some directory on your PATH
# 2. Run: chmod u+x path/to/downloaded/file
#
# Usage:
# $ cd path/to/root/of/a/vscode/project
# $ retheme # choose a random theme
# $ retheme light # choose the 'light' theme
require 'json'
require 'fileutils'
THEMES = {
# shorthand-name => Actual theme name
'default' => 'GitHub Light Default',
'light' => 'GitHub Light Default',
'dark' => 'GitHub Dark Default',
'solarlight' => 'Solarized Light',
'solardark' => 'Solarized Dark',
'dracula' => 'Dracula',
'dark1' => 'Default Dark+',
'monokai' => 'Monokai',
'nightowl' => 'Night Owl',
'nord' => 'Nord',
'atom1light' => 'Atom One Light',
'tomnight' => 'Tomorrow Night',
'tomnightbright' => 'Tomorrow Night Bright'
}.freeze
puts "Available themes: (pass short name as arg to choose it)"
THEMES.each do |short_name, full_name|
puts " #{short_name}: #{full_name}"
end
puts ""
def main
new_theme_name = ARGV.shift || THEMES.keys.sample
new_theme = THEMES.fetch(new_theme_name)
cwd = Dir.pwd
vscode_settings_dir_path = File.absolute_path(File.join(cwd, '.vscode'))
vscode_settings_file = File.absolute_path(File.join(cwd, '.vscode/settings.json'))
FileUtils.mkdir(vscode_settings_dir_path) unless Dir.exist?(vscode_settings_dir_path)
settings = if File.exist?(vscode_settings_file)
JSON.parse(File.read(vscode_settings_file))
else
{}
end
puts "Changing VS Code theme to: #{new_theme}"
settings['workbench.colorTheme'] = new_theme
File.write(vscode_settings_file, JSON.pretty_generate(settings))
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment