draw method

  1. @override
void draw()
override

Fonction chargée de l'affichage sur le terminal

Implementation

@override
void draw() {
  displayedElements = 0;
  int width = console.windowWidth;
  int availableLines = console.windowHeight;

  int startIndex = (displayedSubList)*MAX_DISPLAY_LINES;
  int endIndex = (displayedSubList+1)*(MAX_DISPLAY_LINES);
  int displayEndIndex = endIndex < options.length ? endIndex : options.length;
  console.clearScreen();
  console.setForegroundColor(ConsoleColor.cyan);
  String titleText = '=== Menu Principal ===';
  String footerText = 'n : Ajouter un cours   |   e : Éditer les cours   |   r : Rechercher \ni: Importer les cours depuis Celene | o : Options | esc : Quitter';
  console.writeLine(titleText);
  availableLines -= (titleText.getLineUsed(width) + footerText.getLineUsed(width) +2);
  console.resetColorAttributes();
  for (int i = startIndex; i < displayEndIndex; i++) {
    String lineText = i == selectedIndex ? '> ${options[i]}': '  ${options[i]}';
    if (i == selectedIndex) {
      console.setForegroundColor(ConsoleColor.black);
      console.setBackgroundColor(ConsoleColor.yellow);
      console.writeLine(lineText);
      console.resetColorAttributes();
    } else {
      console.writeLine(lineText);
    }
    availableLines -= lineText.getLineUsed(width);
    displayedElements++;
    if (availableLines == 0){
      // No more lines available to display text on terminal, so change displayEndIndex to the last line printed on screen to finish the loop
      displayEndIndex = i;
    }
  }
  for (int i = 0; i < availableLines; i++) {
    console.writeLine('');
  }
  console.setForegroundColor(ConsoleColor.brightCyan);
  console.writeLine('─' * console.windowWidth); // ligne de séparation
  console.setForegroundColor(ConsoleColor.brightCyan);
  console.writeLine(footerText);
  console.resetColorAttributes();
}