File: nom

package info (click to toggle)
nom 0.1.5-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: ruby: 650; makefile: 10
file content (95 lines) | stat: -rwxr-xr-x 3,150 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env ruby

# nom – helps you lose weight by tracking your energy intake
# Copyright (C) 2014-2023  blinry <mail@blinry.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

require "date"
require "nom/nom"

commands = [
    # format: [ long_form, short_form, arguments, description ]
    [ "status", nil, nil, "Display a short food log" ],
    [ "weight", "w", "<weight>", "Report a weight measurement" ],
    [ "search", "s", "<term>", "Search for a food item in the web" ],
    [ "nom", "n", "<description> <energy>", "Report that you ate something" ],
    [ "yesterday", "y", "<desc.> <energy>", "Like nom, but for yesterday" ],
    [ "plot", "p", nil, "Plot a weight/intake graph" ],
    [ "log", "l", nil, "Display the full food log" ],
    [ "grep", "g", "<term>", "Search in the food log" ],
    [ "edit", "e", nil, "Edit the input file" ],
    [ "editw", "ew", nil, "Edit the weight file" ],
    [ "config", "c", nil, "Edit the config file (see below for options)" ],
    [ "help", nil, nil, "Print this help" ],
]

aliases = {
    "--help": "help",
    "-h": "help",
}

nom = Nom::Nom.new

cmd_name = (ARGV.shift or "status")

if aliases.include? cmd_name.to_sym
    cmd_name = aliases[cmd_name.to_sym]
end

command = commands.find{|c| c[0] == cmd_name or c[1] == cmd_name}

if command.nil?
    ARGV.unshift(cmd_name)
    if ARGV.last.to_f != 0
        if ARGV.size > 1
            # some words followed by a number
            cmd_name = "nom"
        else
            # a single number
            cmd_name = "weight"
        end
    else
        # some words
        cmd_name = "search"
    end

    command = commands.find{|c| c[0] == cmd_name or c[1] == cmd_name}
end

if command[0] == "help"
    puts "Available subcommands:"
    commands.each do |c|
        puts "  "+"#{c[1].to_s.rjust(2)}#{c[1] ? "," : " "} #{c[0]} #{c[2]}".ljust(32)+c[3]
    end
    puts "There are some useful defaults:"
    puts "      "+"(no arguments)".ljust(28)+"status"
    puts "      "+"<number>".ljust(28)+"weight <number>"
    puts "      "+"<term>".ljust(28)+"search <term>"
    puts "      "+"<term> <number>".ljust(28)+"nom <term> <number>"
    nom.config_usage
else
    begin
        if ARGV.empty?
            nom.send(command[0])
        else
            nom.send(command[0], ARGV)
        end
    rescue Exception => e
        puts e.backtrace
        puts e.message
        puts "Something went wrong. Usage of this command is: nom #{command[0]} #{command[2]}"
    end
end