Skip to content

Commit

Permalink
installer v3
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Sep 9, 2024
1 parent 7a7e284 commit 0b23ff3
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 161 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/dergoogler/mmrl/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private String mmrlUserAgent() {
return "MMRL/" + BuildConfig.VERSION_NAME + " (Linux; Android " + Build.VERSION.RELEASE + "; " + Build.MODEL + " Build/" + Build.DISPLAY + ")";
}

private boolean isEmulator = (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
private final boolean isEmulator = (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
|| Build.FINGERPRINT.startsWith("generic")
|| Build.FINGERPRINT.startsWith("unknown")
|| Build.HARDWARE.contains("goldfish")
Expand Down
54 changes: 30 additions & 24 deletions docs/Installer/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
# Installer V2
# Installer V3

Implantation

```shell
if [ "$MMRL_INTR" = "true" ]; then
mmrl_exec() { ui_print "#!mmrl:$*"; }
gui_print() { mmrl_exec color "\"$@\""; }
mmrl_setLastLine() { mmrl_exec setLastLine "\"$@\""; }
gui_image() { mmrl_exec addImage "$*"; }
else
mmrl_exec() { true; }
gui_print() { ui_print "$@" | sed 's/<[A-Z.]*>//g'; }
mmrl_setLastLine() { true; }
gui_image() { true; }
fi
mmrl_exec() {
if [ "$MMRL_INTR" = "true" ]; then
local command=$1
shift
local args=$(printf "|%s" "$@")
args=${args:1}
echo "#!mmrl:<$command=($args)>"
fi
}
```

## Internal commands
Expand All @@ -28,9 +26,10 @@ Clears everything from the terminal
mmrl_exec clearTerminal
```

| Args | Description |
| ---- | ----------- |
| No | |
| Args | Description |
| ---- | ---------------- |
| No | |
| | Returns no value |

### Replace last line

Expand All @@ -40,9 +39,10 @@ You can replace the last placed line, even the last line is a button
mmrl_exec setLastLine "This is a cool log"
```

| Args | Description |
| --------- | -------------------------------------- |
| `args[0]` | Text that should replace the last line |
| Args | Description |
| ---- | -------------------------------------- |
| `$1` | Text that should replace the last line |
| | Returns no value |

### Remove last line

Expand All @@ -52,11 +52,12 @@ This command just removes the last line
mmrl_exec removeLastLine
```

| Args | Description |
| ---- | ----------- |
| No | |
| Args | Description |
| ---- | ---------------- |
| No | |
| | Returns no value |

### Add a button
<!-- ### Add a button
This command can a little bit more but it has less functionality because you can't a click event
Expand All @@ -67,15 +68,20 @@ mmrl_exec addButton "Button text here" --variant "contained or outlined"
| Args | Description |
| ----------- | ----------------------------------------- |
| `args[0]` | Button text |
| `--variant` | Choose between `contained` and `outlined` |
| `--variant` | Choose between `contained` and `outlined` | -->

## Making colored text easir!

When you implant the API you can start using `gui_print`.

```shell
gui_print "This is <FG.MAGENTA>MMRL<R>!"
mmrl_exec color "This is <FG.MAGENTA>MMRL<R>!"
```

| Args | Description |
| ---- | --------------- |
| `$1` | Text |
| | Returns a value |

> [!NOTE]
> Other installer will return `This is MMRL!` because it's a MMRL only syntax
81 changes: 81 additions & 0 deletions docs/Installer/V2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Installer V2

Implantation

```shell
if [ "$MMRL_INTR" = "true" ]; then
mmrl_exec() { ui_print "#!mmrl:$*"; }
gui_print() { mmrl_exec color "\"$@\""; }
mmrl_setLastLine() { mmrl_exec setLastLine "\"$@\""; }
gui_image() { mmrl_exec addImage "$*"; }
else
mmrl_exec() { true; }
gui_print() { ui_print "$@" | sed 's/<[A-Z.]*>//g'; }
mmrl_setLastLine() { true; }
gui_image() { true; }
fi
```

## Internal commands

Every internal command starts with `mmrl_exec`!

### Clear terminal

Clears everything from the terminal

```shell
mmrl_exec clearTerminal
```

| Args | Description |
| ---- | ----------- |
| No | |

### Replace last line

You can replace the last placed line, even the last line is a button

```shell
mmrl_exec setLastLine "This is a cool log"
```

| Args | Description |
| --------- | -------------------------------------- |
| `args[0]` | Text that should replace the last line |

### Remove last line

This command just removes the last line

```shell
mmrl_exec removeLastLine
```

| Args | Description |
| ---- | ----------- |
| No | |

### Add a button

This command can a little bit more but it has less functionality because you can't a click event

```shell
mmrl_exec addButton "Button text here" --variant "contained or outlined"
```

| Args | Description |
| ----------- | ----------------------------------------- |
| `args[0]` | Button text |
| `--variant` | Choose between `contained` and `outlined` |

## Making colored text easir!

When you implant the API you can start using `gui_print`.

```shell
gui_print "This is <FG.MAGENTA>MMRL<R>!"
```

> [!NOTE]
> Other installer will return `This is MMRL!` because it's a MMRL only syntax
File renamed without changes.
127 changes: 63 additions & 64 deletions src/activitys/InstallTerminalV2Activity/hooks/useLines.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Ansi } from "@Components/Ansi";
import { Image } from "@Components/dapi/Image";
import { useModFS } from "@Hooks/useModFS";
import { useStrings } from "@Hooks/useStrings";
import Button from "@mui/material/Button";
import { Shell } from "@Native/Shell";
import { useConfirm } from "material-ui-confirm";
import ModFS from "modfs";
import React from "react";

interface LinesContext {
Expand Down Expand Up @@ -34,23 +36,49 @@ const LinesContext = React.createContext<LinesContext>({
clearTerminal() {},
});

type IntrCommand = (args: string[], options: Record<string, string>, add: any) => void;
const colors = {
R: "\x1b[0m",
BRIGHT: "\x1b[1m",
DIM: "\x1b[2m",
UNDERSCORE: "\x1b[4m",
FG: {
BLACK: "\x1b[30m",
RED: "\x1b[31m",
GREEN: "\x1b[32m",
YELLOW: "\x1b[33m",
BLUE: "\x1b[34m",
MAGENTA: "\x1b[35m",
CYAN: "\x1b[36m",
WHITE: "\x1b[37m",
GRAY: "\x1b[90m",
},
BG: {
BLACK: "\x1b[40m",
RED: "\x1b[41m",
GREEN: "\x1b[42m",
YELLOW: "\x1b[43m",
BLUE: "\x1b[44m",
MAGENTA: "\x1b[45m",
CYAN: "\x1b[46m",
WHITE: "\x1b[47m",
GRAY: "\x1b[100m",
},
};

interface LinesProviderProps extends React.PropsWithChildren {
commands: Record<string, IntrCommand>;
}
interface LinesProviderProps extends React.PropsWithChildren {}

const LinesProvider = (props: LinesProviderProps) => {
const { strings } = useStrings();
const { modFS } = useModFS();
const [useInt, setUseInt] = React.useState(false);
const [lines, setLines] = React.useState<any[]>([]);
const confirm = useConfirm();
const { commands, children } = props;
const { children } = props;

const addText = (text: string, props?: object) => {
const txt = processCommand(text);
if (typeof txt === "string") {

if (typeof txt === "string" && txt !== "undefined") {
setLines((lines) => [
...lines,
{
Expand Down Expand Up @@ -113,66 +141,37 @@ const LinesProvider = (props: LinesProviderProps) => {
]);
};

const processCommand = (rawCommand: string) => {
const format = React.useMemo(
() => ({
addImage(data: string) {
addImage(data);
return "undefined";
},
setLastLine(text: string) {
if (typeof text === "undefined") return "undefined";
setLastLine(text);
return "undefined";
},
color: (text: string) => {
if (typeof text === "undefined") return "undefined";
return ModFS.format(text, colors);
},
clearTerminal: () => {
setLines([]);
return "undefined";
},
removeLastLine: () => {
setLines((p) => p.slice(0, -1));
return "undefined";
},
}),
[]
);

const processCommand = (rawCommand: string): string | "undefined" => {
if (rawCommand.startsWith("#!mmrl:")) {
let args: string[] = [];
let options = {};
let command: string;
rawCommand = rawCommand.substring(7);
const i = rawCommand.indexOf(" ");

if (i !== -1 && rawCommand.length !== i + 1) {
// Extract command arguments and options
const argsString = rawCommand.substring(i + 1).trim();
const matches = argsString.match(/"([^"]+)"|--?[\w-]+|\S+/g);

if (matches) {
for (let j = 0; j < matches.length; j++) {
let match = matches[j];
if (match.startsWith("--")) {
// Long option
const key = match.substring(2);
let value: string | boolean = true;
if (j + 1 < matches.length && !matches[j + 1].startsWith("-")) {
value = matches[++j];
// Remove surrounding quotes if present
if (value.startsWith('"') && value.endsWith('"')) {
value = value.slice(1, -1);
}
}
options[key] = value;
} else if (match.startsWith("-")) {
// Short option
const key = match.substring(1);
let value: string | boolean = true;
if (j + 1 < matches.length && !matches[j + 1].startsWith("-")) {
value = matches[++j];
// Remove surrounding quotes if present
if (value.startsWith('"') && value.endsWith('"')) {
value = value.slice(1, -1);
}
}
options[key] = value;
} else {
// Positional argument
// Remove surrounding quotes if present
if (match.startsWith('"') && match.endsWith('"')) {
match = match.slice(1, -1);
}
args.push(match);
}
}
}

command = rawCommand.substring(0, i);
} else {
command = rawCommand;
}

const handleCommand = commands[command];
if (handleCommand) {
handleCommand(args, options, { addButton: addButton, addText: addText, addImage: addImage, setLines: setLines, lines: lines });
}
return ModFS.format(rawCommand, format) as string | "undefined";
} else {
const info = /^\-(\s+)?(.+)/gm;
const warn = /^\?(\s+)?(.+)/gm;
Expand Down
Loading

0 comments on commit 0b23ff3

Please sign in to comment.