Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardFevrier committed Jun 6, 2024
0 parents commit d4ce316
Show file tree
Hide file tree
Showing 13 changed files with 334 additions and 0 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Release

on:
push:
tags: '*'
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
arch: [amd64, arm64]
os: [darwin, freebsd, linux, windows]
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21.4'

- name: Install dependencies
run: |
go mod download
- name: Build
run: |
GOARCH=${{ matrix.arch }} GOOS=${{ matrix.os }} go build -o vsc-theme-converter-${{ matrix.os }}-${{ matrix.arch }} ./main.go
# Tried gzip but without succeding of keeping execute permissions
- name: Zip
run: |
zip vsc-theme-converter-${{ matrix.os }}-${{ matrix.arch }}.zip vsc-theme-converter-${{ matrix.os }}-${{ matrix.arch }}
- uses: actions/upload-artifact@v4
with:
name: vsc-theme-converter-${{ matrix.os }}-${{ matrix.arch }}.zip
path: |
vsc-theme-converter-${{ matrix.os }}-${{ matrix.arch }}.zip
retention-days: 1

publish:
needs:
- build
runs-on: ubuntu-latest
env:
GH_REPO: ${{ github.repository }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
permissions:
contents: write
steps:
# Must perform checkout first, since it deletes the target directory
# before running, and would therefore delete the downloaded artifacts
- uses: actions/checkout@v4

- uses: actions/download-artifact@v4

- name: Publish release
run: |
TAG_NAME=${{ github.ref_name }}
gh release create $TAG_NAME --title "$TAG_NAME" --target $GITHUB_SHA \
vsc-theme-converter-*/*
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/*
4 changes: 4 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters:
disable-all: true
enable:
- goimports
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
repos:
- repo: https://github.com/golangci/golangci-lint.git
rev: v1.59.0
hooks:
- id: golangci-lint-full
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Richard Février

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Usage

```
$ vsc-theme-converter /path/to/vsc-theme.jsonc [output]
```
Optional `output`: default `{all}`
- micro
- wezterm
- all

# How to get the *.jsonc theme file from VSCode

- Set the theme you want to convert as your actual theme.
- Use the `Command Palette` to export it with `Developer: Generate Color Theme From Current Settings` and save it somewhere.

# Contributing

To get VSCode colors related to tokens:

- Use the `Command Palette` with `Developer: Inspect Editor Tokens and Scopes`.
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/RichardFevrier/vsc-theme-converter

go 1.21.4

require (
github.com/goccy/go-json v0.10.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/marcozac/go-jsonc v0.1.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a // indirect
)
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/marcozac/go-jsonc v0.1.1 h1:dnZgAYinXsnI73ZemlbQYPOo1uZYD/LSYI7Aw9IbIeM=
github.com/marcozac/go-jsonc v0.1.1/go.mod h1:BFDFoML/0Y4/XnOpOdomjrDBn1nIG96p7dlVXBDaybI=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a h1:SJy1Pu0eH1C29XwJucQo73FrleVK6t4kYz4NVhp34Yw=
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a/go.mod h1:DFSS3NAGHthKo1gTlmEcSBiZrRJXi28rLNd/1udP1c8=
55 changes: 55 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"encoding/json"
"fmt"
"log"
"os"

"github.com/RichardFevrier/vsc-theme-converter/micro"
"github.com/RichardFevrier/vsc-theme-converter/vscode"
"github.com/RichardFevrier/vsc-theme-converter/wezterm"

"github.com/tailscale/hujson"
)

func main() {

if len(os.Args) < 2 {
fmt.Println("Usage: vsc-theme-converter <path_to_vscode_theme_jsonc> [output]")
fmt.Println("[output] can be either 'all' or 'micro' or 'wezterm'. Default {all}")
return
}

path := os.Args[1]
output := "all"
if len(os.Args) > 2 {
output = os.Args[2]
}

invalidData, err := os.ReadFile(path)
if err != nil {
return
}

data, err := hujson.Standardize(invalidData)
if err != nil {
return
}

var theme vscode.VSCodeTheme

if err := json.Unmarshal(data, &theme); err != nil {
log.Fatal(err)
}

switch output {
case "micro":
micro.Generate(theme)
case "wezterm":
wezterm.Generate(theme)
default:
micro.Generate(theme)
wezterm.Generate(theme)
}
}
64 changes: 64 additions & 0 deletions micro/micro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package micro

import (
"fmt"

"github.com/RichardFevrier/vsc-theme-converter/utils"
"github.com/RichardFevrier/vsc-theme-converter/vscode"
)

func Generate(theme vscode.VSCodeTheme) {

fmt.Println("\nMicro:")

s := "\n"
s += "color-link default \"" + theme.Colors.EditorForeground + "," + theme.Colors.EditorBackground + "\""
append(&s, theme, "comment", "comment")
s += "\n"
append(&s, theme, "constant.language", "constant.bool")
append(&s, theme, "constant.numeric", "constant.number")
append(&s, theme, "constant.character", "constant.specialChar")
append(&s, theme, "string", "constant.string")
s += "\n\n"
s += "color-link symbol.brackets \"" + "#179FFF" + "\""
append(&s, theme, "keyword.operator", "symbol.operator")
s += "\n"
append(&s, theme, "storage.type", "type")
s += "\n"
append(&s, theme, "keyword.control", "special")
append(&s, theme, "keyword.control", "statement")
append(&s, theme, "keyword.other.namespace", "preproc")
s += "\n\n"
s += "color-link underlined \"" + theme.Colors.EditorSelectionBackground + "\"\n"
s += "\n"
s += "color-link diff-added \"" + theme.Colors.EditorGutterAddedBackground + "\"\n"
s += "color-link diff-modified \"" + theme.Colors.EditorGutterModifiedBackground + "\"\n"
s += "color-link diff-deleted \"" + theme.Colors.EditorGutterDeletedBackground + "\"\n"
s += "\n"
s += "color-link gutter-warning \"" + theme.Colors.EditorWarningBorder + "\"\n"
s += "color-link gutter-error \"" + theme.Colors.EditorErrorBorder + "\""
s += "\n"
append(&s, theme, "token.info-token", "message")
append(&s, theme, "token.error-token", "error-message")
s += "\n\n"
s += "color-link line-number \"" + theme.Colors.EditorLineNumberForeground + "\"\n"
s += "color-link indent-char \"" + theme.Colors.EditorIndentGuideBackground + "\"\n"
s += "color-link scrollbar \"" + theme.Colors.ScrollbarSliderBackground + "\"\n"
s += "color-link statusline \"" + theme.Colors.ActivityBarForeground + "\"\n"
s += "\n"
s += "color-link cursor-line \"" + theme.Colors.EditorLineHighlightBackground + "\"\n"
s += "color-link selection \"," + theme.Colors.EditorSelectionBackground + "\""

fmt.Println(s)
}

func append(s *string, theme vscode.VSCodeTheme, vscToken string, microToken string) {
t := utils.Extract(theme, vscToken)
if t != nil {
*s += "\ncolor-link " + microToken + " \""
if len(t.Style) != 0 {
*s += t.Style + " "
}
*s += t.Color + "\""
}
}
22 changes: 22 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package utils

import (
"slices"
"strings"

"github.com/RichardFevrier/vsc-theme-converter/vscode"
)

type Token struct {
Color, Style string
}

func Extract(theme vscode.VSCodeTheme, token string) *Token {
for _, t := range theme.TokenColors {
s := strings.Split(t.Scope, ", ")
if slices.Contains(s, token) {
return &Token{t.Settings.Foreground, t.Settings.FontStyle}
}
}
return nil
}
27 changes: 27 additions & 0 deletions vscode/vscode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package vscode

type VSCodeTheme struct {
Colors struct {
EditorBackground string `json:"editor.background"`
EditorForeground string `json:"editor.foreground"`
ScrollbarSliderBackground string `json:"scrollbarSlider.background"`
ActivityBarForeground string `json:"activityBar.foreground"`
EditorCursorForeground string `json:"editorCursor.foreground"`
EditorLineHighlightBackground string `json:"editor.lineHighlightBackground"`
EditorSelectionBackground string `json:"editor.selectionBackground"`
EditorLineNumberForeground string `json:"editorLineNumber.foreground"`
EditorIndentGuideBackground string `json:"editorIndentGuide.background"`
EditorGutterAddedBackground string `json:"editorGutter.addedBackground"`
EditorGutterDeletedBackground string `json:"editorGutter.deletedBackground"`
EditorGutterModifiedBackground string `json:"editorGutter.modifiedBackground"`
EditorWarningBorder string `json:"editorWarning.border"`
EditorErrorBorder string `json:"editorError.border"`
} `json:"colors"`
TokenColors []struct {
Scope string `json:"scope"`
Settings struct {
Foreground string `json:"foreground"`
FontStyle string `json:"fontStyle"`
} `json:"settings"`
} `json:"tokenColors"`
}
23 changes: 23 additions & 0 deletions wezterm/wezterm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package wezterm

import (
"fmt"

"github.com/RichardFevrier/vsc-theme-converter/vscode"
)

func Generate(theme vscode.VSCodeTheme) {

fmt.Println("\nWezterm:")

s := "\n"
s += "[colors]\n"
s += "background = \"" + theme.Colors.EditorBackground + "\"\n"
s += "foreground = \"" + theme.Colors.EditorForeground + "\"\n"
s += "cursor_bg = \"" + theme.Colors.EditorCursorForeground + "\"\n"
s += "cursor_border = \"" + theme.Colors.EditorCursorForeground + "\"\n"
s += "scrollbar_thumb = \"" + theme.Colors.ScrollbarSliderBackground + "\"\n"
s += "selection_bg = \"" + theme.Colors.EditorSelectionBackground + "\"\n"

fmt.Println(s)
}

0 comments on commit d4ce316

Please sign in to comment.