|
| 1 | +/** |
| 2 | + Copyright 2014 Gordon Williams ([email protected]) |
| 3 | +
|
| 4 | + This Source Code is subject to the terms of the Mozilla Public |
| 5 | + License, v2.0. If a copy of the MPL was not distributed with this |
| 6 | + file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | +
|
| 8 | + ------------------------------------------------------------------ |
| 9 | + An Example Plugin |
| 10 | + ------------------------------------------------------------------ |
| 11 | +**/ |
| 12 | +"use strict"; |
| 13 | +(function(){ |
| 14 | + |
| 15 | + var icon; |
| 16 | + var log = []; |
| 17 | + var logStarted = false; |
| 18 | + |
| 19 | + function init() { |
| 20 | + Espruino.Core.Config.add("SHOW_TERMINAL_LOGGER_ICON", { |
| 21 | + section : "General", |
| 22 | + name : "Terminal Log Icon", |
| 23 | + description : "Show an icon in the terminal window that allows you to record the contents of the terminal to a file", |
| 24 | + type : "boolean", |
| 25 | + defaultValue : false, |
| 26 | + onChange : function(newValue) { showIcon(newValue); } |
| 27 | + }); |
| 28 | + showIcon(Espruino.Config.SHOW_TERMINAL_LOGGER_ICON); |
| 29 | + Espruino.addProcessor("terminalNewLine", function(line, callback) { |
| 30 | + if (logStarted) { |
| 31 | + log.push(line); |
| 32 | + icon.setInfo(getLogStateMessage()); |
| 33 | + } |
| 34 | + callback(line); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + function getLogStateMessage() { |
| 39 | + if (logStarted) { |
| 40 | + return "🔴 "+log.length; |
| 41 | + } else { |
| 42 | + if (log.length) return "⭕ "+log.length; |
| 43 | + else return "⭕ IDLE"; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + function showIcon(show) { |
| 48 | + if (icon!==undefined) { |
| 49 | + icon.remove(); |
| 50 | + icon = undefined; |
| 51 | + } |
| 52 | + if (show) { |
| 53 | + icon = Espruino.Core.App.addIcon({ |
| 54 | + id: "termLog", |
| 55 | + icon: "log", |
| 56 | + title : "Terminal Logging", |
| 57 | + //order: -90, |
| 58 | + area: { |
| 59 | + name: "terminal", |
| 60 | + position: "top" |
| 61 | + }, |
| 62 | + click: showMenu, |
| 63 | + info : getLogStateMessage() |
| 64 | + }); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + function showMenu() { |
| 69 | + var buttons = []; |
| 70 | + if (logStarted) { |
| 71 | + buttons.push({ name:"Stop", callback : function() { |
| 72 | + logStarted = false; |
| 73 | + icon.setInfo(getLogStateMessage()); |
| 74 | + popup.close(); |
| 75 | + }}); |
| 76 | + } else { |
| 77 | + buttons.push({ name:"Start", callback : function() { |
| 78 | + logStarted = true; |
| 79 | + icon.setInfo(getLogStateMessage()); |
| 80 | + popup.close(); |
| 81 | + }}); |
| 82 | + } |
| 83 | + if (log.length) { |
| 84 | + buttons.push({ name:"Save", callback : function() { |
| 85 | + Espruino.Core.Utils.fileSaveDialog(log.join("\r\n"), "log.txt", function() { |
| 86 | + icon.setInfo(getLogStateMessage()); |
| 87 | + popup.close(); |
| 88 | + }); |
| 89 | + }}); |
| 90 | + buttons.push({ name:"Clear", callback : function() { |
| 91 | + log = []; |
| 92 | + icon.setInfo(getLogStateMessage()); |
| 93 | + popup.close(); |
| 94 | + }}); |
| 95 | + } |
| 96 | + var popup = Espruino.Core.App.openPopup({ |
| 97 | + title: "Terminal Logging", |
| 98 | + padding: true, |
| 99 | + contents: `<p>Logging allows you to store the data from the terminal into a file.</p> |
| 100 | +<p>You currently have ${log.length} lines recorded</p>`, |
| 101 | + position: "center", |
| 102 | + buttons : buttons |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + Espruino.Plugins.TerminalLogger = { |
| 107 | + init : init, |
| 108 | + }; |
| 109 | +}()); |
0 commit comments