Skip to content

Conversation

friedow
Copy link

@friedow friedow commented Jul 18, 2023

Behavior After this Change
When a plugin calls "Clear", the search results of this specific plugin are cleared.

Behavior Before this Change
When a plugin calls "Clear", the search results of all plugins are cleared.

@jacobgkau jacobgkau requested review from a team August 4, 2023 15:26
@jacobgkau jacobgkau self-assigned this Aug 4, 2023
@jacobgkau
Copy link
Member

So far, I haven't been able to find anywhere where this changes behavior with the default plugins.

I do see some places where comments in the documentation or code might need to be updated to match this change?

/// Clear all results in the launcher list

/// Clear all results in the launcher list.

@friedow
Copy link
Author

friedow commented Aug 7, 2023

Oh, I'm sorry, I forgot add my motivation to fix this! First and foremost: this fix is not necessary to run the default plugins. In fact, most of the default plugins don't use the "Clear" command at all. The reason being that none of the default plugins provide data "proactively" instead plugins like the calc plugin provide data "reactively". They wait for a user input which clears the current list of items implicitly and invokes a "Search" command on the plugin which then adds search results.

This works well for most of the use cases. However, I implemented a clock plugin (which tells the current time, accurate to seconds) which needs to update data "proactively". It uses "Clear" to delete the old time entry and adds another one afterwards even if there was no user interaction at all.

Here is the source for that clock plugin:
clock.nu (https://www.nushell.sh/ script. This should be kinda readable, if not feel free to ask anything :)

#!/usr/bin/env nu

def main [] {
  mut lastSearch = ""

  while true {
    let command = (bash -c "read -t 1 input; inputOrTimeout=${input:="Timeout"}; echo $inputOrTimeout" | from json)

    if $command == "Exit" {
      break
    }

    if $command == "Interrupt" {
      continue
    }

    if ($command | describe) starts-with "record" and  $command.Search? != null {
      $lastSearch = $command.Search
    }
    let searchString = $lastSearch

    mut entries = (getEntries)
    if ($searchString | str length ) > 0  {
      $entries = ($entries | filter {|entry| entryMatchesSearch $entry $searchString })
    }

    if $command == "Timeout" {
      print '"Clear"'
    }
    printEntries $entries
  }
}

def getEntries [] {
  let date = (date now)
  let currentTime = ($date | date format '%H:%M:%S')
  let currentDate = ($date | date format '%A, %d. %B %Y')

  let timeEntry = ($'
    Append:
      id: 0
      name: ($currentTime)
      description: Time
      icon:
        Name: accessories-clock
  ' | from yml)

  let dateEntry = ($'
    Append:
      id: 1
      name: ($currentDate)
      description: Date
      icon:
        Name: date
  ' | from yml)

  return [ $timeEntry, $dateEntry ]
}

def entryMatchesSearch [entry: record, searchString: string] {
  let searchTerms = ($searchString | str downcase | split words)

  let entryString = ($entry.Append.name + " " + $entry.Append.description)
  let entryTerms = ($entryString | str downcase | split words)

  for searchTerm in $searchTerms {
    for entryTerm in $entryTerms {
      if ($entryTerm | str distance $searchTerm | $in <= 2) {
        return true
      }

      if ($entryTerm | str contains $searchTerm) {
        return true
      }
    }
  }

  return false
}

def printEntries [entries: list] {
  for entry in $entries {
    print ($entry | to json -r)
  }
  print '"Finished"'
}

plugin.ron

(
    name: "Clock",
    description: "Shows the current time and date.",
    bin: (path: "clock.nu"),
    icon: Name("clock"),
    query: (history: false),
)

FYI: I'm currently migrating all my launcher plugins to pop-launcher and of course all of this is open source :>. https://github.com/friedow/dotfiles and plugins: https://github.com/friedow/dotfiles/tree/main/modules/onagre

@mmstick
Copy link
Member

mmstick commented Aug 7, 2023

Wouldn't it be better for the plugin to store the time, and only give a search result when requested by a search?

@friedow
Copy link
Author

friedow commented Aug 8, 2023

Updating data proactively is an explicit design decision I made. I really like the reactive feel of it. The clock plugin is also just a simple example of this. Imagine plugins like system monitors, that show CPU or RAM usage. This is where the concept really shines ;).

I mean, its pretty cool :D
onagre
(please ignore the horrible theme :D)

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

Successfully merging this pull request may close these issues.

3 participants