Skip to content

Commit

Permalink
fix: profile support, default profile powershell on window or sh on u…
Browse files Browse the repository at this point in the history
…nix (#58)

resolves #15
  • Loading branch information
daretodave committed Apr 28, 2024
1 parent df4f376 commit eb614e4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
29 changes: 25 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import { Profile } from './main/framework/runtime'

export const DEFAULT_WORKSPACE = '~/mterm'

export const DEFAULT_PLATFORM =
export const DEFAULT_PROFILE = process.platform === 'win32' ? 'powershell' : 'sh'
export const DEFAULT_PROFILES: Record<string, Profile> =
process.platform === 'win32'
? `${process.env.SYSTEMROOT}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -noprofile -command $ARGS`
: 'sh -c $ARGS'
? {
powershell: {
platform: `${process.env.SYSTEMROOT}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -noprofile -command $ARGS`,
theme: 'theme.css',
icon: 'default'
},
wsl: {
platform: `bash -c $ARGS`,
theme: 'theme.css',
icon: 'default'
}
}
: {
sh: {
platform: 'sh -c $ARGS',
theme: 'theme.css',
icon: 'default'
}
}
export const DEFAULT_FOLDER = '$CWD'
export const DEFAULT_SETTING_RUNNER_SHORTCUT = '`+CommandOrControl'
export const DEFAULT_SETTING_COMMANDER_MODE_TOGGLE_SHORTCUT = '`+Shift+CommandOrControl'
Expand All @@ -24,7 +44,8 @@ export const DEFAULT_SETTING_RUNNER_BOUNDS = {
h: 500
}
export const DEFAULT_SETTINGS = {
platform: DEFAULT_PLATFORM,
defaultProfile: DEFAULT_PROFILE,
profiles: DEFAULT_PROFILES,
runner: {
shortcut: DEFAULT_SETTING_RUNNER_SHORTCUT,
bounds: DEFAULT_SETTING_RUNNER_BOUNDS,
Expand Down
21 changes: 19 additions & 2 deletions src/main/framework/runtime-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { BootstrapContext } from '../bootstrap'
import {
Command,
CommandViewModel,
Profile,
ProfileMap,
Result,
ResultStream,
ResultStreamEvent,
Expand All @@ -12,7 +14,11 @@ import short from 'short-uuid'
import { execute } from './runtime-executor'
import createDOMPurify from 'dompurify'
import { JSDOM } from 'jsdom'
import { DEFAULT_PLATFORM, DEFAULT_SETTING_IS_COMMANDER_MODE } from '../../constants'
import {
DEFAULT_PROFILE,
DEFAULT_PROFILES,
DEFAULT_SETTING_IS_COMMANDER_MODE
} from '../../constants'
import Convert from 'ansi-to-html'

const convert = new Convert()
Expand Down Expand Up @@ -115,7 +121,13 @@ export function attach({ app, workspace }: BootstrapContext): void {
throw `Command '${id}' in runtime '${runtimeTarget}' does not exist`
}

const platform = workspace.settings.get<string>('platform', DEFAULT_PLATFORM)
let profileKey = runtimeTarget.profile
if (profileKey === 'default') {
profileKey = workspace.settings.get<string>('defaultProfile', DEFAULT_PROFILE)
}

const profiles = workspace.settings.get<ProfileMap>('profiles', DEFAULT_PROFILES)
const profile: Profile = profiles[profileKey]

const result: Result = command.result

Expand Down Expand Up @@ -153,6 +165,11 @@ export function attach({ app, workspace }: BootstrapContext): void {
command.error = result.code !== 0
}

if (!profile) {
throw `Profile ${profileKey} does not exist, provided by runtime as = ${runtimeTarget.profile}`
}

const platform = profile.platform
const finalizeConfirm = await execute({
platform,
workspace,
Expand Down
14 changes: 14 additions & 0 deletions src/main/framework/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,22 @@ export interface CommandViewModel {
error: boolean
id: string
}

export interface Profile {
platform: string
theme: string
icon: string
}

export interface ProfileViewModel extends Profile {
key: string
}

export type ProfileMap = Record<string, Profile>
export interface RuntimeModel {
id: string
prompt: string
profile: string
result: Result
target: boolean
folder: string
Expand All @@ -53,6 +66,7 @@ export class Runtime {
constructor(public folder: string) {
this.folder = resolveFolderPathForMTERM(folder)
}
public profile: string = 'default'
public id: string = short.generate()
public history: Command[] = []
public prompt: string = ''
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/src/runner/runner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export default function Runner(): ReactElement {
const command: Command = await window.electron.ipcRenderer.invoke(
'runtime.prepareExecute',
runtime.id,
historicalExecution ? historicalExecution.prompt : runtime.prompt
historicalExecution ? historicalExecution.prompt : runtime.prompt,
'default'
)

await reloadRuntimesFromBackend()
Expand Down
1 change: 1 addition & 0 deletions src/renderer/src/runner/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface Runtime {
result: Result
prompt: string
target: boolean
profile: string
folder: string
appearance: {
title: string
Expand Down

0 comments on commit eb614e4

Please sign in to comment.