<commit_before>(function() {
  var States = {RUNNING: 0, PAUSED: 1, STOPPED: 2};
  var Hats = {WORK: 0, REST: 1};
  // Current state
  var st = {};


  var db = (function() {
    var tasks = localStorage.tasks || '["Task 1", "Task 2"]';
    tasks = JSON.parse(tasks);
    var workTimeMin = Number(localStorage.workTimeMin || '0');
    var workTimeSec = Number(localStorage.workTimeSec || '0');
    var restTimeMin = Number(localStorage.restTimeMin || '0');
    var restTimeSec = Number(localStorage.restTimeSec || '0');

    return {
      getTaskById: function(id) {
        return tasks[id] || '';
      },

      taskCount: tasks.length,

      workTime: (workTimeMin * 60 + workTimeSec) * 1000,

      restTime: (restTimeMin * 60 + restTimeSec) * 1000
    };
  })();

  var initState = function() {
    st = {
      state: States.STOPPED,
      hat: Hats.REST,
      prevElapsedTime: 0,
      curStreakStartTime: 0,
      targetTime: 0,
      taskId: -1
    };
    updateTimer(0);
    updateTask('');
  };

  var nextTask = function() {
    if (st.taskId > -1) {
      document.getElementById('beep').play();
    }

    st.prevElapsedTime = 0;
    st.curStreakStartTime = Date.now();

    if (st.hat === Hats.WORK) {

      st.hat = Hats.REST;
      updateTask('Rest');
      if (st.taskId + 1 < db.taskCount) {
        updateNextTask('Next Task: ' + db.getTaskById(st.taskId + 1));
      } else {
        updateNextTask('Next Task: ' + db.getTaskById(0));
      }
      st.targetTime = db.restTime;

    } else {

      st.hat = Hats.WORK;
      st.taskId++;
      if (st.taskId >= db.taskCount) {
        st.taskId = 0;
      }
      updateTask(db.getTaskById(st.taskId));
      updateNextTask('');

      st.targetTime = db.workTime;

    }

    tick();
  };

  var updateTimer = function(timeRemaining) {
    var secs = Math.floor(timeRemaining / 1000);
    var mins = Math.floor(secs / 60);
    secs -= mins * 60;
    minStr = mins.toFixed(0).toString();
    secStr = _.padLeft(secs.toFixed(0).toString(), 2, '0');
    $('#countdown').text(minStr + ' : ' + secStr);
  };

  var updateTask = function(task) {
    $('#task').text(task);
  };

  var updateNextTask = function(task) {
    $('#next-task').text(task);
  };

  var updateRunButton = function() {
    var iconStr = st.state === States.RUNNING ? 'pause' : 'play';
    $('#run').html('<i class="fa fa-' + iconStr + '"></i>');
  };

  var tick = function() {
    if (st.state !== States.RUNNING) return;

    var now = Date.now();
    var totalTime = st.prevElapsedTime + now - st.curStreakStartTime;
    var timeRemaining = st.targetTime - totalTime;
    if (timeRemaining < 0) timeRemaining = 0;

    if (timeRemaining === 0) nextTask();
    else updateTimer(timeRemaining);
  };

  /*** Event listeners ***/
  $('#run').click(function() {
    var btn = $('#run');
    if (st.state === States.STOPPED) {

      st.state = States.RUNNING;
      nextTask();

    } else if (st.state === States.RUNNING) {

      st.state = States.PAUSED;

    } else { // States.PAUSED

      st.state = States.RUNNING;

    }

    updateRunButton();
  });

  $('#stop').click(function() {
    initState();
    updateRunButton();
  });

  $('#edit').click(function() {
    var gui = require('nw.gui');
    var win = gui.Window.get().window;
    win.location = 'edit.html';
  });

  /*** Start here ***/
  initState();

  setInterval(tick, 200);
})();

<commit_msg>Pause and resume tasks. Closes #18<commit_after>(function() {
  var States = {RUNNING: 0, PAUSED: 1, STOPPED: 2};
  var Hats = {WORK: 0, REST: 1};
  // Current state
  var st = {};


  var db = (function() {
    var tasks = localStorage.tasks || '["Task 1", "Task 2"]';
    tasks = JSON.parse(tasks);
    var workTimeMin = Number(localStorage.workTimeMin || '0');
    var workTimeSec = Number(localStorage.workTimeSec || '0');
    var restTimeMin = Number(localStorage.restTimeMin || '0');
    var restTimeSec = Number(localStorage.restTimeSec || '0');

    return {
      getTaskById: function(id) {
        return tasks[id] || '';
      },

      taskCount: tasks.length,

      workTime: (workTimeMin * 60 + workTimeSec) * 1000,

      restTime: (restTimeMin * 60 + restTimeSec) * 1000
    };
  })();

  var initState = function() {
    st = {
      state: States.STOPPED,
      hat: Hats.REST,
      prevElapsedTime: 0,
      curStreakStartTime: 0,
      targetTime: 0,
      taskId: -1
    };
    updateTimer(0);
    updateTask('');
  };

  var nextTask = function() {
    if (st.taskId > -1) {
      document.getElementById('beep').play();
    }

    st.prevElapsedTime = 0;
    st.curStreakStartTime = Date.now();

    if (st.hat === Hats.WORK) {

      st.hat = Hats.REST;
      updateTask('Rest');
      if (st.taskId + 1 < db.taskCount) {
        updateNextTask('Next Task: ' + db.getTaskById(st.taskId + 1));
      } else {
        updateNextTask('Next Task: ' + db.getTaskById(0));
      }
      st.targetTime = db.restTime;

    } else {

      st.hat = Hats.WORK;
      st.taskId++;
      if (st.taskId >= db.taskCount) {
        st.taskId = 0;
      }
      updateTask(db.getTaskById(st.taskId));
      updateNextTask('');

      st.targetTime = db.workTime;

    }

    tick();
  };

  var pauseTask = function() {
    st.state = States.PAUSED;
    st.prevElapsedTime += Date.now() - st.curStreakStartTime;
  };

  var resumeTask = function() {
    st.state = States.RUNNING;
    st.curStreakStartTime = Date.now();
  };

  var updateTimer = function(timeRemaining) {
    var secs = Math.floor(timeRemaining / 1000);
    var mins = Math.floor(secs / 60);
    secs -= mins * 60;
    minStr = mins.toFixed(0).toString();
    secStr = _.padLeft(secs.toFixed(0).toString(), 2, '0');
    $('#countdown').text(minStr + ' : ' + secStr);
  };

  var updateTask = function(task) {
    $('#task').text(task);
  };

  var updateNextTask = function(task) {
    $('#next-task').text(task);
  };

  var updateRunButton = function() {
    var iconStr = st.state === States.RUNNING ? 'pause' : 'play';
    $('#run').html('<i class="fa fa-' + iconStr + '"></i>');
  };

  var tick = function() {
    if (st.state !== States.RUNNING) return;

    var now = Date.now();
    var totalTime = st.prevElapsedTime + now - st.curStreakStartTime;
    var timeRemaining = st.targetTime - totalTime;
    if (timeRemaining < 0) timeRemaining = 0;

    if (timeRemaining === 0) nextTask();
    else updateTimer(timeRemaining);
  };

  /*** Event listeners ***/
  $('#run').click(function() {
    var btn = $('#run');
    if (st.state === States.STOPPED) {

      st.state = States.RUNNING;
      nextTask();

    } else if (st.state === States.RUNNING) {

      pauseTask();

    } else { // States.PAUSED

      resumeTask();

    }

    updateRunButton();
  });

  $('#stop').click(function() {
    initState();
    updateRunButton();
  });

  $('#edit').click(function() {
    var gui = require('nw.gui');
    var win = gui.Window.get().window;
    win.location = 'edit.html';
  });

  /*** Start here ***/
  initState();

  setInterval(tick, 200);
})();


{'fraction_non_alphanumeric': 0.17783760804874593, 'fraction_numerical': 0.015020546974635115, 'mean_word_length': 2.530765382691346, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '269675', 'n_tokens_mistral': 2561, 'n_tokens_neox': 2417, 'n_words': 500}