<commit_before>/*
Customization and selection of the build command.

*/

import { List } from "immutable";

const { Loading } = require("smc-webapp/r_misc");

import {
  Alert,
  MenuItem,
  DropdownButton,
  ButtonToolbar,
  FormControl
} from "react-bootstrap";

import { React, Rendered, Component } from "../../app-framework";

import { split } from "../generic/misc";

import { Engine, build_command } from "./latexmk";

const ENGINES: Engine[] = ["PDFLaTeX", "XeLaTeX", "LuaTex"];

interface Props {
  actions: any;
  filename: string;
  build_command: string | List<string>;
  knitr: boolean;
}

interface State {
  build_command: string;
  focus: boolean;
}

export class BuildCommand extends Component<Props, State> {
  constructor(props) {
    super(props);
    this.state = {
      build_command: this.build_command_string(props.build_command),
      focus: false,
    };
  }

  componentWillReceiveProps(next: Props): void {
    if (next.build_command != this.props.build_command) {
      // set by another user or menu selection.
      this.setState({
        build_command: this.build_command_string(next.build_command)
      });
    }
  }

  build_command_string(cmd: string | List<string>): string {
    let s: string;
    if (typeof cmd === "string") {
      s = cmd;
    } else {
      let v: string[] = [];
      cmd.forEach(function(t: string) {
        if (split(t).length > 1) {
          // some minimal escape for now...
          if (t.indexOf("'") === -1) {
            t = `'${t}'`;
          } else {
            t = `"${t}"`;
          }
        }
        v.push(t);
      });
      s = v.join(" ");
    }
    return s;
  }

  select_engine(engine: Engine): void {
    this.props.actions.set_build_command(
      build_command(engine, this.props.filename, this.props.knitr)
    );
  }

  render_item(engine: string): Rendered {
    return (
      <MenuItem
        key={engine}
        eventKey={engine}
        onSelect={engine => this.select_engine(engine)}
      >
        {engine}
      </MenuItem>
    );
  }

  render_items(): Rendered[] {
    const v: Rendered[] = [];
    for (let engine of ENGINES) {
      v.push(this.render_item(engine));
    }
    return v;
  }

  render_dropdown(): Rendered {
    return (
      <ButtonToolbar>
        <DropdownButton title="Engine" id="cc-latex-build-command" pullRight>
          {this.render_items()}
        </DropdownButton>
      </ButtonToolbar>
    );
  }

  handle_command_line_change(val: string): void {
    this.setState({ build_command: val });
  }

  handle_build_change(): void {
    if (
      this.state.build_command !=
      this.build_command_string(this.props.build_command)
    ) {
      this.props.actions.set_build_command(this.state.build_command);
    }
  }

  render_command_line(): Rendered {
    return (
      <FormControl<commit_msg>use syncdb file and store correctly<commit_after>/*
Customization and selection of the build command.

*/

import { List, fromJS } from "immutable";

const { Loading } = require("smc-webapp/r_misc");

import {
  Alert,
  MenuItem,
  DropdownButton,
  ButtonToolbar,
  FormControl
} from "react-bootstrap";

import { React, Rendered, Component } from "../../app-framework";

import { split } from "../generic/misc";

import { Engine, build_command } from "./latexmk";

const ENGINES: Engine[] = ["PDFLaTeX", "XeLaTeX", "LuaTex"];

interface Props {
  actions: any;
  filename: string;
  build_command: string | List<string>;
  knitr: boolean;
}

interface State {
  build_command: string;
  focus: boolean;
}

export class BuildCommand extends Component<Props, State> {
  constructor(props) {
    super(props);
    this.state = {
      build_command: this.build_command_string(props.build_command),
      focus: false
    };
  }

  componentWillReceiveProps(next: Props): void {
    if (this.state.focus) {
      return;
    }
    if (next.build_command != this.props.build_command) {
      // set by another user or menu selection.
      this.setState({
        build_command: this.build_command_string(next.build_command)
      });
    }
  }

  build_command_string(cmd: string | List<string>): string {
    let s: string;
    if (typeof cmd === "string") {
      s = cmd;
    } else {
      let v: string[] = [];
      cmd.forEach(function(t: string) {
        if (split(t).length > 1) {
          // some minimal escape for now...
          if (t.indexOf("'") === -1) {
            t = `'${t}'`;
          } else {
            t = `"${t}"`;
          }
        }
        v.push(t);
      });
      s = v.join(" ");
    }
    return s;
  }

  select_engine(engine: Engine): void {
    const cmd: string[] = build_command(
      engine,
      this.props.filename,
      this.props.knitr
    );
    this.props.actions.set_build_command(cmd);
    this.setState({ build_command: this.build_command_string(fromJS(cmd)) });
  }

  render_item(engine: string): Rendered {
    return (
      <MenuItem
        key={engine}
        eventKey={engine}
        onSelect={engine => this.select_engine(engine)}
      >
        {engine}
      </MenuItem>
    );
  }

  render_items(): Rendered[] {
    const v: Rendered[] = [];
    for (let engine of ENGINES) {
      v.push(this.render_item(engine));
    }
    return v;
  }

  render_dropdown(): Rendered {
    return (
      <ButtonToolbar>
        <DropdownButton title="Engine" id="cc-latex-build-command" pullRight>
          {this.render_items()}
        </DropdownButton>
      </ButtonToolbar>
    );
  }

  handle_command_line_change(val: string): void {
    this.setState({ build_command: val });
  }

  handle_build_change(): void {
    if (
      this.state.build_command !=
      this.build_command_string(this.props.build_command)
    ) {
      if (!this.state.build_command) {
        this.select_engine(ENGINES[0]);
      } else {
        this.props.actions.set_build_command(this.state.build_command);
      }
    }
  }

  render_command_line(): Rendered {
    return (
      <FormControl
{'fraction_non_alphanumeric': 0.14120989304812834, 'fraction_numerical': 0.0008355614973262032, 'mean_word_length': 2.495911214953271, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, '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': '2494371', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1938, 'n_words': 395}