Drafts Action Directory

stopwatch

Posted by dchar, Last updated: 2018-07-04 14:32:46 UTC

A simple stopwatch action.

Run the action to start and again at each lap.

Keep notes and comments in between laps.

13:01:02 >> Start

13:10:07 >> Lap 1  09:05 / 09:05

13:19:40 >> Lap 2  09:33 / 18:38

13:29:10 >> Lap 3  09:30 / 28:08

13:30:55 >> Lap 4  01:45 / 29:53
Install

Steps

  • script

    // simple drafts stopwatch
    
    class Stopwatch {
        constructor() {
            this.iLap = 0;
            this.tStart = 0;
            this.tLast = 0;
        }
    
        lap(s) {
            let lines = s.split('\n');
            if ((lines.length == 1) && lines[0].length == 0) {
                lines.pop();
            }
    
            lines.push(this.timestamp());
    
            return lines.map(this.processLine, this).join('\n');
        }
    
        elpasedTime(t) {
            return this.toHMS(t - this.tStart);
        }
    
        lapTime(t) {
            return this.toHMS(t - this.tLast);
        }
    
        parseTimestamp(s) {
            let m = s.match(/^\s*(\d{2}):(\d{2}):(\d{2})/);
            return m ? ((+m[1])*60*60 + (+m[2])*60 + (+m[3])) : 0; 
        }
    
        processLine(l) {
            let t = this.parseTimestamp(l);
            if (t == 0) {
                return l;
            }
    
            l = l.split(' >> ')[0] + ' >> ';
            if (this.iLap) {
                l += 'Lap ' + this.iLap + ((this.iLap < 10) ? '  ' : ' ');
                l += this.lapTime(t) + ' / ' + this.elpasedTime(t);
            } else {
                l += 'Start';
                this.tStart = t;
            }
    
            this.iLap++;
            this.tLast = t;
            return l;        
        }
    
        timestamp() {
            return (new Date()).toString('HH:mm:ss');
        }
    
        toHMS(t) {
            return Date.today().addSeconds(t).toString('H:mm:ss').replace(/^0:/, '');
        }
    }
    
    const text = (new Stopwatch()).lap(editor.getText());
    editor.setText(text)
    

Options

  • After Success Nothing
    Notification None
    Log Level Info

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.