starlight-cli 1.0.15 → 1.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.js +60 -23483
  2. package/package.json +1 -1
  3. package/src/starlight.js +37 -66
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-cli",
3
- "version": "1.0.15",
3
+ "version": "1.0.16",
4
4
  "description": "Starlight Programming Language CLI",
5
5
  "bin": {
6
6
  "starlight": "index.js"
package/src/starlight.js CHANGED
@@ -2,14 +2,14 @@ const fs = require('fs');
2
2
  const path = require('path');
3
3
  const os = require('os');
4
4
  const { exec } = require('child_process');
5
+ const readline = require('readline');
5
6
  const readlineSync = require('readline-sync');
6
- const blessed = require('blessed');
7
7
 
8
8
  const Lexer = require('./lexer');
9
9
  const Parser = require('./parser');
10
10
  const Evaluator = require('./evaluator');
11
11
 
12
- const VERSION = '1.0.15';
12
+ const VERSION = '1.0.16';
13
13
 
14
14
  const COLOR = {
15
15
  reset: '\x1b[0m',
@@ -23,6 +23,7 @@ const COLOR = {
23
23
  magenta: '\x1b[35m'
24
24
  };
25
25
 
26
+
26
27
  function waitAndExit(code = 0) {
27
28
  try { process.stdin.setRawMode(true); } catch {}
28
29
  console.error(COLOR.gray + '\nPress any key to exit...' + COLOR.reset);
@@ -35,13 +36,21 @@ function fatal(msg) {
35
36
  waitAndExit(1);
36
37
  }
37
38
 
38
- function highlightCode(text) {
39
- return text
39
+
40
+ function highlightCode(line) {
41
+ return line
42
+ // strings
40
43
  .replace(/"(?:\\.|[^"])*"/g, m => COLOR.yellow + m + COLOR.reset)
44
+ // numbers
41
45
  .replace(/\b\d+(\.\d+)?\b/g, m => COLOR.magenta + m + COLOR.reset)
42
- .replace(/\b(sldeploy|import|from|const|let|var|if|else|for|while|func|return|break|continue|define|ask)\b/g, m => COLOR.blue + m + COLOR.reset);
46
+ // keywords
47
+ .replace(
48
+ /\b(sldeploy|import|from|const|let|var|if|else|for|while|func|return|break|continue|define|ask)\b/g,
49
+ m => COLOR.blue + m + COLOR.reset
50
+ );
43
51
  }
44
52
 
53
+
45
54
  const args = process.argv.slice(2);
46
55
 
47
56
  if (args.length === 0) {
@@ -66,7 +75,7 @@ if (args[0] === '-v' || args[0] === '--version') {
66
75
  if (args[0] === '--help') {
67
76
  console.log(`
68
77
  starlight <file.sl> Run file
69
- starlight --writedirectly Interactive editor
78
+ starlight --writedirectly Interactive editor (Notepad and VS Code are recommended!)
70
79
  starlight --learn Learning guide
71
80
  `);
72
81
  process.exit(0);
@@ -77,78 +86,40 @@ if (args[0] === '--learn') {
77
86
  process.exit(0);
78
87
  }
79
88
 
89
+
80
90
  if (args[0] === '--writedirectly') {
81
91
  const tempFile = path.join(os.tmpdir(), `starlight-${Date.now()}.sl`);
92
+ const lines = [];
82
93
 
83
- const screen = blessed.screen({
84
- smartCSR: true,
85
- title: 'Starlight Editor'
86
- });
94
+ console.log(COLOR.green + 'Interactive Starlight Editor' + COLOR.reset);
95
+ console.log(COLOR.gray + 'Type code. Use :run to execute.\n' + COLOR.reset);
87
96
 
88
- const statusBar = blessed.box({
89
- top: 0,
90
- left: 0,
91
- height: 1,
92
- width: '100%',
93
- content: 'Starlight Editor | Ctrl+S Save | Ctrl+R Run | Ctrl+Q Quit',
94
- style: { fg: 'black', bg: 'green' }
97
+ const rl = readline.createInterface({
98
+ input: process.stdin,
99
+ output: process.stdout,
100
+ terminal: false
95
101
  });
96
102
 
97
- const editor = blessed.textarea({
98
- top: 1,
99
- left: 0,
100
- width: '100%',
101
- height: '100%-2',
102
- keys: true,
103
- mouse: true,
104
- scrollbar: { bg: 'gray' },
105
- style: { fg: 'white', bg: 'black' },
106
- inputOnFocus: true
107
- });
108
-
109
- const output = blessed.box({
110
- bottom: 0,
111
- left: 0,
112
- height: 1,
113
- width: '100%',
114
- content: ' Ready ',
115
- style: { fg: 'white', bg: 'blue' }
116
- });
103
+ process.stdout.write(COLOR.cyan + '> ' + COLOR.reset);
117
104
 
118
- screen.append(statusBar);
119
- screen.append(editor);
120
- screen.append(output);
105
+ rl.on('line', (line) => {
106
+ if (line.trim() === ':run') {
107
+ rl.close();
108
+ fs.writeFileSync(tempFile, lines.join('\n'), 'utf8');
109
+ runFile(tempFile, true, () => savePrompt(lines));
110
+ return;
111
+ }
121
112
 
122
- if (fs.existsSync(tempFile)) {
123
- editor.setValue(fs.readFileSync(tempFile, 'utf8'));
124
- }
125
-
126
- editor.focus();
127
- screen.render();
128
-
129
- screen.key(['C-q'], () => screen.destroy());
130
-
131
- screen.key(['C-s'], () => {
132
- fs.writeFileSync(tempFile, editor.getValue());
133
- output.setContent(' Saved ');
134
- screen.render();
135
- });
136
-
137
- screen.key(['C-r'], () => {
138
- fs.writeFileSync(tempFile, editor.getValue());
139
- output.setContent(' Running... ');
140
- screen.render();
141
-
142
- runFile(tempFile, true, () => {
143
- output.setContent(' Program finished ');
144
- screen.render();
145
- });
113
+ lines.push(line);
114
+ process.stdout.write(
115
+ COLOR.cyan + '> ' + COLOR.reset + highlightCode(line) + '\n'
116
+ );
146
117
  });
147
118
 
148
- screen.render();
149
119
  return;
150
120
  }
151
121
 
122
+
152
123
  function savePrompt(lines) {
153
124
  try { process.stdin.setRawMode(false); } catch {}
154
125
 
@@ -202,4 +173,4 @@ function runFile(filePath, isTemp = false, callback) {
202
173
 
203
174
  if (!args[0].startsWith('--')) {
204
175
  runFile(path.resolve(args[0]));
205
- }
176
+ }