Skip to content

Commit

Permalink
Work on hints
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Oct 22, 2019
1 parent 493e0d7 commit 12d86ec
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 19 deletions.
8 changes: 4 additions & 4 deletions Blink.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
D24AFD58222410E700CFD3C1 /* MBProgressHUD.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0732F10D1D062BF700AB5438 /* MBProgressHUD.framework */; };
D24AFD59222410E700CFD3C1 /* MBProgressHUD.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 0732F10D1D062BF700AB5438 /* MBProgressHUD.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
D2512AC520BD4C3000A80257 /* curl_ios_static.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D2E4F92E20B2BB4500B30F7B /* curl_ios_static.framework */; };
D25D58102358897B00D1BCAE /* Completions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D25D580F2358897B00D1BCAE /* Completions.swift */; };
D25D58102358897B00D1BCAE /* Complete.swift in Sources */ = {isa = PBXBuildFile; fileRef = D25D580F2358897B00D1BCAE /* Complete.swift */; };
D263489820BEBFE000A8E89F /* libsshd.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D263489720BEBFE000A8E89F /* libsshd.a */; };
D265FBBD2317DD3C0017EAC4 /* BlinkTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D265FBBC2317DD3C0017EAC4 /* BlinkTests.swift */; };
D265FBC52317E5090017EAC4 /* SessionParamsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D265FBC42317E5090017EAC4 /* SessionParamsTests.swift */; };
Expand Down Expand Up @@ -609,7 +609,7 @@
D248E67D22DE14100057FE67 /* SessionParams.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SessionParams.swift; sourceTree = "<group>"; };
D2496F3B20038B3300E75FE9 /* hterm_all.min.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = hterm_all.min.js; sourceTree = "<group>"; };
D2496F402003941B00E75FE9 /* term.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = term.js; sourceTree = "<group>"; };
D25D580F2358897B00D1BCAE /* Completions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Completions.swift; sourceTree = "<group>"; };
D25D580F2358897B00D1BCAE /* Complete.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Complete.swift; sourceTree = "<group>"; };
D263488A20BEBF5800A8E89F /* libsshpp.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = libsshpp.hpp; sourceTree = "<group>"; };
D263488B20BEBF5800A8E89F /* legacy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = legacy.h; sourceTree = "<group>"; };
D263488C20BEBF5800A8E89F /* callbacks.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = callbacks.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -811,7 +811,7 @@
D2887A5D22DCA6D500701BD5 /* SceneDelegate.swift */,
D248E67522DDDF130057FE67 /* UIStateRestorable.swift */,
D2BC514C2355C3AE0034FDD4 /* History.swift */,
D25D580F2358897B00D1BCAE /* Completions.swift */,
D25D580F2358897B00D1BCAE /* Complete.swift */,
);
path = Blink;
sourceTree = "<group>";
Expand Down Expand Up @@ -1800,7 +1800,7 @@
C9B2E0301D6B612400B89F69 /* BKPubKey.m in Sources */,
D27BBA1C20529FFF00AEA303 /* TermStream.m in Sources */,
D296198D214F96D50070935A /* digest-openssl.c in Sources */,
D25D58102358897B00D1BCAE /* Completions.swift in Sources */,
D25D58102358897B00D1BCAE /* Complete.swift in Sources */,
B7D4503C1DD4706000CE0DBE /* Reachability.m in Sources */,
07AA065D1E08B6EB008310B7 /* UserDefaultsPasscodeRepository.swift in Sources */,
B752EE2E1DFEF45300E305C8 /* BKSecurityConfigurationViewController.m in Sources */,
Expand Down
37 changes: 33 additions & 4 deletions Blink/Completions.swift → Blink/Complete.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import ios_system

private let _completionQueue = DispatchQueue(label: "completion.queue")

struct Completions {
struct Complete {

struct ForRequest: Codable {
let id: Int
Expand All @@ -47,6 +47,7 @@ struct Completions {
let requestId: Int
let input: String
let result: [String]
let hint: String
let kind: String
}

Expand Down Expand Up @@ -189,22 +190,49 @@ struct Completions {
return Kind(rawValue: operatesOn(cmd) ?? "") ?? .no
}
}

static func _hint(kind: Kind, candidates: [String]) -> String {
guard let first = candidates.first else {
return ""
}
var result = "";
switch kind {
case .command:
if let hint = _commandHints()[first] {
result = "\(first) - \(hint)"
}
default:
result = candidates.prefix(5).joined(separator: ", ")
}

return result;
}

static func _for(str: String) -> (kind: Kind, result: [String]) {
static func _for(str: String) -> (kind: Kind, result: [String], hint: String) {
let input = _lastCommand(str)
var result:[String] = []

let parts = input.value.split(separator: " ", maxSplits: 1, omittingEmptySubsequences: false)
debugPrint(input.value, parts)

var kind: Kind = .command
var hint: String = ""
if parts.count <= 1 {
return (kind: kind, result: _complete(kind: kind, input: str))
result = _complete(kind: kind, input: input.value)
hint = _hint(kind: kind, candidates: result)
return (kind: kind, result: result.map { input.prefix + $0 }, hint: hint.isEmpty ? "" : input.prefix + hint)
}

let cmd = String(parts[0])
kind = _completionKind(cmd)
result = _complete(kind: kind, input: String(parts[1]))
hint = _hint(kind: kind, candidates: result)

let cmdPrefix = input.prefix + cmd + " "
return (
kind: kind,
result: _complete(kind: kind, input: String(parts[1])).map( { cmd + " " + input.prefix + $0 } )
result: result.map( { cmdPrefix + $0 } ),
hint: hint.isEmpty ? "" : cmdPrefix + hint
)
}

Expand All @@ -214,6 +242,7 @@ struct Completions {
requestId: request.id,
input: request.input,
result: res.result,
hint: res.hint,
kind: res.kind.rawValue)
}

Expand Down
3 changes: 2 additions & 1 deletion Blink/History.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct History {

struct SearchResponse: Codable {
let requestId: Int
let pattern: String
let lines: [Line]
let found: Int
let total: Int
Expand Down Expand Up @@ -165,7 +166,7 @@ struct History {
let (total, lines) = _filter(lines: _getLines(), pattern: request.pattern)
let slice = _slice(lines: lines, with: request)

return SearchResponse(requestId: request.id, lines: slice, found: lines.count, total: total)
return SearchResponse(requestId: request.id, pattern: request.pattern, lines: slice, found: lines.count, total: total)
}

static func _searchAPI(json: String) -> String? {
Expand Down
19 changes: 11 additions & 8 deletions Blink/TermController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,21 +216,24 @@ extension TermController: SessionDelegate {
}
}

let _apiRoutes:[String: (MCPSession, String) -> AnyPublisher<String, Never>] = [
"history.search": History.searchAPI,
"completion.for": Complete.forAPI
]

extension TermController: TermDeviceDelegate {

func apiCall(_ api: String!, andRequest request: String!) {
guard let session = _session else {
guard
let api = api,
let session = _session,
let call = _apiRoutes[api]
else {
return
}

var call: (MCPSession, String) -> AnyPublisher<String, Never>
switch api {
case "history.search": call = History.searchAPI
case "completion.for": call = Completions.forAPI
default: return;
}

weak var termView = _termView

_ = call(session, request)
.receive(on: RunLoop.main)
.sink { termView?.apiResponse(api, response: $0) }
Expand Down
4 changes: 2 additions & 2 deletions Resources/hterm_all.min.js

Large diffs are not rendered by default.

0 comments on commit 12d86ec

Please sign in to comment.