history.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. Kernel, KernelMessage
  5. } from '@jupyterlab/services';
  6. import {
  7. Vector
  8. } from 'phosphor/lib/collections/vector';
  9. import {
  10. IDisposable
  11. } from 'phosphor/lib/core/disposable';
  12. import {
  13. clearSignalData
  14. } from 'phosphor/lib/core/signaling';
  15. /**
  16. * The definition of a console history manager object.
  17. */
  18. export
  19. interface IConsoleHistory extends IDisposable {
  20. /**
  21. * The current kernel supplying navigation history.
  22. */
  23. kernel: Kernel.IKernel;
  24. /**
  25. * The placeholder text that a history session began with.
  26. */
  27. readonly placeholder: string;
  28. /**
  29. * Get the previous item in the console history.
  30. *
  31. * @param placeholder - The placeholder string that gets temporarily added
  32. * to the history only for the duration of one history session. If multiple
  33. * placeholders are sent within a session, only the first one is accepted.
  34. *
  35. * @returns A Promise for console command text or `undefined` if unavailable.
  36. */
  37. back(placeholder: string): Promise<string>;
  38. /**
  39. * Get the next item in the console history.
  40. *
  41. * @param placeholder - The placeholder string that gets temporarily added
  42. * to the history only for the duration of one history session. If multiple
  43. * placeholders are sent within a session, only the first one is accepted.
  44. *
  45. * @returns A Promise for console command text or `undefined` if unavailable.
  46. */
  47. forward(placeholder: string): Promise<string>;
  48. /**
  49. * Add a new item to the bottom of history.
  50. *
  51. * @param item The item being added to the bottom of history.
  52. *
  53. * #### Notes
  54. * If the item being added is undefined or empty, it is ignored. If the item
  55. * being added is the same as the last item in history, it is ignored as well
  56. * so that the console's history will consist of no contiguous repetitions.
  57. */
  58. push(item: string): void;
  59. /**
  60. * Reset the history navigation state, i.e., start a new history session.
  61. */
  62. reset(): void;
  63. }
  64. /**
  65. * A console history manager object.
  66. */
  67. export
  68. class ConsoleHistory implements IConsoleHistory {
  69. /**
  70. * Construct a new console history object.
  71. */
  72. constructor(options?: ConsoleHistory.IOptions) {
  73. this._history = new Vector<string>();
  74. if (options && options.kernel) {
  75. this.kernel = options.kernel;
  76. }
  77. }
  78. /**
  79. * Get whether the console history manager is disposed.
  80. */
  81. get isDisposed(): boolean {
  82. return this._history === null;
  83. }
  84. /**
  85. * The current kernel supplying navigation history.
  86. */
  87. get kernel(): Kernel.IKernel {
  88. return this._kernel;
  89. }
  90. set kernel(newValue: Kernel.IKernel) {
  91. if (newValue === this._kernel) {
  92. return;
  93. }
  94. this._kernel = newValue;
  95. if (!this._kernel) {
  96. this._history = new Vector<string>();
  97. return;
  98. }
  99. this._kernel.requestHistory(Private.initialRequest).then(v => {
  100. this.onHistory(v);
  101. });
  102. }
  103. /**
  104. * The placeholder text that a history session began with.
  105. */
  106. get placeholder(): string {
  107. return this._placeholder;
  108. }
  109. /**
  110. * Dispose of the resources held by the console history manager.
  111. */
  112. dispose(): void {
  113. if (this.isDisposed) {
  114. return;
  115. }
  116. clearSignalData(this);
  117. this._history = null;
  118. }
  119. /**
  120. * Get the previous item in the console history.
  121. *
  122. * @param placeholder - The placeholder string that gets temporarily added
  123. * to the history only for the duration of one history session. If multiple
  124. * placeholders are sent within a session, only the first one is accepted.
  125. *
  126. * @returns A Promise for console command text or `undefined` if unavailable.
  127. */
  128. back(placeholder: string): Promise<string> {
  129. if (!this._hasSession) {
  130. this._hasSession = true;
  131. this._placeholder = placeholder;
  132. }
  133. let content = this._history.at(--this._cursor);
  134. this._cursor = Math.max(0, this._cursor);
  135. return Promise.resolve(content);
  136. }
  137. /**
  138. * Get the next item in the console history.
  139. *
  140. * @param placeholder - The placeholder string that gets temporarily added
  141. * to the history only for the duration of one history session. If multiple
  142. * placeholders are sent within a session, only the first one is accepted.
  143. *
  144. * @returns A Promise for console command text or `undefined` if unavailable.
  145. */
  146. forward(placeholder: string): Promise<string> {
  147. if (!this._hasSession) {
  148. this._hasSession = true;
  149. this._placeholder = placeholder;
  150. }
  151. let content = this._history.at(++this._cursor);
  152. this._cursor = Math.min(this._history.length, this._cursor);
  153. return Promise.resolve(content);
  154. }
  155. /**
  156. * Add a new item to the bottom of history.
  157. *
  158. * @param item The item being added to the bottom of history.
  159. *
  160. * #### Notes
  161. * If the item being added is undefined or empty, it is ignored. If the item
  162. * being added is the same as the last item in history, it is ignored as well
  163. * so that the console's history will consist of no contiguous repetitions.
  164. */
  165. push(item: string): void {
  166. if (item && item !== this._history.back) {
  167. this._history.pushBack(item);
  168. }
  169. this.reset();
  170. }
  171. /**
  172. * Reset the history navigation state, i.e., start a new history session.
  173. */
  174. reset(): void {
  175. this._cursor = this._history.length;
  176. this._hasSession = false;
  177. this._placeholder = '';
  178. }
  179. /**
  180. * Populate the history collection on history reply from a kernel.
  181. *
  182. * @param value The kernel message history reply.
  183. *
  184. * #### Notes
  185. * History entries have the shape:
  186. * [session: number, line: number, input: string]
  187. * Contiguous duplicates are stripped out of the API response.
  188. */
  189. protected onHistory(value: KernelMessage.IHistoryReplyMsg): void {
  190. this._history = new Vector<string>();
  191. let last = '';
  192. let current = '';
  193. for (let i = 0; i < value.content.history.length; i++) {
  194. current = (value.content.history[i] as string[])[2];
  195. if (current !== last) {
  196. this._history.pushBack(last = current);
  197. }
  198. }
  199. // Reset the history navigation cursor back to the bottom.
  200. this._cursor = this._history.length;
  201. }
  202. private _cursor = 0;
  203. private _hasSession = false;
  204. private _history: Vector<string> = null;
  205. private _kernel: Kernel.IKernel = null;
  206. private _placeholder: string = '';
  207. }
  208. /**
  209. * A namespace for ConsoleHistory statics.
  210. */
  211. export
  212. namespace ConsoleHistory {
  213. /**
  214. * The initialization options for a console history object.
  215. */
  216. export
  217. interface IOptions {
  218. /**
  219. * The kernel instance to query for history.
  220. */
  221. kernel?: Kernel.IKernel;
  222. }
  223. }
  224. /**
  225. * A namespace for private data.
  226. */
  227. namespace Private {
  228. export
  229. const initialRequest: KernelMessage.IHistoryRequest = {
  230. output: false,
  231. raw: true,
  232. hist_access_type: 'tail',
  233. n: 500
  234. };
  235. }