aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/BrowserApp.java
blob: 36f200544c37937a5acff8ccdd9ef9a6f916a920 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package ui;

import model.html.*;

import java.nio.file.*;
import java.util.*;

/**
 * The console interface to Apus.
 */
public class BrowserApp {
    private Scanner input;
    private static final String border = "===============================================";
    private String pathString;
    private ArrayList<Node> parsed;
    private ArrayDeque<String> tabs;

    // Renders an arbitrary HTML page and arbitrary HTML input.
    public BrowserApp() {
        println("apus: currently a barebones html/css renderer");
        this.input = new Scanner(System.in);
        this.tabs = new ArrayDeque<>();

        while (true) {
            try {
                String file = Files.readString(Path.of(pathString));
                HtmlParser parser = new HtmlParser();
                parsed = parser.parseHtml(file);
                println(border);
                renderHtml(parsed);
                println(border);
                println("Page rendered. Input additional commands if desired.");
                println("Impemented commands: newuri, newtab, nexttab, quit");
                handleInput(this.input.next());
                println(border);
            } catch (Exception e) {
                println("Reading from the file failed with " + e.toString());
                println("Please try again.");
            }
        }
    }

    // Barebones HTML rendering. Iterates through a list of Nodes and their children and prints any text.
    private void renderHtml(ArrayList<Node> html) {
        for (Node node: html) {
            switch (node) {
                case ElementNode e -> {
                    renderHtml(e.getChildren());
                }
                default -> {
                    println(node.getData());
                }
            }
        }
    }

    // Handles user input after rendering an initial site
    private void handleInput(String input) {
        switch (input) {
            case "newuri" -> {
                println("please provide a path to a file (examples located in data/*):");
                pathString = this.input.next();
            }
            case "newtab" -> {
                this.tabs.add(pathString);
                println("please provide a path to a file (examples located in data/*):");
                pathString = this.input.next();
            }
            case "nexttab" -> {
                this.tabs.add(pathString);
                pathString = this.tabs.removeFirst();
            }
            case "quit" -> System.exit(0);
            default -> println("Sorry, I didn't quite get that. Please try again.");
        }
    }

    private void print(String toPrint) {
        System.out.print(toPrint);
    }

    private void println(String toPrint) {
        System.out.println(toPrint);
    }

}