<commit_before>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import {
  PanelLayout
} from 'phosphor-panel';

import {
  Widget
} from 'phosphor-widget';

import {
  NotebookToolbar, ToolbarButton
} from '../notebook/notebook/toolbar';


/**
 * The class name added to inspector widgets.
 */
const INSPECTOR_CLASS = 'jp-ConsoleInspector';

/**
 * The back button class name.
 */
const BACK_CLASS = 'jp-ConsoleInspector-back';

/**
 * The forward button class name.
 */
const FORWARD_CLASS = 'jp-ConsoleInspector-forward';


/**
 * An inspector widget for a console.
 */
export
class ConsoleInspector extends Widget {
  /**
   * Construct a console inspector widget.
   */
  constructor() {
    super();
    this.addClass(INSPECTOR_CLASS);
    this.layout = new PanelLayout();
  }

  /**
   * The text of the inspector.
   */
  get content(): Widget {
    return this._content;
  }
  set content(newValue: Widget) {
    if (newValue === this._content) {
      return;
    }
    if (this._content) {
      if (this._remember) {
        this._content.hide();
      } else {
        this._content.dispose();
      }
    }
    this._content = newValue;
    if (this._content) {
      let layout = this.layout as PanelLayout;
      layout.addChild(this._content);
      if (this.remember) {
        this._history.push(newValue);
        this._index++;
      }
    }
  }

  /**
   * A flag that indicates whether the inspector remembers history.
   */
  get remember(): boolean {
    return this._remember;
  }
  set remember(newValue: boolean) {
    if (newValue === this._remember) {
      return;
    }
    this._remember = newValue;
    if (!this._remember) {
      this._toolbar.dispose();
      this._history.forEach(widget => widget.dispose());
      this._history = null;
    } else {
      this._toolbar = this._createToolbar();
      this._history = [];
      this._index = -1;
      (this.layout as PanelLayout).insertChild(0, this._toolbar);
    }
  }

  /**
   * The display rank of the inspector.
   */
  get rank(): number {
    return this._rank;
  }
  set rank(newValue: number) {
    this._rank = newValue;
  }

  /**
   * Dispose of the resources held by the widget.
   */
  dispose(): void {
    if (this.isDisposed) {
      return;
    }

    if (this._history) {
      this._history.forEach(widget => widget.dispose());
      this._history = null;
    }

    super.dispose();
  }

  /**
   * Create a history toolbar.
   */
  private _createToolbar(): NotebookToolbar {
    let toolbar = new NotebookToolbar();
    let back = new ToolbarButton({
      className: BACK_CLASS,
      onClick: () => {
        this._index = Math.max(this._index - 1, 0);
        this._content.hide();
        this._content = this._history[this._index];
        this._content.show();
      },
      tooltip: 'Navigate back in history'
    });
    toolbar.add('back', back);

    let forward = new ToolbarButton({
      className: FORWARD_CLASS,
      onClick: () => {
        this._index = Math.min(this._index + 1, this._history.length - 1);
        this._content.hide();
        this._content = this._history[this._index];
        this._content.show();
      },
      tooltip: 'Navigate forward in history'
    });
    toolbar.add('forward', forward);

    return toolbar;
  }

  private _content: Widget = null;
  private _history: Widget[] = null;
  private _index: number = -1;
  private _rank: number = Infinity;<commit_msg>Clean up history navigation of ConsoleInspector.<commit_after>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import {
  PanelLayout
} from 'phosphor-panel';

import {
  Widget
} from 'phosphor-widget';

import {
  NotebookToolbar, ToolbarButton
} from '../notebook/notebook/toolbar';


/**
 * The class name added to inspector widgets.
 */
const INSPECTOR_CLASS = 'jp-ConsoleInspector';

/**
 * The back button class name.
 */
const BACK_CLASS = 'jp-ConsoleInspector-back';

/**
 * The forward button class name.
 */
const FORWARD_CLASS = 'jp-ConsoleInspector-forward';


/**
 * An inspector widget for a console.
 */
export
class ConsoleInspector extends Widget {
  /**
   * Construct a console inspector widget.
   */
  constructor() {
    super();
    this.addClass(INSPECTOR_CLASS);
    this.layout = new PanelLayout();
  }

  /**
   * The text of the inspector.
   */
  get content(): Widget {
    return this._content;
  }
  set content(newValue: Widget) {
    if (newValue === this._content) {
      return;
    }
    if (this._content) {
      if (this._remember) {
        this._content.hide();
      } else {
        this._content.dispose();
      }
    }
    this._content = newValue;
    if (this._content) {
      let layout = this.layout as PanelLayout;
      layout.addChild(this._content);
      if (this.remember) {
        this._history.push(newValue);
        this._index++;
      }
    }
  }

  /**
   * A flag that indicates whether the inspector remembers history.
   */
  get remember(): boolean {
    return this._remember;
  }
  set remember(newValue: boolean) {
    if (newValue === this._remember) {
      return;
    }
    this._remember = newValue;
    if (!this._remember) {
      this._toolbar.dispose();
      this._history.forEach(widget => widget.dispose());
      this._history = null;
    } else {
      this._toolbar = this._createToolbar();
      this._history = [];
      this._index = -1;
      (this.layout as PanelLayout).insertChild(0, this._toolbar);
    }
  }

  /**
   * The display rank of the inspector.
   */
  get rank(): number {
    return this._rank;
  }
  set rank(newValue: number) {
    this._rank = newValue;
  }

  /**
   * Dispose of the resources held by the widget.
   */
  dispose(): void {
    if (this.isDisposed) {
      return;
    }

    if (this._history) {
      this._history.forEach(widget => widget.dispose());
      this._history = null;
    }

    super.dispose();
  }

  /**
   * Navigate back in history.
   */
  private _back(): void {
    if (this._history.length) {
      this._navigateTo(Math.max(this._index - 1, 0));
    }
  }

  /**
   * Navigate forward in history.
   */
  private _forward(): void {
    if (this._history.length) {
      this._navigateTo(Math.min(this._index + 1, this._history.length - 1));
    }
  }

  /**
   * Create a history toolbar.
   */
  private _createToolbar(): NotebookToolbar {
    let toolbar = new NotebookToolbar();
    let back = new ToolbarButton({
      className: BACK_CLASS,
      onClick: () => this._back(),
      tooltip: 'Navigate back in history'
    });
    toolbar.add('back', back);

    let forward = new ToolbarButton({
      className: FORWARD_CLASS,
      onClick: () => this._forward(),
      tooltip: 'Navigate forward in history'
    });
    toolbar.add('forward', forward);

    return toolbar;
  }

  /**
   * Navigate to a known index in history.
   */
  private _navigateTo(index: number): void {
    if (this._content) {
      this._content.hide();
    }
    this._content = this._history[index];
    this._index = index;
    this._content.show();
  }

  private _content: Widget = null;
  private _history: Widget[] = null;
  private _index: number = -1;
  private _rank: number = Infinity;
{'fraction_non_alphanumeric': 0.13391997800082497, 'fraction_numerical': 0.0019249278152069298, 'mean_word_length': 2.4392434988179668, 'pattern_counts': {'":': 0, '<': 3, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5491268', 'n_tokens_mistral': 2306, 'n_tokens_neox': 2229, 'n_words': 561}