Skip to content

Commit aafc662

Browse files
committed
feat: add script postInfo and postError + add scripts
1 parent 87ae04e commit aafc662

46 files changed

Lines changed: 1390 additions & 8 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/const.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ export const MAIN_FUNCTION_EXEC = 'main(this)';
4040
// └ ┘
4141

4242
export const NOTIFY = {
43+
title: 'Text Formatter',
4344
clipboard: {
44-
title: 'Clipboard value change',
45-
subtitle: 'You have a new value in your clipboard'
45+
source: 'CLIP',
46+
text: 'Clipboard value change'
4647
}
4748
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { NOTIFY } from '../const.js';
2+
import type {
3+
Notification,
4+
NotificationItem,
5+
NotificationType
6+
} from '../types.js';
7+
import { FlowLauncher } from '../utils/flowLauncher.js';
8+
9+
export class NotificationManagerController {
10+
notifications: Notification;
11+
12+
constructor() {
13+
this.notifications = {};
14+
}
15+
16+
add(type: NotificationType, item: NotificationItem) {
17+
if (this.notifications[type] === undefined) {
18+
this.notifications[type] = [];
19+
}
20+
this.notifications[type]?.push(item);
21+
}
22+
23+
displayAll() {
24+
//INFO: Not sure if this is the best way
25+
if (Object.values(this.notifications).length > 0)
26+
FlowLauncher.showMessage(NOTIFY.title, this.format());
27+
}
28+
29+
//WARN: Possibly change in futur
30+
format(): string {
31+
const LINE_FEED = '\n';
32+
33+
let formattedNotifications = '';
34+
for (const [section, items] of Object.entries(this.notifications)) {
35+
formattedNotifications += `${String(section).toUpperCase()}${LINE_FEED}`;
36+
items.forEach(item => {
37+
formattedNotifications += `[${String(item.source).toUpperCase()}] - ${item.text}${LINE_FEED}`;
38+
});
39+
}
40+
41+
return formattedNotifications;
42+
}
43+
}

src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import process from 'node:process';
22
import type { Arguments } from './types.js';
33
import { ScriptManager } from './scriptManager.js';
44
import { FlowViewController } from './controllers/FlowViewController.js';
5+
import { NotificationManagerController } from './controllers/NotificationManagerController.js';
56

67
const args: Arguments = JSON.parse(process.argv[2] ?? '{}');
78
const { method, parameters } = args;

src/models/ScriptExecution.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1+
import { NotificationManagerController } from '../controllers/NotificationManagerController.js';
2+
13
type ScriptExecutionArguments = {
24
text: string;
5+
notifier: NotificationManagerController;
36
};
47

8+
interface ScriptExecutionJSExport {
9+
get text(): string;
10+
set text(txt: string);
11+
postError(error: string): void;
12+
postInfo(info: string): void;
13+
}
14+
515
// ╭────────────────────────────────────────────────────────────────────────────────╮
6-
// │ TODO: postInfo and postError functions (notify user) │
716
// │ TODO: insert function (clipboard + new value) │
817
// ╰────────────────────────────────────────────────────────────────────────────────╯
918

10-
export class ScriptExecution {
19+
export class ScriptExecution implements ScriptExecutionJSExport {
1120
fullText: string;
12-
constructor({ text }: ScriptExecutionArguments) {
21+
notifier: NotificationManagerController;
22+
23+
constructor({ text, notifier }: ScriptExecutionArguments) {
1324
this.fullText = text;
25+
this.notifier = notifier;
1426
}
1527

1628
get text(): string {
@@ -20,4 +32,12 @@ export class ScriptExecution {
2032
set text(txt: string) {
2133
this.fullText = txt;
2234
}
35+
36+
postError(error: string): void {
37+
this.notifier.add('ERROR', { source: 'SCRIPT', text: error });
38+
}
39+
40+
postInfo(info: string): void {
41+
this.notifier.add('INFO', { source: 'SCRIPT', text: info });
42+
}
2343
}

src/scriptManager.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@ import { matchesObject, sortByProperty, stringRange } from './utils/utils.js';
1616
import { Clipboard } from './utils/clipboard.js';
1717
import { FlowLauncher } from './utils/flowLauncher.js';
1818
import { ensureError } from './utils/error.js';
19+
import { NotificationManagerController } from './controllers/NotificationManagerController.js';
1920

2021
export class ScriptManager {
2122
scripts: Script[];
2223
scriptsError: ScriptError[];
2324

25+
//WARN: possibly change in futur
26+
notifier: NotificationManagerController;
27+
2428
constructor() {
2529
this.scripts = [];
2630
this.scriptsError = [];
31+
32+
this.notifier = new NotificationManagerController();
2733
}
2834

2935
init() {
@@ -122,10 +128,16 @@ export class ScriptManager {
122128
const result = this.runScript(script, clip);
123129

124130
this.replaceText(result, clip);
131+
132+
//TODO: Possibly change location in futur
133+
this.notifier.displayAll();
125134
}
126135

127136
runScript(script: Script, text: string): string {
128-
let scriptExecution = new ScriptExecution({ text: text });
137+
let scriptExecution = new ScriptExecution({
138+
text: text,
139+
notifier: this.notifier
140+
});
129141

130142
script.run(scriptExecution);
131143

@@ -139,8 +151,7 @@ export class ScriptManager {
139151

140152
//TODO: Possibly change location in futur
141153
// Notify User from change
142-
const { title, subtitle } = NOTIFY.clipboard;
143-
FlowLauncher.showMessage(title, subtitle);
154+
this.notifier.add('MESSAGE', NOTIFY.clipboard);
144155
}
145156
}
146157
}

src/scripts/ASCIIToHex.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
{
3+
"api":1,
4+
"name":"ASCII To Hex",
5+
"description":"Converts ASCII characters to hexadecimal codes.",
6+
"author":"aWZHY0yQH81uOYvH",
7+
"icon":"metamorphose",
8+
"tags":"ascii,hex,convert"
9+
}
10+
**/
11+
12+
function main(state) {
13+
buf = '';
14+
for (i = 0; i < state.text.length; i++) {
15+
code = state.text.charCodeAt(i).toString(16);
16+
if (code.length < 2) buf += '0';
17+
buf += code;
18+
}
19+
state.text = buf.toUpperCase();
20+
}

src/scripts/Collapse.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
{
3+
"api":1,
4+
"name":"Collapse lines",
5+
"description":"Removes all linebreaks from your text",
6+
"author":"Dennis",
7+
"icon":"collapse",
8+
"tags":"strip,remove,collapse,join"
9+
}
10+
**/
11+
12+
function main(input) {
13+
let split = input.text.split(/\r\n|\r|\n/);
14+
input.postInfo(`${split.length} lines collapsed`);
15+
input.text = split.join();
16+
}

src/scripts/CountCharacters.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
{
3+
"api":1,
4+
"name":"Count Characters",
5+
"description":"Get the length of your text",
6+
"author":"Ivan",
7+
"icon":"counter",
8+
"tags":"count,length,size,character"
9+
}
10+
**/
11+
12+
function main(input) {
13+
input.postInfo(`${input.text.length} characters`);
14+
}

src/scripts/CountLines.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
{
3+
"api":1,
4+
"name":"Count Lines",
5+
"description":"Get the line count of your text",
6+
"author":"andipaetzold",
7+
"icon":"counter",
8+
"tags":"count,length,size,line"
9+
}
10+
**/
11+
12+
function main(input) {
13+
input.postInfo(`${input.text.split('\n').length} lines`);
14+
}

src/scripts/CountWords.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
{
3+
"api":1,
4+
"name":"Count Words",
5+
"description":"Get the word count of your text",
6+
"author":"Daniel Stone",
7+
"icon":"counter",
8+
"tags":"count,length,size,words"
9+
}
10+
**/
11+
12+
function main(input) {
13+
let words = input.text.trim().match(/\S+/g);
14+
input.postInfo(`${(words && words.length) || 0} words`);
15+
}

0 commit comments

Comments
 (0)