Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • Njal.Nordal/lab-3
  • elias.m.johansen/lab-3
  • David.Mo/lab-3
  • Henrik.Tennebekk/lab-3
  • Kjetil.Hole/lab-3
  • theodor.andreasen/lab-3
  • Julie.Hallan/lab-3
  • Leon.Dammann/lab-3-inf-101
  • K.Inci/lab-3
  • Oscar.Stromsli/inf-101-lab-3
  • ii/inf101/23v/students/lab-3
  • Jan.Brunner/lab-3-2023
  • Benjamin.Fossbakken/lab-3
  • eivind.hjertholm/lab-3
  • Jacob.Grahm-Haga/lab-3
  • Eric.Bogevik/lab-3-new
  • Eskil.Yttri/lab-3-v-23
  • markus.trohjell/lab-3
  • Henrik.Dalsto/lab-3
  • Dag.Himle/lab-3
  • Olav.Eikanger/lab-3
  • Qingle.Xu/lab-3
  • Axel.Lundeby/lab-3
  • Theodor.Nissen-Meyer/lab-3
  • Jonathan.Christensen/lab-3
  • Yisahak.Sahilu/lab-3-2023
  • Kai.Waloen/lab-3
  • Viktor.Yordanov/lab-3
  • Magne.Qvarme/lab-3
  • Fernando.Aguilar/lab-3
  • R.Bortne/lab-3
  • Elias.Aronsen/lab-3-23-v
  • Halfdan.Hesthammer/lab-3-inf-101-v-23
  • ingmar.forsberg/lab-3
  • Julie.Mikkelsen/lab-3-jm
  • Natanem.Hailu/lab-3
  • Emma.Wergedahl/lab-3
  • Oscar.Larssen/lab-3
  • Martin.Fausa/lab-3
  • Mathias.Handeland/lab-3
  • Lauritz.Angeltveit/lab-3
  • V.Larsen/lab-3
  • Thomas.T.Jensen/lab-3
  • Stian.Bekkeheien/lab-3-inf-101
  • Kristian.Roynestad/lab-3
  • Cecilie.Efford/lab-3023
  • Olav.Skogen/lab-3
  • Havard.Kolve/lab-3
  • Per.Lande/lab-3
  • Idiris.Elmi/lab-3
  • Espen.Svasand/lab-3
  • ole-andreas.jensen/lab-3
  • Cecilie.Tveter/lab-3-inf-101-v-23
  • Marius.Jorgensen/lab-3
  • Stine.Fanebust/lab-3
  • Henrik.Skjeret/lab-3-v-23
56 results
Show changes
Commits on Source (15)
This diff is collapsed.
package no.uib.inf101.terminal;
// UiB INF101 ShellLab - CommandLineInterface.java
//
// Grensesnittet CommanLineInterface beskriver metoder for input og
// output til et program som virker med et tekstbasert grensesnitt.
// Du trenger ikke gjøre noen endringer i denne filen for denne lab'en.
/**
* A command line interface is a program with a text-based user interface.
* The user can enter keys as input, and the program will respond by giving
......@@ -10,13 +16,14 @@ public interface CommandLineInterface {
/**
* Called when a key is pressed.
*
* @param key The key that was pressed
* @param key the key that was pressed
*/
void keyPressed(char key);
/**
* Get the text to display on the screen.
* @return The text to display
*
* @return the text to display
*/
String getScreenContent();
}
package no.uib.inf101.terminal;
// UiB INF101 ShellLab - Context.java
//
// Denne filen inneholder kode som navigerer et filsystem. Du trenger
// ikke gjøre noen endringer i denne filen for denne lab'en (med mindre
// du selv ønsker).
import java.io.File;
import java.io.IOException;
import java.util.Objects;
public class Context {
////////////////////////
// Instance variables
////////////////////////
private final File home;
private File cwd;
////////////////////////
// Constructors
////////////////////////
/**
* Create a new context with the java current working directory as
* home.
*/
public Context() {
this(new File(System.getProperty("user.dir")));
}
/**
* Create a new context with the given directory as home.
*
* @param home the home directory.
*/
public Context(File home) {
try {
this.home = home.getCanonicalFile();
......@@ -18,27 +44,50 @@ public class Context {
}
}
////////////////////////
// Instance methods
////////////////////////
/**
* Go to the given path, if it exists. The path can be relative to
* the current working directory, or absolute.
*
* @param path the path to go to.
* @return true if the path exists, false otherwise
*/
public boolean goToPath(String path) {
File newDir = new File(cwd, path);
File newDir = new File(this.cwd, path);
if (!newDir.isDirectory()) {
return false;
}
try {
cwd = newDir.getCanonicalFile();
this.cwd = newDir.getCanonicalFile();
return true;
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}
/**
* Get the current working directory.
*
* @return the current working directory.
*/
public File getCwd() {
return this.cwd;
}
/** Set the current working directory to the home directory.*/
public void goToHome() {
this.cwd = this.home;
}
/**
* Check if the current working directory is the home directory.
*
* @return true if the current working directory is the home
* directory, false otherwise.
*/
public boolean isAtHome() {
return Objects.equals(this.cwd, this.home);
}
......
package no.uib.inf101.terminal;
// UiB INF101 ShellLab - DynamicShell.java
//
// Denne koden er gitt som et eksempel på en klasse som implementerer
// CommandLineInterface. Du trenger ikke gjøre noen endringer i denne
// filen for denne lab'en (med mindre du selv ønsker).
public class DummyShell implements CommandLineInterface {
private String screenContent = "$ ";
@Override
public void keyPressed(char keyCode) {
if (keyCode == '\n') {
public void keyPressed(char key) {
if (key == '\n') {
screenContent += "\n$ ";
} else {
screenContent += keyCode;
screenContent += key;
}
}
......
package no.uib.inf101.terminal;
// UiB INF101 ShellLab - EchoShell.java
//
// Denne koden er gitt som et eksempel på en klasse som implementerer
// CommanLineInterface. Du trenger ikke gjøre noen endringer i denne
// filen for denne lab'en (med mindre du selv ønsker).
import java.util.ArrayList;
public class EchoShell implements CommandLineInterface {
......@@ -7,13 +13,13 @@ public class EchoShell implements CommandLineInterface {
String currentLine = "";
@Override
public void keyPressed(char keyCode) {
if (keyCode == '\n') {
public void keyPressed(char key) {
if (key == '\n') {
outputLines.add("$ " + currentLine);
outputLines.add("Oh, an echo! listen: " + currentLine);
currentLine = "";
} else {
currentLine += keyCode;
currentLine += key;
}
}
......
package no.uib.inf101.terminal;
// UiB INF101 ShellLab - Main.java
// Dette er filen som inneholder main-metoden.
public class Main {
public static void main(String[] args) {
......
package no.uib.inf101.terminal;
// UiB INF101 ShellLab - SimpleShell.java
//
// Dette er klassen vi skal forbedre i denne lab'en. Slik koden er
// allerede før du begynner på laben, støtter den tre kommandoer:
//
// - cd: Bytt til en annen mappe
// - ls: List opp filer i mappen
// - pwd: Vis sti til nåværende mappe
//
// Vi skal endre denne klassen slik at den
// - kan vises av Terminal.java
// - kan støtte ubegrenset mange kommandoer
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
// TODO: Let SimpleShell implement CommandLineInterface
// TODO 1: Let SimpleShell implement CommandLineInterface
public class SimpleShell {
//////////////////////////////////////////////////////////////////////
......@@ -14,24 +28,24 @@ public class SimpleShell {
/** The prompt to show before each command */
private final String prompt = "$ ";
/** The context variable contains cwd and home directories etc. */
private final Context context = new Context(
new File(System.getProperty("user.dir"))
);
private final Context context = new Context();
/** A list of historic commands and their outputs */
private final ArrayList<String> history = new ArrayList<>();
private final List<String> history = new ArrayList<>();
/** The command currently being typed */
private String currentCommand = "";
// TODO 4: Create instance variable storing Command objects
//////////////////////////////////////////////////////////////////////
/// Public methods //////
/// Public instance methods //////
/// (methods expected to be used by someone outside this class) //////
//////////////////////////////////////////////////////////////////////
/** Constructor for SimpleShell */
public SimpleShell() {
// TODO: Install core commands SimpleShell supports (pwd, ls, cd)
// TODO 7-8-9: Install core commands SimpleShell supports (pwd, ls, cd)
}
// TODO: rename method to fit new interface, annotate with @Override
// TODO 2: rename method to fit new interface, annotate with @Override
// Note: methods with @Override generally do not need javadoc comments,
// since the javadoc comment is inherited. You should hence remove the
// javadoc comment here unless there is something special about this
......@@ -46,10 +60,12 @@ public class SimpleShell {
this.processCurrentCommandLine();
} else if (key >= ' ' && key <= '~') {
this.currentCommand += key;
} else {
// Some special key was pressed (e.g. shift, ctrl), we ignore it
}
}
// TODO: rename method to fit new interface, annotate with @Override
// TODO 3: rename method to fit new interface, annotate with @Override
/**
* Get the text which the terminal should display
*
......@@ -65,6 +81,8 @@ public class SimpleShell {
return s;
}
// TODO 5: Add method to install a command
//////////////////////////////////////////////////////////////////////
/// Private methods ///////////////////
/// (helper methods used internally in this class) ///////////////////
......@@ -102,7 +120,8 @@ public class SimpleShell {
* @return The output of the command
*/
private String executeCommand(String commandName, String[] args) {
// TODO: Change sequence of if/else if below to a lookup in a map
// TODO 6: Call run on Command object for given commandName if present
// TODO 7-8-9: Remove if's for cd, ls, pwd once installed as commands
if (Objects.equals(commandName, "pwd")) {
return this.doPwd(args);
} else if (Objects.equals(commandName, "cd")) {
......@@ -114,12 +133,12 @@ public class SimpleShell {
}
}
// TODO: remove this method and replace it with Command -type object
// TODO 7: remove this method and replace it with Command -type object
private String doPwd(String[] args) {
return this.context.getCwd().getAbsolutePath();
}
// TODO: remove this method and replace it with Command -type object
// TODO 8: remove this method and replace it with Command -type object
private String doCd(String[] args) {
if (args.length == 0) {
this.context.goToHome();
......@@ -135,7 +154,7 @@ public class SimpleShell {
}
}
// TODO: remove this method and replace it with Command -type object
// TODO 9: remove this method and replace it with Command -type object
private String doLs(String[] args) {
File cwd = this.context.getCwd();
String s = "";
......
package no.uib.inf101.terminal;
// UiB INF101 ShellLab - Terminal.java
//
// Denne filen inneholder kode som tegner et vindu på skjermen, som
// viser et CommandLineInterface -program. Du er ikke forventet å
// forstå denne koden denne uken, og du trenger ikke gjøre noen
// endringer i denne filen for denne lab'en (med mindre du selv ønsker).
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
/**
* A simple terminal GUI for interacting with command line interfaces.
*/
public class Terminal extends JPanel {
private final CommandLineInterface terminal;
////////////////////////
// Instance variables
////////////////////////
/** The command line interface to interact with. */
private final CommandLineInterface cli;
////////////////////////
// Constructor
////////////////////////
/** Create a new terminal GUI for the given command line interface. */
public Terminal(CommandLineInterface cli) {
this.terminal = cli;
this.cli = cli;
this.setPreferredSize(new Dimension(400, 300));
this.setFocusable(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
Terminal.this.terminal.keyPressed(e.getKeyChar());
Terminal.this.cli.keyPressed(e.getKeyChar());
Terminal.this.repaint();
}
});
}
////////////////////////
// Instance methods
////////////////////////
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
String content = this.terminal.getScreenContent();
// Calculate number of lines that can fit display
int lineHeight = g.getFontMetrics().getHeight() + 5;
int lines = this.getHeight() / lineHeight;
// Draw the text on the screen, line by line
g.setColor(Color.BLACK);
g.setFont(new Font("Monospaced", Font.PLAIN, 12));
int y = lineHeight;
int lineHeight = g.getFontMetrics().getHeight() + 2;
// Iterate through the last 'lines' lines of the content
String[] linesOfContent = content.split("\n");
for (int i = Math.max(0, linesOfContent.length - lines); i < linesOfContent.length; i++) {
g.drawString(linesOfContent[i], 5, y);
// Tegn teksten på skjermen, linje for linje. Potensiell forbedring:
// hvis det er for mange linjer, vis kun de siste av dem.
String content = this.cli.getScreenContent();
int y = 20;
for (String line : content.split("\n")) {
g.drawString(line, 5, y);
y += lineHeight;
}
}
/** Run the terminal GUI in a window frame. */
public void run() {
JFrame frame = new JFrame("INF101 Terminal");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
......
package no.uib.inf101.terminal;
// UiB INF101 ShellLab - TextAnswers.java
//
// I denne filen skal du besvare spørsmålene under. Du kan sjekke
// svarene dine ved å kjøre testen TextAnswersTest.java.
public class TextAnswers {
/*
/**
* Consider the following code:
*
* CommandLineInterface cli = new DummyShell();
*
* What is the type of the variable cli?
*/
/** What is the type of the variable cli? */
static final String q1 = "";
/** In which class is the object cli refers to? */
/**
* Consider the following code:
* CommandLineInterface cli = new DummyShell();
*
* In which class is the object cli refers to?
*/
static final String q2 = "";
/** True or false: CommandLineInterface is both a type and an interface. */
/**
* Consider the following code:
* CommandLineInterface cli = new DummyShell();
*
* true or false: CommandLineInterface is both a type and an interface.
*/
static final Boolean q3 = null;
/** True or false: DummyShell is both a class and a type. */
/**
* Consider the following code:
* CommandLineInterface cli = new DummyShell();
*
* true or false: DummyShell is both a class and a type.
*/
static final Boolean q4 = null;
}