Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a replxx_hints_size function for c api. #43

Closed
Milerius opened this issue May 19, 2019 · 6 comments
Closed

Add a replxx_hints_size function for c api. #43

Milerius opened this issue May 19, 2019 · 6 comments

Comments

@Milerius
Copy link

So we can detect if there is only one match in the hints, and make a colorized output also in C and other bindings.

@AmokHuginnsson
Copy link
Owner

Can you explain what is the use of the proposed API function?

@Milerius
Copy link
Author

It's the equivalent of the exemple in cxx-api.cxx (hints.size()) to detect that only one hint's remain

@AmokHuginnsson
Copy link
Owner

AmokHuginnsson commented May 19, 2019

cxx-api example has call hints.size() indeed, but this call is not using replxx API, it is client code only. You can already achieve the same effect with C API. To be honest I still do not understand what do you want to achieve. Client code knows how many hints there are and replxx code does not cache this information anywhere. And caching this information should be responsibility of client code anyway.

@Milerius
Copy link
Author

How can you get the hints.size using the c api :O ?

@AmokHuginnsson
Copy link
Owner

Please look at the code of the cxx-api example carefully.
The hints variable is defined locally, and populated locally in the for loop, hints.size() is just a lucky convenience given by std::vector<T> type. In C code one would have to use local variable to count hints.

Using c-api hintHook as an example:

void hintHook(char const* context, replxx_hints* lc, int* contextLen, ReplxxColor* c, void* ud) {
  char** examples = (char**)( ud );
  int i;
  int utf8ContextLen = context_len( context );
  int prefixLen = strlen( context ) - utf8ContextLen;
  int hintCount = 0;
  *contextLen = utf8str_codepoint_len( context + prefixLen, utf8ContextLen );
  if ( *contextLen > 0 ) {
    for (i = 0; examples[i] != NULL; ++i) {                                                                                                                                                                                                                    
      if (strncmp(context + prefixLen, examples[i], utf8ContextLen) == 0) {
        replxx_add_hint(lc, examples[i]);
        ++ hintCount;
      }
    }
  }
  // now `hintCount` variable holds the number of hints.
} 

@Milerius
Copy link
Author

Milerius commented May 19, 2019

Ok I got it, thank's a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants