-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes to be committed: modified: mod.ts deleted: src/accessibility.js new file: src/accessibility.ts deleted: src/format.js new file: src/format.ts deleted: src/index.js new file: src/index.ts new file: src/types.d.ts deleted: src/utility.js new file: src/utility.ts modified: test/accessibility.test.js modified: test/format.test.js modified: test/index.test.js
- Loading branch information
1 parent
21a1005
commit bd6681b
Showing
13 changed files
with
264 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { isDarkMode } from './src/accessibility.js' | ||
export { xbar, separator } from './src/index.js' | ||
export { isDarkMode } from './src/accessibility.ts' | ||
export { xbar, separator } from './src/index.ts' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { | ||
pipe, | ||
pipeCompatibleFilter, | ||
pipeCompatibleMap, | ||
pipeCompatibleReduce, | ||
pipeCompatibleSplit, | ||
} from './utility.ts'; | ||
|
||
import { Menu, MenuItem } from './types.d.ts'; | ||
// array access layer | ||
const lastIndex = (arr: string[]) => arr.length - 1; | ||
const lastItem = (arr: string[]) => arr[lastIndex(arr)]; | ||
|
||
// array element transform layer | ||
const appendLastLine = (lines: string[], word: string) => [ | ||
...lines.slice(0, lastIndex(lines)), | ||
lastItem(lines).concat(' ', word), | ||
]; | ||
const newLine = (lines: string[], newLine: string) => lines.concat(newLine); | ||
|
||
// conditional logic layer | ||
const isUnderLimit = (line: string, word: string, lineLength: number) => | ||
line.length + word.length + 1 <= lineLength || line === ''; //if line is empty always return true | ||
|
||
// array reducer | ||
const reduceLines = (lineLength: number) => | ||
(lines: string[], word: string) => { | ||
return isUnderLimit(lastItem(lines), word, lineLength) | ||
? appendLastLine(lines, word) | ||
: newLine(lines, word); | ||
}; | ||
|
||
// word wrap transformations | ||
const spiltToWords = pipeCompatibleSplit(' '); | ||
const removeExtraSpaces = pipeCompatibleFilter((word: string) => word !== ''); | ||
|
||
const reduceToLines = (maxLineLength: number) => | ||
pipeCompatibleReduce(reduceLines(maxLineLength))(['']); | ||
|
||
const passPropertiesToLInes = (propertiesToPass: Menu) => | ||
pipeCompatibleMap((line: string) => ({ | ||
text: line.trim(), | ||
...propertiesToPass, | ||
})); | ||
|
||
const castToType = (menuItem: MenuItem): { text: string; wordWrap: number } => ( | ||
{ | ||
text: `${menuItem.text}`, | ||
wordWrap: parseInt(`${menuItem.wordWrap}`), | ||
...menuItem, | ||
} | ||
); | ||
|
||
// apply transformations in order with pipe | ||
const applyWordWrap = ( | ||
{ text, wordWrap, ...menuItemProperties }: { text: string; wordWrap: number }, | ||
): MenuItem[] => | ||
pipe( | ||
spiltToWords, | ||
removeExtraSpaces, | ||
reduceToLines(wordWrap), | ||
passPropertiesToLInes(menuItemProperties), | ||
)(text); | ||
|
||
// entry layer conditional logic | ||
const hasWordWrap = (item: MenuItem) => !!item.wordWrap; | ||
const hasText = (item: MenuItem) => !!item.text; | ||
const shouldApplyWordWrap = (item: MenuItem) => | ||
hasWordWrap(item) && hasText(item); | ||
|
||
// deno-lint-ignore no-unused-vars | ||
const ignoreWordWrap = ({ wordWrap, ...item }: MenuItem) => item; | ||
|
||
// entry layer | ||
export const wordWrap = (menuItem: MenuItem) => | ||
shouldApplyWordWrap(menuItem) | ||
? applyWordWrap(castToType(menuItem)) | ||
: [ignoreWordWrap(menuItem)]; | ||
|
||
/** | ||
* returns true if XBARDarkMode is enabled. | ||
* allways returns false if 'env' permission has not been previously granted. | ||
*/ | ||
export const isDarkMode = async (): Promise<boolean> => { | ||
const permissions = await Deno.permissions.query({ name: 'env' }); | ||
return permissions.state === 'granted' | ||
? Deno.env.get('XBARDarkMode') === 'true' | ||
: false; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { pipe, pipeCompatibleReduce } from './utility.ts'; | ||
import { Menu } from './types.d.ts'; | ||
|
||
// line Object access layer | ||
const getText = (lineObj: Menu) => lineObj.text ?? ''; | ||
const getSubMenuLevel = (lineObj: Menu) => parseInt(`${lineObj.submenuLevel}`); | ||
|
||
// deno-lint-ignore no-unused-vars | ||
const ignoreSubMenuLevel = ({ submenuLevel, ...lineObj }: Menu) => lineObj; | ||
// deno-lint-ignore no-unused-vars | ||
const ignoreText = ({ text, ...lineObj }: Menu) => lineObj; | ||
|
||
// line formatting layer | ||
const getSubMenuFormatting = (lineObj: Menu) => | ||
'--'.repeat(getSubMenuLevel(lineObj)); | ||
const getFormattedText = (lineObj: Menu) => `${getText(lineObj)}`.trim(); | ||
|
||
const formatOutputOptions = pipeCompatibleReduce(( | ||
output: string, | ||
[option, value]: string, | ||
) => output.concat(` | ${option}=${value}`))(''); | ||
|
||
const getFormattedOutputOptions = pipe( | ||
ignoreText, | ||
ignoreSubMenuLevel, | ||
Object.entries, | ||
formatOutputOptions, | ||
); | ||
|
||
// entry layer | ||
export const formatLine = (lineObj: Menu) => { | ||
const formattedLine = getSubMenuFormatting(lineObj) + | ||
getFormattedText(lineObj) + | ||
getFormattedOutputOptions(lineObj); | ||
console.log(formattedLine); | ||
return formattedLine; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { pipe, pipeCompatibleMap } from './utility.ts'; | ||
import { wordWrap } from './accessibility.ts'; | ||
import { formatLine } from './format.ts'; | ||
import { Menu, MenuItem, MenuItemWithSubMenu } from './types.d.ts'; | ||
|
||
// item layer | ||
//const isSeparator = (item:MenuItem) => item === separator; | ||
|
||
const hasSubmenu = (item: MenuItem) => !!item.submenu; | ||
|
||
const getSubmenu = (item: MenuItemWithSubMenu) => | ||
item.submenu.map((e) => ({ | ||
...e, | ||
submenuLevel: item.submenuLevel ? item.submenuLevel + 1 : 1, | ||
})); | ||
|
||
// deno-lint-ignore no-unused-vars | ||
const ignoreSubmenu = ({ submenu, ...item }: MenuItem): Menu => item; | ||
|
||
const formatItem = pipe(wordWrap, pipeCompatibleMap(formatLine)); | ||
|
||
// array layer | ||
const addItem = (arr: string[], item: Menu) => [ | ||
...arr, | ||
...formatItem(item), | ||
]; | ||
|
||
const addItemWithSubmenu = (arr: string[], item: MenuItem) => [ | ||
...addItem(arr, ignoreSubmenu(item)), | ||
...pipe(getSubmenu, xbar)(item), | ||
]; | ||
|
||
//const includeSeparator = (arr:MenuItem[]) => [ | ||
// ...arr, | ||
// formatLine(separator), | ||
//]; | ||
|
||
//entry layer | ||
/** | ||
* Prints output to xbar and returns printed output as an array of strings | ||
*/ | ||
export const xbar = (layout: MenuItem[]): string[] => | ||
layout.reduce((menuItems: string[], item) => { | ||
switch (true) { | ||
// case (isSeparator(item)): | ||
// return includeSeparator(menuItems); | ||
case (hasSubmenu(item)): | ||
return addItemWithSubmenu(menuItems, item); | ||
default: | ||
return addItem(menuItems, item); | ||
} | ||
}, []); | ||
|
||
/** used for declaring a separator*/ | ||
export const separator = { | ||
text: '---', | ||
}; |
Oops, something went wrong.