<commit_before>import log from '../logger';

export default addBrightness;

const brightnessCmd: string = '/Users/mafredri/.bin/brightness';

// Conservative start value...
let brightnessValue: number = Storage.get('brightness');
let bModal: Modal;
let bModalHideHandler: number;

function addBrightness(value: number) {
	if (brightnessValue === undefined) {
		brightnessValue = 40;
	}

	if (value < 0) {
		brightnessValue = Math.max(brightnessValue + value, 0);
	} else {
		brightnessValue = Math.min(brightnessValue + value, 100);
	}

	Storage.set('brightness', brightnessValue);

	showBrightness(brightnessValue);
	Task.run(brightnessCmd, ['set', String(brightnessValue)], (t) => {
		log(t.output, t.error, t.status);
	});
}

function showBrightness(value: number) {
	clearTimeout(bModalHideHandler);
	if (!bModal) bModal = new Modal();

	bModal.text = `Brightness: ${value}`;
	bModal.showCenterOn(Screen.main());

	bModalHideHandler = setTimeout(closeBrightnessModal, 1000);
}

function closeBrightnessModal() {
	bModal.close();
	bModal = null;
	bModalHideHandler = null;
}
<commit_msg>Debounce setting the screen brightness<commit_after>import log from '../logger';
import debounce from './debounce';

export default addBrightness;

const brightnessCmd: string = '/Users/mafredri/.bin/brightness';

// Conservative start value...
let brightnessValue: number = Storage.get('brightness');
let bModal: Modal;
let bModalHideHandler: number;

const debouncedApplyBrightness = debounce(function applyBrightness() {
	Task.run(brightnessCmd, ['set', String(brightnessValue)], (t) => {
		log(t.output, t.error, t.status);
	});
}, 500);

function addBrightness(value: number) {
	if (brightnessValue === undefined) {
		brightnessValue = 40;
	}

	if (value < 0) {
		brightnessValue = Math.max(brightnessValue + value, 0);
	} else {
		brightnessValue = Math.min(brightnessValue + value, 100);
	}

	Storage.set('brightness', brightnessValue);

	showBrightness(brightnessValue);
	debouncedApplyBrightness();
}

function showBrightness(value: number) {
	clearTimeout(bModalHideHandler);
	if (!bModal) bModal = new Modal();

	bModal.text = `Brightness: ${value}`;
	bModal.showCenterOn(Screen.main());

	bModalHideHandler = setTimeout(closeBrightnessModal, 1000);
}

function closeBrightnessModal() {
	bModal.close();
	bModal = null;
	bModalHideHandler = null;
}

{'fraction_non_alphanumeric': 0.14085106382978724, 'fraction_numerical': 0.010638297872340425, 'mean_word_length': 5.567039106145251, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1742502', 'n_tokens_mistral': 843, 'n_tokens_neox': 767, 'n_words': 168}