github.com/quantumghost/awgo@v0.15.0/workflow_options.go (about)

     1  //
     2  // Copyright (c) 2018 Dean Jackson <deanishe@deanishe.net>
     3  //
     4  // MIT Licence. See http://opensource.org/licenses/MIT
     5  //
     6  // Created on 2018-02-09
     7  //
     8  
     9  package aw
    10  
    11  import "github.com/deanishe/awgo/fuzzy"
    12  
    13  // Option is a configuration option for Workflow.
    14  // Pass one or more Options to New() or Workflow.Configure().
    15  //
    16  // An Option returns its inverse (i.e. an Option that restores the
    17  // previous value).
    18  //
    19  // You can apply Options at any time, so you can, e.g. suppress UIDs
    20  // if you need to for items to be in a particular order.
    21  type Option func(wf *Workflow) Option
    22  
    23  // options combines Options, allowing the application of their inverse
    24  // via a single call to options.apply().
    25  type options []Option
    26  
    27  // apply configures Workflow with all options and returns a single Option
    28  // to reverse all changes.
    29  func (opts options) apply(wf *Workflow) Option {
    30  	previous := make(options, len(opts))
    31  	for i, opt := range opts {
    32  		previous[i] = wf.Configure(opt)
    33  	}
    34  	return previous.apply
    35  }
    36  
    37  // HelpURL sets link shown in debugger & log if Run() catches a panic
    38  // ("Get help at http://…").
    39  // Set this to the URL of an issue tracker/forum thread where users can
    40  // ask for help.
    41  func HelpURL(URL string) Option {
    42  	return func(wf *Workflow) Option {
    43  		prev := wf.helpURL
    44  		ma := &helpMA{URL}
    45  		if URL != "" {
    46  			wf.MagicActions.Register(ma)
    47  		} else {
    48  			wf.MagicActions.Unregister(ma)
    49  		}
    50  		wf.helpURL = URL
    51  		return HelpURL(prev)
    52  	}
    53  }
    54  
    55  // LogPrefix is the printed to debugger at the start of each run.
    56  // Its purpose is to ensure that the first real log message is shown
    57  // on its own line.
    58  // It is only sent to Alfred's debugger, not the log file.
    59  //
    60  // Default: Beer Mug (\U0001F37A)
    61  func LogPrefix(prefix string) Option {
    62  	return func(wf *Workflow) Option {
    63  		prev := wf.logPrefix
    64  		wf.logPrefix = prefix
    65  		return LogPrefix(prev)
    66  	}
    67  }
    68  
    69  // MagicPrefix sets the prefix for "magic" commands.
    70  // If a user enters this prefix, AwGo takes control of the workflow and
    71  // shows a list of matching magic commands to the user.
    72  //
    73  // Default: workflow:
    74  func MagicPrefix(prefix string) Option {
    75  	return func(wf *Workflow) Option {
    76  		prev := wf.magicPrefix
    77  		wf.magicPrefix = prefix
    78  		return MagicPrefix(prev)
    79  	}
    80  }
    81  
    82  // MaxLogSize sets the size (in bytes) when workflow log is rotated.
    83  // Default: 1 MiB
    84  func MaxLogSize(bytes int) Option {
    85  	return func(wf *Workflow) Option {
    86  		prev := wf.maxLogSize
    87  		wf.maxLogSize = bytes
    88  		return MaxLogSize(prev)
    89  	}
    90  }
    91  
    92  // MaxResults is the maximum number of results to send to Alfred.
    93  // 0 means send all results.
    94  // Default: 0
    95  func MaxResults(num int) Option {
    96  	return func(wf *Workflow) Option {
    97  		prev := wf.maxResults
    98  		wf.maxResults = num
    99  		return MaxResults(prev)
   100  	}
   101  }
   102  
   103  // TextErrors tells Workflow to print errors as text, not JSON.
   104  // Messages are still sent to STDOUT. Set to true if error
   105  // should be captured by Alfred, e.g. if output goes to a Notification.
   106  func TextErrors(on bool) Option {
   107  	return func(wf *Workflow) Option {
   108  		prev := wf.textErrors
   109  		wf.textErrors = on
   110  		return TextErrors(prev)
   111  	}
   112  }
   113  
   114  // SortOptions sets the fuzzy sorting options for Workflow.Filter().
   115  // See fuzzy and fuzzy.Option for info on (configuring) the sorting
   116  // algorithm.
   117  //
   118  // _examples/fuzzy contains an example workflow using fuzzy sort.
   119  func SortOptions(opts ...fuzzy.Option) Option {
   120  
   121  	return func(wf *Workflow) Option {
   122  
   123  		prev := wf.sortOptions
   124  		wf.sortOptions = opts
   125  
   126  		return SortOptions(prev...)
   127  	}
   128  }
   129  
   130  // SessionName changes the name of the variable used to store the session ID.
   131  //
   132  // This is useful if you have multiple Script Filters chained together that
   133  // you don't want to use the same cache.
   134  func SessionName(name string) Option {
   135  
   136  	return func(wf *Workflow) Option {
   137  
   138  		prev := wf.sessionName
   139  		wf.sessionName = name
   140  
   141  		return SessionName(prev)
   142  	}
   143  }
   144  
   145  // SuppressUIDs prevents UIDs from being set on feedback Items.
   146  //
   147  // This turns off Alfred's knowledge, i.e. prevents Alfred from
   148  // applying its own sort, so items will be shown in the
   149  // order you add them.
   150  //
   151  // Useful if you need to force a particular item to the top/bottom.
   152  //
   153  // This setting only applies to Items created *after* it has been
   154  // set.
   155  func SuppressUIDs(on bool) Option {
   156  	return func(wf *Workflow) Option {
   157  		prev := wf.Feedback.NoUIDs
   158  		wf.Feedback.NoUIDs = on
   159  		return SuppressUIDs(prev)
   160  	}
   161  }
   162  
   163  // Update sets the updater for the Workflow.
   164  // Panics if a version number isn't set (in Alfred Preferences).
   165  //
   166  // See Updater interface and subpackage update for more documentation.
   167  func Update(updater Updater) Option {
   168  	return func(wf *Workflow) Option {
   169  		if updater != nil && wf.Version() == "" {
   170  			panic("can't set Updater as workflow has no version number")
   171  		}
   172  		prev := wf.Updater
   173  		wf.setUpdater(updater)
   174  		return Update(prev)
   175  	}
   176  }
   177  
   178  // AddMagic registers Magic Actions with the Workflow.
   179  // Magic Actions connect special keywords/queries to callback functions.
   180  // See the MagicAction interface for more information.
   181  func AddMagic(actions ...MagicAction) Option {
   182  	return func(wf *Workflow) Option {
   183  		for _, action := range actions {
   184  			wf.MagicActions.Register(action)
   185  		}
   186  		return RemoveMagic(actions...)
   187  	}
   188  }
   189  
   190  // RemoveMagic unregisters Magic Actions with Workflow.
   191  // Magic Actions connect special keywords/queries to callback functions.
   192  // See the MagicAction interface for more information.
   193  func RemoveMagic(actions ...MagicAction) Option {
   194  	return func(wf *Workflow) Option {
   195  		wf.MagicActions.Unregister(actions...)
   196  		return AddMagic(actions...)
   197  	}
   198  }