Skip to content

Commit

Permalink
Added interaction mode setting
Browse files Browse the repository at this point in the history
  • Loading branch information
cgxeiji committed Nov 5, 2018
1 parent b84df20 commit e481102
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 17 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,33 @@ go get -d github.com/cgxeiji/scholar

Done!

## Interactive Mode

By default, Scholar will launch a selection screen when `edit`, `open`, or
`remove` commands have a query with more than one entry.

If you want to use Scholar inside a script, you can disable interactive mode by
passing the flag `-i`, or setting `interactive: false` in the configuration
file.

When interactive mode is disabled, Scholar will return an `exit status 1` and
the number of entries found if there is more than one entry that matches the
query.

## TODO

### General

- [ ] Add `-i` flag to enable/disable interactive mode.
- [ ] Add `interactive: true` settings in the configuration file.
- [x] Add `-i` flag to enable/disable interactive mode.
- [x] Add `interactive: true` settings in the configuration file.
- [x] Make attached file path relative to entry, ~unless is an external file~.
- [ ] Be able to reference a file instead of copying it.
- [ ] Add support for attaching multiple files.

### Add

- [ ] Add a flag for manual/auto input of metadata.
- [ ] Handle non-interactive mode for add.

### Config

Expand Down
2 changes: 1 addition & 1 deletion cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var editCmd = &cobra.Command{
Edit an entry's metadata using the default's text editor.
`,
Run: func(cmd *cobra.Command, args []string) {
if entry := guiQuery(entryList(), strings.Join(args, " ")); entry != nil {
if entry := queryEntry(strings.Join(args, " ")); entry != nil {
if addAttach != "" {
attach(entry, addAttach)
return
Expand Down
19 changes: 19 additions & 0 deletions cmd/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,22 @@ func checkDirKey(path, dir string, e *scholar.Entry) {
fmt.Println(" ", filepath.Join(path, dir), ">",
filepath.Join(path, e.GetKey()))
}

func queryEntry(search string) *scholar.Entry {
var entry *scholar.Entry

if viper.GetBool("GENERAL.interactive") != viper.GetBool("interactive") {
entry = guiQuery(entryList(), search)
} else {
found := guiSearch(search, entryList(), searcher)
if len(found) == 1 {
entry = found[0]
} else {
fmt.Println("Found", len(found), "entries.")
fmt.Println("Please, refine your query.")
os.Exit(1)
}
}

return entry
}
22 changes: 11 additions & 11 deletions cmd/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,6 @@ func guiQuery(entries []*scholar.Entry, search string) *scholar.Entry {
v.Title = "SEARCH BAR"
fmt.Fprint(v, search)

searcher := func(input string, entry *scholar.Entry) bool {
title := strings.Replace(strings.ToLower(entry.Required["title"]), " ", "", -1)
aus := strings.Replace(strings.ToLower(entry.Required["author"]), " ", "", -1)
k := strings.Replace(strings.ToLower(entry.Key), " ", "", -1)
s := fmt.Sprintf("%s%s%s", title, aus, k)
input = strings.TrimSpace(input)
input = strings.Replace(strings.ToLower(input), " ", "", -1)

return strings.Contains(s, input)
}

// Check if the initial search is a unique result
found := guiSearch(search, entries, searcher)
switch len(found) {
Expand Down Expand Up @@ -432,3 +421,14 @@ func formatEntryInfo(w io.Writer, e *scholar.Entry) {
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}

func searcher(input string, entry *scholar.Entry) bool {
title := strings.Replace(strings.ToLower(entry.Required["title"]), " ", "", -1)
aus := strings.Replace(strings.ToLower(entry.Required["author"]), " ", "", -1)
k := strings.Replace(strings.ToLower(entry.Key), " ", "", -1)
s := fmt.Sprintf("%s%s%s", title, aus, k)
input = strings.TrimSpace(input)
input = strings.Replace(strings.ToLower(input), " ", "", -1)

return strings.Contains(s, input)
}
4 changes: 2 additions & 2 deletions cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ TODO: if there is no file attached, the entry's metadata file is opened.
--------------------------------------------------------------------------------
`,
Run: func(cmd *cobra.Command, args []string) {
if entry := guiQuery(entryList(), strings.Join(args, " ")); entry != nil {
if entry := queryEntry(strings.Join(args, " ")); entry != nil {
if entry.File != "" {
open(filepath.Join(libraryPath(), entry.GetKey(), entry.File))
} else if url, ok := entry.Optional["url"]; ok && url != "" {
Expand All @@ -66,7 +66,7 @@ TODO: if there is no file attached, the entry's metadata file is opened.
} else if doi, ok := entry.Optional["doi"]; ok && doi != "" {
open(fmt.Sprintf("https://dx.doi.org/%s", doi))
} else {
fmt.Println("No file or url associated with entry.")
fmt.Println("No file, doi, or url associated with entry.")
os.Exit(1)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ TODO: Add remove confirmation
--------------------------------------------------------------------------------
`,
Run: func(cmd *cobra.Command, args []string) {
if entry := guiQuery(entryList(), strings.Join(args, " ")); entry != nil {
if entry := queryEntry(strings.Join(args, " ")); entry != nil {
path := filepath.Join(libraryPath(), entry.GetKey())
if err := os.RemoveAll(path); err != nil {
panic(err)
Expand Down
5 changes: 5 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func init() {
//rootCmd.PersistentFlags().StringVar(&confFile, "config", "", "config file (default $HOME/.config/scholar/config.yaml)")
//rootCmd.PersistentFlags().StringVar(&typesFile, "types", "", "entry types file (default $HOME/.config/scholar/types.yaml)")
rootCmd.PersistentFlags().StringVarP(&currentLibrary, "library", "l", "", "specify the library")
rootCmd.PersistentFlags().BoolP("interactive", "i", false, "toggle interactive mode (enabled by default)")
viper.BindPFlag("interactive", rootCmd.PersistentFlags().Lookup("interactive"))
}

// initConfig reads in config file and ENV variables if set.
Expand All @@ -86,6 +88,9 @@ func initConfig() {
}

viper.AutomaticEnv() // read in environment variables that match
// Set default values
viper.SetDefault("GENERAL.interactive", true)
viper.SetDefault("GENERAL.editor", "vi")

// Load the configuration file. If not found, auto-generate one.
if err := viper.ReadInConfig(); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions cmd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ GENERAL:
default: scholar
# Set the default text editor
editor: vi
# Set this to false to disable interactive mode
interactive: true
# Set the email for polite use of CrossRef
mailto: mail@example.com
Expand All @@ -60,6 +62,8 @@ GENERAL:
default: scholar
# Set the default text editor
editor: vi
# Set this to false to disable interactive mode
interactive: true
# Set the email for polite use of CrossRef
mailto: mail@example.com
Expand Down

0 comments on commit e481102

Please sign in to comment.