@@ -2,6 +2,8 @@ import { basename, dirname, join } from 'path';
22import { ExtensionContext , commands , Uri , window , workspace } from 'vscode' ;
33import { state } from './extension' ;
44
5+ const sketchNumber = 0 ;
6+
57export function setupCommands ( context : ExtensionContext ) {
68 const runSketch = commands . registerCommand ( 'processing.sketch.run' , ( resource : Uri ) => {
79 // TODO: Use VScode contexts to highlight run button when sketch is running, blocked until we do not run the sketch in a terminal
@@ -104,23 +106,20 @@ export function setupCommands(context: ExtensionContext) {
104106 } ) ;
105107
106108 const newSketch = commands . registerCommand ( 'processing.sketch.new' , async ( ) => {
107- const folder = await window . showOpenDialog ( {
108- canSelectFiles : false ,
109- canSelectFolders : true ,
110- canSelectMany : false ,
111- title : "Select a folder to create a new sketch in" ,
112- defaultUri : workspace . workspaceFolders && workspace . workspaceFolders [ 0 ]
113- ? Uri . joinPath ( workspace . workspaceFolders [ 0 ] . uri , '..' )
114- : undefined ,
115- } ) ;
116- if ( ! folder || folder . length === 0 ) {
117- return ;
109+ const lastSketchNumberReset = context . globalState . get < number > ( 'lastSketchNumberReset' , 0 ) ;
110+ const now = Date . now ( ) ;
111+ // Reset the sketch number if it has been more than 24 hours since the last reset
112+ if ( now - lastSketchNumberReset > 24 * 60 * 60 * 1000 ) {
113+ context . globalState . update ( 'sketchNumber' , 0 ) ;
114+ context . globalState . update ( 'lastSketchNumberReset' , now ) ;
118115 }
119- // TODO: increment a,b,c if a sketch with the same name already exists
116+
117+ const sketchNumber = context . globalState . get < number > ( 'sketchNumber' , 0 ) ;
118+
120119 const sketchName = await window . showInputBox ( {
121120 prompt : "Enter the name of the new sketch" ,
122121 placeHolder : "Sketch name" ,
123- value : `sketch_${ new Date ( ) . toISOString ( ) . slice ( 2 , 10 ) . replace ( / - / g, '' ) } a ` ,
122+ value : `sketch_${ new Date ( ) . toISOString ( ) . slice ( 2 , 10 ) . replace ( / - / g, '' ) } ${ numberToAlpha ( sketchNumber ) } ` ,
124123 validateInput : ( value ) => {
125124 if ( ! value || value . trim ( ) === "" ) {
126125 return "Sketch name cannot be empty." ;
@@ -132,20 +131,47 @@ export function setupCommands(context: ExtensionContext) {
132131 window . showErrorMessage ( "No sketch name provided." ) ;
133132 return ;
134133 }
134+ const folder = await window . showOpenDialog ( {
135+ canSelectFiles : false ,
136+ canSelectFolders : true ,
137+ canSelectMany : false ,
138+ title : "Select a folder to create a new sketch in" ,
139+ defaultUri : workspace . workspaceFolders && workspace . workspaceFolders [ 0 ]
140+ ? Uri . joinPath ( workspace . workspaceFolders [ 0 ] . uri , '..' )
141+ : undefined ,
142+ } ) ;
143+ if ( ! folder || folder . length === 0 ) {
144+ return ;
145+ }
135146 const sketchPath = Uri . joinPath ( folder [ 0 ] , sketchName ) ;
136147 try {
137148 await workspace . fs . createDirectory ( sketchPath ) ;
138149 const sketchFile = Uri . joinPath ( sketchPath , `${ sketchName } .pde` ) ;
139150 await workspace . fs . writeFile ( sketchFile , new Uint8Array ( ) ) ;
140151 await commands . executeCommand ( 'processing.sketch.open' , sketchPath . fsPath ) ;
141152 window . showInformationMessage ( `New sketch created: ${ sketchName } ` ) ;
153+
154+ context . globalState . update ( 'sketchNumber' , sketchNumber + 1 ) ;
142155 } catch ( error ) {
143156 window . showErrorMessage ( `Could not create sketch: ${ error instanceof Error ? error . message : error } ` ) ;
144157 console . error ( error ) ;
145158 }
159+
146160 } ) ;
147161
148162 // TODO: Add command to select Processing version and set the setting
149163
150164 context . subscriptions . push ( runSketch , stopSketch , openSketch , newSketch ) ;
151165}
166+
167+ // Helper function to convert a number to alphabetical (e.g., 0 = a, 1 = b, ..., 25 = z, 26 = aa, etc.)
168+ function numberToAlpha ( n : number ) : string {
169+ let s = '' ;
170+ n ++ ;
171+ while ( n > 0 ) {
172+ n -- ; // Adjust for 0-indexing
173+ s = String . fromCharCode ( 97 + ( n % 26 ) ) + s ;
174+ n = Math . floor ( n / 26 ) ;
175+ }
176+ return s ;
177+ }
0 commit comments