Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

88
LINES

< > BotCompany Repo | #1020446 // Quick Search [unifies other search modules]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Libraryless. Click here for Pure Java version (13160L/97K).

1  
!7
2  
3  
srecord Result(S text, S moduleID, S moduleDesc, O[] params, double value) {
4  
  O coreObject() {
5  
    ret optPar(params, 'additionalInfo, text);
6  
  }
7  
}
8  
9  
module QuickSearchManager > DynQuickSearch {
10  
  transient S resultsQuery;
11  
  transient Map<S, L<Result>> resultsByModule = synchroMap();
12  
  transient L<Result> shownResults;
13  
  transient Map<S, Number> moduleRankings;
14  
  
15  
  start {
16  
    //set doSearchVerbose;
17  
    thread {
18  
      moduleRankings = mapValues(f parseDouble, mechMap("Search Module Rankings"));
19  
    }
20  
  }
21  
  
22  
  afterVisualize {
23  
    onDoubleClickOrEnter(list, r {
24  
      Result r = _get(shownResults, getSelectedIndex(list));
25  
      if (r == null) ret;
26  
      dm_hideQuickSearchModules();
27  
      O defaultAction = getOpt(r.coreObject(), 'defaultAction);
28  
      if (defaultAction == null) defaultAction = optPar(r.params, 'defaultAction);
29  
      callFInNewThread(defaultAction, r.coreObject());
30  
    });
31  
    listPopupMenu(list, voidfunc(JPopupMenu menu, S item) {
32  
      final Result r = _get(shownResults, getSelectedIndex(list));
33  
      if (r == null) ret;
34  
      O[] params = optPar(r.params, 'popupItems);
35  
      for (int i = 0; i+1 < l(params); i += 2) {
36  
        if (isMenuSeparatorIndicator(params[i])) continue with --i;
37  
        fO action = params[i+1];
38  
        addMenuItem(menu, str(params[i]), r {
39  
          callFInNewThread(action, r.coreObject())
40  
        });
41  
      }
42  
    });
43  
  }
44  
  
45  
  LS calc_impl() {
46  
    L<Result> results = concatLists(cloneValues(getResultsByModule(dm_topInput())));
47  
    sortByFieldDesc_inPlace(results, 'value);
48  
    ret map(func(Result r) -> S { resultToText(r) }, shownResults = takeFirst(50, results));
49  
  }
50  
  
51  
  S resultToText(Result r) {
52  
    ret "[" + r.moduleDesc + "] " + r.text;
53  
  }
54  
  
55  
  // clear if query changed
56  
  Map<S, L<Result>> getResultsByModule(S input) {
57  
    synchronized(resultsByModule) {
58  
      if (neq(resultsQuery, input)) {
59  
        resultsQuery = input;
60  
        resultsByModule.clear();
61  
      }
62  
      ret resultsByModule;
63  
    }
64  
  }
65  
  
66  
  // API
67  
  
68  
  void setResultsForModule(S moduleID, S query, L results) {
69  
    setResultsForModule(moduleID, null, query, results);
70  
  }
71  
  
72  
  // results: LS or LPair<S, O> (string to print, additional info)
73  
  void setResultsForModule(fS moduleID, S _moduleDesc, S query, L results, fO... _) {
74  
    fS moduleDesc = _moduleDesc != null ? _moduleDesc : dm_moduleName(moduleID);
75  
    if (neq(query, dm_topInput())) ret with print("Results for wrong query: " + query);
76  
    final double moduleValue = toDouble(mapGet(moduleRankings, dm_programID(moduleID)));
77  
    synchronized(resultsByModule) {
78  
      getResultsByModule(query).put(moduleID, map(results, func(O o) -> Result {
79  
        o instanceof S ?
80  
          Result((S) o, moduleID, moduleDesc, _, 0)
81  
          : Result(getString(o, 'a), moduleID, moduleDesc, paramsPlus(_, additionalInfo := _get(o, 'b)), moduleValue) }));
82  
    }
83  
    
84  
    // show/update list
85  
    //if (nempty(results))
86  
      doSearch(resultsQuery);
87  
  }
88  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 9 computer(s): bhatertpkbcr, cfunsshuasjs, gwrvuhgaqvyk, hpgrupgrauku, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1020446
Snippet name: Quick Search [unifies other search modules]
Eternal ID of this version: #1020446/39
Text MD5: c97879fbd15d2fbc158a9a9836d04869
Transpilation MD5: 724dc76451f4c34cc4ec242457635a45
Author: stefan
Category: javax / stefan's os
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-01-21 13:34:28
Source code size: 3031 bytes / 88 lines
Pitched / IR pitched: No / No
Views / Downloads: 481 / 8096
Version history: 38 change(s)
Referenced in: [show references]