this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #7 from patrik-csak/set-profile

set terminal profile on `enable` or `set-(dark|light)-profile`

authored by

patrik csak and committed by
GitHub
cb1ddc8b a5819775

+96 -24
+47 -13
actions/enable.js
··· 1 1 import {config} from '../config.js'; 2 2 import {packageJson} from '../constants/index.js'; 3 - import {enableAutomaticSwitching} from '../functions/index.js'; 3 + import { 4 + enableAutomaticSwitching, 5 + getCurrentMode, 6 + isTerminalOpen, 7 + setTerminalProfile, 8 + } from '../functions/index.js'; 4 9 5 - export async function enable({darkProfile, lightProfile}) { 6 - if (!darkProfile && !config.darkProfile) { 7 - throw new Error( 8 - `Dark profile must be specified with --dark-profile or previously set with \`${packageJson.name} set-dark-mode\``, 9 - ); 10 + /** 11 + * @param {string} mode 12 + */ 13 + function undefinedProfileMessage(mode) { 14 + return `${mode} profile must be specified with --${mode}-profile or previously set with \`${packageJson.name} set-${mode}-mode\``; 15 + } 16 + 17 + /** 18 + * @param {object} parameters 19 + * @param {string|undefined} parameters.darkProfile 20 + * @param {string|undefined} parameters.lightProfile 21 + */ 22 + export async function enable(parameters) { 23 + if ( 24 + [parameters.darkProfile, config.darkProfile].every( 25 + (value) => value === undefined, 26 + ) 27 + ) { 28 + throw new Error(undefinedProfileMessage('dark')); 10 29 } 11 30 12 - if (!lightProfile && !config.lightProfile) { 13 - throw new Error( 14 - `Light profile must be specified with --light-profile or previously set with \`${packageJson.name} set-light-mode\``, 15 - ); 31 + if ( 32 + [parameters.lightProfile, config.lightProfile].every( 33 + (value) => value === undefined, 34 + ) 35 + ) { 36 + throw new Error(undefinedProfileMessage('light')); 16 37 } 17 38 18 - if (darkProfile) config.darkProfile = darkProfile; 19 - if (lightProfile) config.lightProfile = lightProfile; 39 + if (parameters.darkProfile !== undefined) { 40 + config.darkProfile = parameters.darkProfile; 41 + } 42 + 43 + if (parameters.lightProfile !== undefined) { 44 + config.lightProfile = parameters.lightProfile; 45 + } 20 46 21 47 await enableAutomaticSwitching(); 22 48 23 - console.log('Automatic switching enabled'); 49 + if (await isTerminalOpen()) { 50 + const mode = await getCurrentMode(); 51 + 52 + if (parameters[`${mode}Profile`] !== undefined) { 53 + await setTerminalProfile(config[`${mode}Profile`]); 54 + } 55 + } 56 + 57 + console.log('automatic switching enabled'); 24 58 }
+15 -1
actions/set-mode-profile.js
··· 1 1 import {config} from '../config.js'; 2 + import { 3 + getCurrentMode, 4 + isAutomaticSwitchingEnabled, 5 + isTerminalOpen, 6 + setTerminalProfile, 7 + } from '../functions/index.js'; 2 8 3 - export function setModeProfile({mode, profile}) { 9 + export async function setModeProfile({mode, profile}) { 4 10 config[`${mode}Profile`] = profile; 11 + 12 + if ((await isAutomaticSwitchingEnabled()) && (await isTerminalOpen())) { 13 + const currentMode = await getCurrentMode(); 14 + 15 + if (currentMode === mode) { 16 + await setTerminalProfile(profile); 17 + } 18 + } 5 19 6 20 console.log(`${mode} mode profile set to '${profile}'`); 7 21 }
+4 -7
actions/update-profile.js
··· 1 - import darkMode from 'dark-mode'; 2 - import {setTerminalProfile, setTerminalDefaultProfile} from 'terminal-profile'; 3 1 import {config} from '../config.js'; 4 2 import { 3 + getCurrentMode, 5 4 isAutomaticSwitchingEnabled, 6 5 isTerminalOpen, 6 + setTerminalProfile, 7 7 } from '../functions/index.js'; 8 8 9 9 export async function updateProfile() { ··· 19 19 throw new Error('Light profile not set'); 20 20 } 21 21 22 - const mode = (await darkMode.isEnabled()) ? 'dark' : 'light'; 22 + const mode = await getCurrentMode(); 23 23 const profile = config[`${mode}Profile`]; 24 24 25 - await Promise.all([ 26 - setTerminalProfile(profile), 27 - setTerminalDefaultProfile(profile), 28 - ]); 25 + await setTerminalProfile(profile); 29 26 }
+7 -1
changelog.md
··· 5 5 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 6 6 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 7 7 8 + ## [Unreleased](https://github.com/patrik-csak/auto-terminal-profile/compare/v4.0.0...HEAD) 9 + 10 + ### Changed 11 + 12 + - **BREAKING**: `enable` and `set-(dark|light)-profile` now set the Terminal profile 13 + 8 14 ## [4.0.0](https://github.com/patrik-csak/auto-terminal-profile/compare/v3.0.2...v4.0.0) – 2024-03-25 9 15 10 16 ### Added 11 17 12 - - Documentation explaining how to automatically update Terminal profile when opening Terminal 18 + - Documentation explaining how to automatically update Terminal profile when opening Terminal 13 19 14 20 ### Changed 15 21
+2 -2
config.js
··· 12 12 }; 13 13 14 14 /** 15 - * @return {string} 15 + * @return {string|undefined} 16 16 */ 17 17 get darkProfile() { 18 18 return config_.get(this.keys.darkProfile); ··· 26 26 } 27 27 28 28 /** 29 - * @return {string} 29 + * @return {string|undefined} 30 30 */ 31 31 get lightProfile() { 32 32 return config_.get(this.keys.lightProfile);
+8
functions/get-current-mode.js
··· 1 + import darkMode from 'dark-mode'; 2 + 3 + /** 4 + * @returns {Promise<'dark' | 'light'>} Current macOS appearance mode 5 + */ 6 + export async function getCurrentMode() { 7 + return (await darkMode.isEnabled()) ? 'dark' : 'light'; 8 + }
+2
functions/index.js
··· 1 1 export {enableAutomaticSwitching} from './enable-automatic-switching.js'; 2 2 export {disableAutomaticSwitching} from './disable-automatic-switching.js'; 3 + export {getCurrentMode} from './get-current-mode.js'; 3 4 export {getLaunchAgentPlistFileContents} from './get-launch-agent-plist-file-contents.js'; 4 5 export {isAutomaticSwitchingEnabled} from './is-automatic-switching-enabled.js'; 5 6 export {isTerminalOpen} from './is-terminal-open.js'; 7 + export {setTerminalProfile} from './set-terminal-profile.js';
+11
functions/set-terminal-profile.js
··· 1 + import { 2 + setTerminalDefaultProfile as setDefaultProfile, 3 + setTerminalProfile as setProfile, 4 + } from 'terminal-profile'; 5 + 6 + /** 7 + * @param {string} profile 8 + */ 9 + export async function setTerminalProfile(profile) { 10 + await Promise.all([setDefaultProfile(profile), setProfile(profile)]); 11 + }