Skip to content

Commit 03d350e

Browse files
committed
add variable support
1 parent 263f0c7 commit 03d350e

File tree

5 files changed

+79
-7
lines changed

5 files changed

+79
-7
lines changed

README.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ If you want to set current file with a existed file, use this:
6767
> now a.cpp
6868
```
6969

70+
Then use `edit` to open default editor to edit code:
71+
72+
```sh
73+
> edit
74+
```
75+
7076
Then use `run` command to run code.
7177

7278
```sh
@@ -92,6 +98,42 @@ Clean the compiling output and something else:
9298
> clean
9399
```
94100

101+
### Variables
102+
103+
You can use builtin variables just like in bash:
104+
105+
```sh
106+
echo $wdir
107+
```
108+
109+
|Variable|Description|
110+
|-|-|
111+
|`wdir`|Working directory|
112+
|`current`|Current file|
113+
|`input`|Input file|
114+
|`output`|Output file|
115+
|`config`|Config file|
116+
117+
### Commands
118+
119+
These are builtin commands. You can use system command in `importedCommand` list.
120+
121+
|Command|Description|
122+
|-|-|
123+
|`init`|Initialize ECR data|
124+
|`clear`|Clear ECR data|
125+
|`new [file] [-e --edit]`|Create new code file|
126+
|`now [file]`|Change current file|
127+
|`edit [file] [-n --now]`|Edit code file|
128+
|`run [file] [-io --io]`|Run code file|
129+
|`clean`|Clean temp files|
130+
|`pwd`|Print working directory|
131+
|`cd`|Change working directory|
132+
|`version`|Get version|
133+
|`cls`|Clear console|
134+
|`exit`|Exit|
135+
|`-h --help`|Get help|
136+
95137
# Config
96138

97139
The config files is at `.ecr/`
@@ -123,15 +165,18 @@ This file contains basic config.
123165
"defaultIO": "ss",
124166

125167
// The default time limit for every step when run
126-
"defaultTimeLimit": 5
168+
"defaultTimeLimit": 5,
169+
170+
// The default editor
171+
"defaultEditor": "vim"
127172
}
128173
```
129174

130175
## executor.json
131176

132177
This file gives the way to run a code file.
133178

134-
You can use these varible in command:
179+
You can use these varibles in command:
135180

136181
- `fileName` The code file name
137182
- `fileNameWithoutExt` The code file name without extension

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def read(*parts):
1515

1616
setup(
1717
name="edl-cr",
18-
version="0.0.1.4",
18+
version="0.0.1.5",
1919
description="A CLI tool to run code",
2020
long_description=long_description,
2121
long_description_content_type="text/markdown",

src/ecr/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def getITParser():
4747
cmd_clear.set_defaults(func=clear)
4848

4949
cmd_new = subpars.add_parser("new", help="Create new code file")
50-
cmd_new.add_argument("filename", nargs="?", default=None, type=str)
50+
cmd_new.add_argument("file", nargs="?", default=None, type=str)
5151
cmd_new.add_argument("-e", "--edit", action="store_true",
5252
default=False, help="Edit the file")
5353
cmd_new.set_defaults(func=new)
@@ -194,6 +194,7 @@ def main(): # pragma: no cover
194194
try:
195195
oricmd = str(console.inputCommand(
196196
f'{shared.man.currentFile if shared.man != None and shared.man.currentFile != None else ""}{defaultPrompt}', completer=getCommandCompleter(), complete_in_thread=False))
197+
oricmd = helper.formatWithVars(oricmd, shared.variables)
197198
except KeyboardInterrupt:
198199
continue
199200
except EOFError:

src/ecr/helper.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
from .shared import version
34
from .core import manager
45
from .ui import color, console
@@ -22,3 +23,20 @@ def printHead():
2223
elif shared.man.state == manager.WorkManagerState.LoadFailed:
2324
console.write(color.useRed("ECR"), end=" ")
2425
console.write(shared.cwd)
26+
27+
28+
varFormatRE = re.compile(r'\$(?P<name>[a-zA-Z_]\w*)')
29+
30+
31+
def bashVarToPythonVar(m):
32+
s = m.groupdict()
33+
return "{" + s["name"] + "}"
34+
35+
36+
def formatWithVars(oristr, var):
37+
try:
38+
tmp = {k: v() for k, v in var.items()}
39+
oristr = varFormatRE.sub(bashVarToPythonVar, oristr)
40+
return oristr.format(**tmp)
41+
except:
42+
return oristr

src/ecr/shared.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
from .core import WorkManager
1+
from .core import WorkManager, path
22
from .ui import CLI
33

4-
version = "0.0.1.4"
4+
version = "0.0.1.5"
55

66
cwd = None
77

8-
man: WorkManager = None
8+
man: WorkManager = None
9+
10+
variables = {
11+
"current": lambda: None if man == None else man.currentFile,
12+
"wdir": lambda: None if man == None else man.workingDirectory,
13+
"config": lambda: None if man == None else path.getConfigPath(man.workingDirectory),
14+
"input": lambda: None if man == None else path.getFileInputPath(man.workingDirectory),
15+
"output": lambda: None if man == None else path.getFileOutputPath(man.workingDirectory),
16+
}

0 commit comments

Comments
 (0)