Skip to content

Commit 5ac656a

Browse files
committed
better srcipts objects file management
1 parent 13abb57 commit 5ac656a

File tree

8 files changed

+211
-151
lines changed

8 files changed

+211
-151
lines changed

src/objects/scripting/BashScript.cpp

Lines changed: 75 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ void BashScript::setupObjectContent(shared_ptr<ofAppGLFWWindow> &mainWindow){
109109

110110
// Load script
111111
watcher.start();
112-
if(filepath == "none"){
112+
/*if(filepath == "none"){
113113
isNewObject = true;
114114
ofFile file (ofToDataPath("scripts/empty.sh"));
115115
filepath = copyFileToPatchFolder(this->patchFolderPath,file.getAbsolutePath());
116-
}
116+
}*/
117117

118118
if(!isThreadRunning()){
119119
startThread();
@@ -135,11 +135,6 @@ void BashScript::updateObjectContent(map<int,shared_ptr<PatchObject>> &patchObje
135135
}
136136
}
137137

138-
/*if(needToLoadScript){
139-
needToLoadScript = false;
140-
loadScript(filepath);
141-
}*/
142-
143138
// path watcher
144139
while(watcher.waitingEvents()) {
145140
pathChanged(watcher.nextEvent());
@@ -195,15 +190,15 @@ void BashScript::drawObjectNodeGui( ImGuiEx::NodeCanvas& _nodeCanvas ){
195190
canvasZoom = _nodeCanvas.GetCanvasScale();
196191

197192
// file dialog
198-
string newFileName = "bashScript_"+ofGetTimestampString("%y%m%d")+".sh";
193+
/*string newFileName = "bashScript_"+ofGetTimestampString("%y%m%d")+".sh";
199194
if(ImGuiEx::getFileDialog(fileDialog, saveScriptFlag, "Save new bash script as", imgui_addons::ImGuiFileBrowser::DialogMode::SAVE, ".sh", newFileName, scaleFactor)){
200195
ofFile fileToRead(ofToDataPath("scripts/empty.sh"));
201196
ofFile newBashFile (fileDialog.selected_path);
202197
ofFile::copyFromTo(fileToRead.getAbsolutePath(),checkFileExtension(newBashFile.getAbsolutePath(), ofToUpper(newBashFile.getExtension()), "SH"),true,true);
203198
filepath = copyFileToPatchFolder(this->patchFolderPath,checkFileExtension(newBashFile.getAbsolutePath(), ofToUpper(newBashFile.getExtension()), "SH"));
204199
threadLoaded = false;
205200
reloadScript();
206-
}
201+
}*/
207202

208203
if(ImGuiEx::getFileDialog(fileDialog, loadScriptFlag, "Select a bash script", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, ".sh", "", scaleFactor)){
209204
ofFile bashFile (fileDialog.selected_path);
@@ -233,6 +228,47 @@ void BashScript::drawObjectNodeConfig(){
233228
if(ImGui::Button("New",ImVec2(224*scaleFactor,26*scaleFactor))){
234229
saveScriptFlag = true;
235230
}
231+
232+
if(saveScriptFlag){
233+
newScriptName = "bashScript_"+ofGetTimestampString("%y%m%d")+".sh";
234+
ImGui::OpenPopup("Save new bash script as");
235+
}
236+
237+
if(ImGui::BeginPopup("Save new bash script as")){
238+
239+
if(ImGui::InputText("##NewFileNameInput", &newScriptName,ImGuiInputTextFlags_EnterReturnsTrue)){
240+
if(newScriptName != ""){
241+
// save file in data/ folder
242+
ofFile fileToRead(ofToDataPath("scripts/empty.sh"));
243+
ofFile newBashFile (this->patchFolderPath+newScriptName);
244+
ofFile::copyFromTo(fileToRead.getAbsolutePath(),checkFileExtension(newBashFile.getAbsolutePath(), ofToUpper(newBashFile.getExtension()), "SH"),true,true);
245+
filepath = this->patchFolderPath+newScriptName;
246+
threadLoaded = false;
247+
reloadScript();
248+
}
249+
ImGui::CloseCurrentPopup();
250+
}
251+
ImGui::SameLine();
252+
if(ImGui::Button("Cancel")){
253+
ImGui::CloseCurrentPopup();
254+
}
255+
ImGui::SameLine();
256+
if(ImGui::Button("Create")){
257+
if(newScriptName != ""){
258+
// save file in data/ folder
259+
ofFile fileToRead(ofToDataPath("scripts/empty.sh"));
260+
ofFile newBashFile (this->patchFolderPath+newScriptName);
261+
ofFile::copyFromTo(fileToRead.getAbsolutePath(),checkFileExtension(newBashFile.getAbsolutePath(), ofToUpper(newBashFile.getExtension()), "SH"),true,true);
262+
filepath = this->patchFolderPath+newScriptName;
263+
threadLoaded = false;
264+
reloadScript();
265+
}
266+
ImGui::CloseCurrentPopup();
267+
}
268+
ImGui::EndPopup();
269+
270+
}
271+
236272
ImGui::Spacing();
237273
if(ImGui::Button("Open",ImVec2(224*scaleFactor,26*scaleFactor))){
238274
loadScriptFlag = true;
@@ -243,16 +279,6 @@ void BashScript::drawObjectNodeConfig(){
243279
"Load and run a bash script files (Bourne-Again SHell). You can type code with the Mosaic code editor, or with your default code editor. Scripts will refresh automatically on save.",
244280
"https://mosaic.d3cod3.org/reference.php?r=bash-script", scaleFactor);
245281

246-
// file dialog
247-
string newFileName = "bashScript_"+ofGetTimestampString("%y%m%d")+".sh";
248-
if(ImGuiEx::getFileDialog(fileDialog, saveScriptFlag, "Save new bash script as", imgui_addons::ImGuiFileBrowser::DialogMode::SAVE, ".sh", newFileName, scaleFactor)){
249-
ofFile fileToRead(ofToDataPath("scripts/empty.sh"));
250-
ofFile newBashFile (fileDialog.selected_path);
251-
ofFile::copyFromTo(fileToRead.getAbsolutePath(),checkFileExtension(newBashFile.getAbsolutePath(), ofToUpper(newBashFile.getExtension()), "SH"),true,true);
252-
filepath = copyFileToPatchFolder(this->patchFolderPath,checkFileExtension(newBashFile.getAbsolutePath(), ofToUpper(newBashFile.getExtension()), "SH"));
253-
threadLoaded = false;
254-
reloadScript();
255-
}
256282

257283
if(ImGuiEx::getFileDialog(fileDialog, loadScriptFlag, "Select a bash script", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, ".sh", "", scaleFactor)){
258284
ofFile bashFile (fileDialog.selected_path);
@@ -264,10 +290,6 @@ void BashScript::drawObjectNodeConfig(){
264290

265291
//--------------------------------------------------------------
266292
void BashScript::removeObjectContent(bool removeFileFromData){
267-
if(filepath != ofToDataPath("scripts/empty.sh",true) && removeFileFromData){
268-
//removeFile(filepath);
269-
}
270-
271293
std::unique_lock<std::mutex> lck(mutex);
272294
stopThread();
273295
condition.notify_all();
@@ -277,41 +299,45 @@ void BashScript::removeObjectContent(bool removeFileFromData){
277299
//--------------------------------------------------------------
278300
void BashScript::loadScript(string scriptFile){
279301

280-
filepath = forceCheckMosaicDataPath(scriptFile);
281-
currentScriptFile.open(filepath);
302+
ofFile tmpf(scriptFile);
303+
304+
if(tmpf.isFile() && ofToUpper(tmpf.getExtension()) == "SH"){
305+
filepath = forceCheckMosaicDataPath(scriptFile);
306+
currentScriptFile.open(filepath);
282307

283-
string cmd = "";
284-
FILE *execFile;
308+
string cmd = "";
309+
FILE *execFile;
285310

286-
cmd = "sh "+filepath;
287-
execFile = popen(cmd.c_str(), "r");
311+
cmd = "sh "+filepath;
312+
execFile = popen(cmd.c_str(), "r");
288313

289-
if (execFile){
290-
scriptLoaded = true;
291-
watcher.removeAllPaths();
292-
watcher.addPath(filepath);
314+
if (execFile){
315+
scriptLoaded = true;
316+
watcher.removeAllPaths();
317+
watcher.addPath(filepath);
293318

294-
ofLog(OF_LOG_NOTICE,"[verbose] bash script: %s RUNNING!",filepath.c_str());
295-
ofLog(OF_LOG_NOTICE," ");
319+
ofLog(OF_LOG_NOTICE,"[verbose] bash script: %s RUNNING!",filepath.c_str());
320+
ofLog(OF_LOG_NOTICE," ");
296321

297-
char buffer[128];
298-
_outletParams[0] = new string();
299-
*static_cast<string *>(_outletParams[0]) = "";
300-
while(!feof(execFile)){
301-
if(fgets(buffer, sizeof(buffer), execFile) != nullptr){
302-
char *s = buffer;
303-
std::string tempstr(s);
304-
static_cast<string *>(_outletParams[0])->append(tempstr);
305-
static_cast<string *>(_outletParams[0])->append(" ");
322+
char buffer[128];
323+
_outletParams[0] = new string();
324+
*static_cast<string *>(_outletParams[0]) = "";
325+
while(!feof(execFile)){
326+
if(fgets(buffer, sizeof(buffer), execFile) != nullptr){
327+
char *s = buffer;
328+
std::string tempstr(s);
329+
static_cast<string *>(_outletParams[0])->append(tempstr);
330+
static_cast<string *>(_outletParams[0])->append(" ");
331+
}
306332
}
307-
}
308-
//ofLog(OF_LOG_NOTICE,"%s",static_cast<string *>(_outletParams[0])->c_str());
309-
ofLog(OF_LOG_NOTICE,"[verbose]bash script: %s EXECUTED!",filepath.c_str());
333+
//ofLog(OF_LOG_NOTICE,"%s",static_cast<string *>(_outletParams[0])->c_str());
334+
ofLog(OF_LOG_NOTICE,"[verbose]bash script: %s EXECUTED!",filepath.c_str());
310335

311-
this->saveConfig(false);
336+
this->saveConfig(false);
312337

313-
pclose(execFile);
338+
pclose(execFile);
314339

340+
}
315341
}
316342

317343
}

src/objects/scripting/BashScript.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class BashScript : public ofThread, public PatchObject{
7575
string lastMessage;
7676

7777
imgui_addons::ImGuiFileBrowser fileDialog;
78+
string newScriptName;
7879

7980
ofImage *bashIcon;
8081
float posX, posY, drawW, drawH;

src/objects/scripting/LuaScript.cpp

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ LuaScript::LuaScript() : PatchObject("lua script"){
7676
setupTrigger = false;
7777

7878
lastLuaScript = "";
79-
newFileFromFilepath = ofToDataPath("scripts/empty.lua");
8079
loadLuaScriptFlag = false;
8180
saveLuaScriptFlag = false;
8281
luaScriptLoaded = false;
@@ -116,12 +115,6 @@ void LuaScript::autoloadFile(string _fp){
116115
openScript(_fp);
117116
}
118117

119-
//--------------------------------------------------------------
120-
void LuaScript::autosaveNewFile(string fromFile){
121-
newFileFromFilepath = fromFile;
122-
saveScript(newFileFromFilepath);
123-
}
124-
125118
//--------------------------------------------------------------
126119
void LuaScript::setupObjectContent(shared_ptr<ofAppGLFWWindow> &mainWindow){
127120

@@ -147,19 +140,12 @@ void LuaScript::setupObjectContent(shared_ptr<ofAppGLFWWindow> &mainWindow){
147140
static_cast<LiveCoding *>(_outletParams[1])->lua.addListener(this);
148141
watcher.start();
149142

150-
if(filepath == "none"){
151-
isNewObject = true;
152-
ofFile file (ofToDataPath("scripts/empty.lua"));
153-
//filepath = file.getAbsolutePath();
154-
filepath = copyFileToPatchFolder(this->patchFolderPath,file.getAbsolutePath());
155-
}
156-
157143
}
158144

159145
//--------------------------------------------------------------
160146
void LuaScript::updateObjectContent(map<int,shared_ptr<PatchObject>> &patchObjects){
161147

162-
if(needToLoadScript){
148+
if(needToLoadScript && filepath != "none"){
163149
needToLoadScript = false;
164150
loadScript(filepath);
165151
setupTrigger = false;
@@ -187,7 +173,7 @@ void LuaScript::updateObjectContent(map<int,shared_ptr<PatchObject>> &patchObjec
187173

188174
if(luaScriptSaved){
189175
luaScriptSaved = false;
190-
saveScript(lastLuaScript);
176+
newScript(lastLuaScript);
191177
}
192178

193179
if(!loaded && ofGetElapsedTimeMillis()-loadTime > 1000){
@@ -196,7 +182,7 @@ void LuaScript::updateObjectContent(map<int,shared_ptr<PatchObject>> &patchObjec
196182
prevH = this->getCustomVar("HEIGHT");
197183
this->width = prevW;
198184
this->height = prevH;
199-
reloadScriptThreaded();
185+
reloadScript();
200186
}
201187
}
202188

@@ -344,11 +330,11 @@ void LuaScript::drawObjectNodeGui( ImGuiEx::NodeCanvas& _nodeCanvas ){
344330
canvasZoom = _nodeCanvas.GetCanvasScale();
345331

346332
// file dialog
347-
string newFileName = "luaScript_"+ofGetTimestampString("%y%m%d")+".lua";
333+
/*string newFileName = "luaScript_"+ofGetTimestampString("%y%m%d")+".lua";
348334
if(ImGuiEx::getFileDialog(fileDialog, saveLuaScriptFlag, "Save new Lua script as", imgui_addons::ImGuiFileBrowser::DialogMode::SAVE, ".lua", newFileName, scaleFactor)){
349335
lastLuaScript = fileDialog.selected_path;
350336
luaScriptSaved = true;
351-
}
337+
}*/
352338

353339
if(ImGuiEx::getFileDialog(fileDialog, loadLuaScriptFlag, "Select a lua script", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, ".lua", "", scaleFactor)){
354340
lastLuaScript = fileDialog.selected_path;
@@ -376,6 +362,39 @@ void LuaScript::drawObjectNodeConfig(){
376362
if(ImGui::Button("New",ImVec2(224*scaleFactor,26*scaleFactor))){
377363
saveLuaScriptFlag = true;
378364
}
365+
366+
if(saveLuaScriptFlag){
367+
newScriptName = "luaScript_"+ofGetTimestampString("%y%m%d")+".lua";
368+
ImGui::OpenPopup("Save new lua script as");
369+
}
370+
371+
if(ImGui::BeginPopup("Save new lua script as")){
372+
373+
if(ImGui::InputText("##NewFileNameInput", &newScriptName,ImGuiInputTextFlags_EnterReturnsTrue)){
374+
if(newScriptName != ""){
375+
// save file in data/ folder
376+
lastLuaScript = this->patchFolderPath+newScriptName;
377+
luaScriptSaved = true;
378+
}
379+
ImGui::CloseCurrentPopup();
380+
}
381+
ImGui::SameLine();
382+
if(ImGui::Button("Cancel")){
383+
ImGui::CloseCurrentPopup();
384+
}
385+
ImGui::SameLine();
386+
if(ImGui::Button("Create")){
387+
if(newScriptName != ""){
388+
// save file in data/ folder
389+
lastLuaScript = this->patchFolderPath+newScriptName;
390+
luaScriptSaved = true;
391+
}
392+
ImGui::CloseCurrentPopup();
393+
}
394+
ImGui::EndPopup();
395+
396+
}
397+
379398
ImGui::Spacing();
380399
if(ImGui::Button("Open",ImVec2(224*scaleFactor,26*scaleFactor))){
381400
loadLuaScriptFlag = true;
@@ -389,20 +408,14 @@ void LuaScript::drawObjectNodeConfig(){
389408
}
390409
ImGui::Spacing();
391410
if(ImGui::Button("Reload Script",ImVec2(224*scaleFactor,26*scaleFactor))){
392-
reloadScriptThreaded();
411+
reloadScript();
393412
}
394413

395414
ImGuiEx::ObjectInfo(
396415
"This object is a live-coding lua script container, with OF bindings mimicking the OF programming structure. You can type code with the Mosaic code editor, or with your default code editor. Scripts will refresh automatically on save.",
397416
"https://mosaic.d3cod3.org/reference.php?r=lua-script", scaleFactor);
398417

399418
// file dialog
400-
string newFileName = "luaScript_"+ofGetTimestampString("%y%m%d")+".lua";
401-
if(ImGuiEx::getFileDialog(fileDialog, saveLuaScriptFlag, "Save new Lua script as", imgui_addons::ImGuiFileBrowser::DialogMode::SAVE, ".lua", newFileName, scaleFactor)){
402-
lastLuaScript = fileDialog.selected_path;
403-
luaScriptSaved = true;
404-
}
405-
406419
if(ImGuiEx::getFileDialog(fileDialog, loadLuaScriptFlag, "Select a lua script", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, ".lua", "", scaleFactor)){
407420
lastLuaScript = fileDialog.selected_path;
408421
luaScriptLoaded = true;
@@ -415,10 +428,6 @@ void LuaScript::removeObjectContent(bool removeFileFromData){
415428
// LUA EXIT
416429
static_cast<LiveCoding *>(_outletParams[1])->lua.scriptExit();
417430
///////////////////////////////////////////
418-
419-
if(removeFileFromData){
420-
//removeFile(filepath);
421-
}
422431
}
423432

424433
//--------------------------------------------------------------
@@ -543,26 +552,25 @@ void LuaScript::openScript(string scriptFile){
543552
filepath = copyFileToPatchFolder(this->patchFolderPath,file.getAbsolutePath());
544553
static_cast<LiveCoding *>(_outletParams[1])->liveEditor.openFile(filepath);
545554
static_cast<LiveCoding *>(_outletParams[1])->liveEditor.reset();
546-
reloadScriptThreaded();
555+
reloadScript();
547556
}
548557
}
549558
}
550559

551560
//--------------------------------------------------------------
552-
void LuaScript::saveScript(string scriptFile){
553-
ofFile fileToRead(newFileFromFilepath);
561+
void LuaScript::newScript(string scriptFile){
562+
ofFile fileToRead(ofToDataPath("scripts/empty.lua"));
554563
ofFile newLuaFile (scriptFile);
555564
ofFile::copyFromTo(fileToRead.getAbsolutePath(),checkFileExtension(newLuaFile.getAbsolutePath(), ofToUpper(newLuaFile.getExtension()), "LUA"),true,true);
556-
filepath = copyFileToPatchFolder(this->patchFolderPath,checkFileExtension(newLuaFile.getAbsolutePath(), ofToUpper(newLuaFile.getExtension()), "LUA"));
565+
filepath = scriptFile;
557566
static_cast<LiveCoding *>(_outletParams[1])->liveEditor.openFile(filepath);
558567
static_cast<LiveCoding *>(_outletParams[1])->liveEditor.reset();
559-
reloadScriptThreaded();
568+
reloadScript();
560569
}
561570

562571
//--------------------------------------------------------------
563572
void LuaScript::loadScript(string scriptFile){
564573

565-
//filepath = forceCheckMosaicDataPath(scriptFile);
566574
currentScriptFile.open(filepath);
567575

568576
static_cast<LiveCoding *>(_outletParams[1])->filepath = filepath;
@@ -679,8 +687,7 @@ void LuaScript::clearScript(){
679687
}
680688

681689
//--------------------------------------------------------------
682-
void LuaScript::reloadScriptThreaded(){
683-
newFileFromFilepath = ofToDataPath("scripts/empty.lua");
690+
void LuaScript::reloadScript(){
684691

685692
unloadScript();
686693

@@ -698,7 +705,7 @@ void LuaScript::pathChanged(const PathWatcher::Event &event) {
698705
case PathWatcher::MODIFIED:
699706
//ofLogVerbose(PACKAGE) << "path modified " << event.path;
700707
filepath = event.path;
701-
reloadScriptThreaded();
708+
reloadScript();
702709
break;
703710
case PathWatcher::DELETED:
704711
//ofLogVerbose(PACKAGE) << "path deleted " << event.path;

0 commit comments

Comments
 (0)