-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgopher_menu.ts
43 lines (38 loc) · 1.32 KB
/
gopher_menu.ts
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
import {CRLF} from './gopher_common.ts';
import {GopherItem} from './gopher_item.ts';
import {ItemType} from './gopher_types.ts';
/** Represents a Gopher top-level menu. */
export class Menu extends GopherItem {
/** The items within this menu. */
items: MenuItem[] = Array<MenuItem>();
/** Generates a Gopher menu from the raw string returned from the server. */
constructor(menuString: string) {
super();
const lines = menuString.split(CRLF);
for (const line of lines) {
if (!line) continue;
if (line === `.`) break;
this.items.push(new MenuItem(line));
}
}
}
/** Represents a single entry in a Gopher menu. */
export class MenuItem extends GopherItem {
constructor(menuItemString: string) {
super();
this.original = menuItemString;
this.type = menuItemString.substring(0, 1) as ItemType;
const parts = menuItemString.substring(1).split('\t');
this.name = parts[0];
this.selector = parts[1];
this.hostname = parts[2];
this.port = Number(parts[3]);
if (parts[4] && parts[4] === '+') this.gopherPlus = true;
}
toString(): string {
if (this.selector === 'fake' || this.port === 0 || this.hostname === '(NULL)') {
return `${this.type} ${this.name}`;
}
return `${this.type} ${this.name} gopher://${this.hostname}:${this.port}${this.selector}`;
}
}