Skip to content

Commit

Permalink
fetch theme from the server
Browse files Browse the repository at this point in the history
  • Loading branch information
pomdtr committed Dec 7, 2023
1 parent aac498a commit 53d2e8c
Show file tree
Hide file tree
Showing 304 changed files with 63 additions and 30 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ name: Deploy static content to Pages
on:
# Runs on pushes targeting the default branch
push:
branches: ["main"]
# run only against tags
tags:
- "v*.*.*"

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand Down
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"deno.enablePaths": [
"./scripts"
]
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ custom path.
## Endpoints

- `/` - Open Default Profile
- `/p/<profile>` - Open Profile
- `/config` - View Configuration
- `/?reload=true` - Reload the Page when the Command Exits
- `/?profile=<profile>` - Open Specific Profile
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Config struct {
}

type Profile struct {
Command string `json:"command"`
Command string `json:"command,omitempty"`
Args []string `json:"args,omitempty"`
Cwd string `json:"cwd,omitempty"`
Env map[string]string `json:"env,omitempty"`
Expand Down
9 changes: 5 additions & 4 deletions frontend/src/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { AttachAddon } from "xterm-addon-attach";
import { nanoid } from "nanoid";
import { Config } from "./config";

async function importTheme(name: string) {
return fetchJSON(`${import.meta.env.BASE_URL}themes/${name}.json`) as Promise<ITheme>
async function fetchTheme(name: string, origin: URL) {
const themeUrl = new URL(`/themes/${name}.json`, origin)
return fetchJSON(themeUrl) as Promise<ITheme>
}

async function fetchJSON(url: string | URL, options?: RequestInit) {
Expand All @@ -27,8 +28,8 @@ async function main() {
}

const config = await fetchJSON(new URL("/config", origin)) as Config
const lightTheme = await importTheme(config.theme || "Tomorrow")
const darkTheme = await importTheme(config.themeDark || config.theme || "Tomorrow Night")
const lightTheme = await fetchTheme(config.theme || "Tomorrow", origin)
const darkTheme = await fetchTheme(config.themeDark || config.theme || "Tomorrow Night", origin)
const terminal = new Terminal({
cursorBlink: true,
allowProposedApi: true,
Expand Down
36 changes: 19 additions & 17 deletions frontend/scripts/refresh-themes.mjs → scripts/refresh-themes.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// @ts-check
#!/usr/bin/env deno run -A

import degit from "npm:degit"
import * as path from "https://deno.land/std/path/mod.ts"
import { existsSync } from "https://deno.land/std/fs/mod.ts";

import degit from "degit"
import os from "os"
import path from "path"
import fs from "fs/promises"

const dirname = path.dirname(new URL(import.meta.url).pathname)
const emitter = await degit('mbadolato/iTerm2-Color-Schemes/vscode', {
Expand All @@ -12,14 +12,14 @@ const emitter = await degit('mbadolato/iTerm2-Color-Schemes/vscode', {
verbose: true
})

emitter.on('info', info => {
emitter.on('info', (info: any) => {
console.log(info.message)
})

const cloneDir = path.join(os.tmpdir(), "degit")
const cloneDir = path.join(Deno.makeTempDirSync(), "degit")
await emitter.clone(cloneDir)

const entries = await fs.readdir(cloneDir, { withFileTypes: true })
const entries = await Deno.readDirSync(cloneDir)
const keyMapping = {
"terminal.foreground": "foreground",
"terminal.background": "background",
Expand All @@ -41,19 +41,21 @@ const keyMapping = {
"terminal.ansiBrightYellow": "ansiBrightYellow",
"terminal.selectionBackground": "selectionBackground",
"terminalCursor.foreground": "cursor"
}
const themeDir = path.join(dirname, "..", "public", "themes")
} as Record<string, string>
const themeDir = path.join(dirname, "..", "themes")

await fs.rm(themeDir, { recursive: true, force: true })
await fs.mkdir(themeDir)
if (existsSync(themeDir)) {
Deno.removeSync(themeDir, { recursive: true })
}
Deno.mkdirSync(themeDir)

for (const entry of entries) {
const vscodeTheme = JSON.parse(await fs.readFile(path.join(cloneDir, entry.name), { encoding: "utf-8" }))
const xtermTheme = {}
const vscodeTheme = JSON.parse(Deno.readTextFileSync(path.join(cloneDir, entry.name)))
const xtermTheme: Record<string, string> = {}
for (const [key, value] of Object.entries(vscodeTheme["workbench.colorCustomizations"])) {
xtermTheme[keyMapping[key]] = value
xtermTheme[keyMapping[key]] = value as string
}
await fs.writeFile(path.join(themeDir, entry.name), JSON.stringify(xtermTheme, null, 4))
await Deno.writeTextFileSync(path.join(themeDir, entry.name), JSON.stringify(xtermTheme, null, 4))
}

await fs.rm(path.join(cloneDir), { recursive: true, force: true })
Deno.removeSync(path.join(cloneDir), { recursive: true })
33 changes: 28 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ func NewHandler() (http.Handler, error) {
return
}

config.Env = nil
for k, profile := range config.Profiles {
profile.Command = ""
profile.Args = nil
profile.Cwd = ""
profile.Env = nil
config.Profiles[k] = profile
}

encoder := json.NewEncoder(w)
encoder.SetIndent("", " ")
encoder.SetEscapeHTML(false)
Expand All @@ -61,11 +70,6 @@ func NewHandler() (http.Handler, error) {
}
})

r.Get("/p/{profileName}", func(w http.ResponseWriter, r *http.Request) {
profileName := chi.URLParam(r, "profileName")
http.Redirect(w, r, fmt.Sprintf("/?profile=%s", profileName), http.StatusFound)
})

ttyMap := make(map[string]*os.File)
r.Get("/pty/{terminalID}", func(w http.ResponseWriter, r *http.Request) {
terminalID := chi.URLParam(r, "terminalID")
Expand Down Expand Up @@ -193,6 +197,13 @@ func NewHandler() (http.Handler, error) {
w.Write([]byte("Resized"))
})

themeHandler, err := ThemeHandler()
if err != nil {
return nil, err
}

r.Handle("/themes/*", http.StripPrefix("/themes", themeHandler))

frontendHandler, err := FrontendHandler()
if err != nil {
return nil, err
Expand All @@ -214,6 +225,18 @@ func FrontendHandler() (http.Handler, error) {
return http.FileServer(http.FS(fs)), nil
}

//go:embed all:themes
var themes embed.FS

func ThemeHandler() (http.Handler, error) {
fs, err := fs.Sub(themes, "themes")
if err != nil {
return nil, err
}

return http.FileServer(http.FS(fs)), nil
}

var (
maxBufferSizeBytes = 512
keepalivePingTimeout = 20 * time.Second
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 53d2e8c

Please sign in to comment.