Compare commits
25 Commits
Author | SHA1 | Date | |
---|---|---|---|
241ebf7fb4 | |||
8e09177f24 | |||
ad6cbf62af | |||
97664825e2 | |||
35894ebafc | |||
49c2cf90d4 | |||
5f802d2084 | |||
3611b94ab6 | |||
00f15ca913 | |||
6c1288d741 | |||
8e4f505ad0 | |||
79ab374f86 | |||
acc7655e83 | |||
c657ed047c | |||
c083462a63 | |||
a469ae41a2 | |||
62a16292ec | |||
9a8277dd07 | |||
a9a649d280 | |||
4b4e64500a | |||
f24c7aafc1 | |||
328cbc3b0c | |||
4e016a3b4f | |||
43d86103c4 | |||
9112fc018c |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,2 @@
|
||||
*.o
|
||||
kwh-cost
|
||||
.vscode
|
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021-2023 Alexander "Arav" Andreev <me@arav.su>
|
||||
Copyright (c) 2021-2024 Alexander "Arav" Andreev <me@arav.su>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
|
31
Makefile
31
Makefile
@ -1,30 +1,29 @@
|
||||
TARGET=kwh-cost
|
||||
TARGET := kwh-cost
|
||||
|
||||
DESTDIR:=
|
||||
PREFIX:=/usr/local
|
||||
DESTDIR ?=
|
||||
PREFIX ?= /usr/local
|
||||
|
||||
CFLAGS=--std=c99 -O3 -Wall -Werror -Wextra -pedantic
|
||||
CC ?= cc
|
||||
CFLAGS := -std=c99 -O3 -Wall -Werror -Wextra -pedantic
|
||||
|
||||
SRC_DIR=src
|
||||
|
||||
DESTDIR=/
|
||||
|
||||
.PHONY: clean install uninstall test
|
||||
|
||||
${TARGET}: ${SRC_DIR}/main.c ${SRC_DIR}/main.h
|
||||
${CC} ${CFLAGS} ${LDFLAGS} $< -o $@
|
||||
${TARGET}: ${TARGET}.c
|
||||
${CC} ${CFLAGS} $< -o $@
|
||||
|
||||
clean:
|
||||
rm ${TARGET}
|
||||
rm -f ${TARGET}
|
||||
|
||||
install:
|
||||
install -Dm755 ${TARGET} ${DESTDIR}${PREFIX}/bin/${TARGET}
|
||||
install -Dm644 LICENSE ${DESTDIR}${PREFIX}/share/licenses/${TARGET}/LICENSE
|
||||
install: | ${TARGET}
|
||||
install -spDm755 ${TARGET} ${DESTDIR}${PREFIX}/bin/${TARGET}
|
||||
install -pDm644 LICENSE ${DESTDIR}${PREFIX}/share/licenses/${TARGET}/LICENSE
|
||||
|
||||
uninstall:
|
||||
rm -f ${DESTDIR}${PREFIX}/bin/${TARGET}
|
||||
rm -rf ${DESTDIR}${PREFIX}/share/licenses/${TARGET}
|
||||
rm -f ${DESTDIR}${PREFIX}/share/licenses/${TARGET}/LICENSE
|
||||
rmdir ${DESTDIR}${PREFIX}/share/licenses/${TARGET}
|
||||
|
||||
test:
|
||||
@echo -n "Does input \"3.68 120 30d\" gives 317.95? "
|
||||
test: | ${TARGET}
|
||||
@echo -en "Does input \e[36m\"3.68 120 30d\"\e[0m gives 317.95? "
|
||||
@test "${shell ./kwh-cost 3.68 120 30d}" = "317.95" && { echo -e "\e[32mYES\e[0m"; } || { echo -e "\e[31mNO\e[0m"; }
|
7
PKGBUILD
7
PKGBUILD
@ -1,21 +1,20 @@
|
||||
# Maintainer: Alexander "Arav" Andreev <me@arav.su>
|
||||
pkgname=kwh-cost
|
||||
pkgver=1.1.1
|
||||
pkgver=1.2.0
|
||||
pkgrel=1
|
||||
pkgdesc="Calculates cost of electricity."
|
||||
arch=('i686' 'x86_64' 'arm' 'armv6h' 'armv7h' 'aarch64')
|
||||
url="https://git.arav.su/Arav/kwh-cost"
|
||||
license=('MIT')
|
||||
options=('strip')
|
||||
source=("${pkgver}.tar.gz::https://git.arav.su/Arav/kwh-cost/archive/${pkgver}.tar.gz")
|
||||
md5sums=('SKIP')
|
||||
|
||||
build() {
|
||||
cd "$srcdir/$pkgname"
|
||||
make DESTDIR="$pkgdir/"
|
||||
make
|
||||
}
|
||||
|
||||
package() {
|
||||
cd "$srcdir/$pkgname"
|
||||
make DESTDIR="$pkgdir/" PREFIX="/usr" install
|
||||
make DESTDIR="$pkgdir" PREFIX="/usr" install
|
||||
}
|
16
README.md
16
README.md
@ -8,12 +8,14 @@ Calculates cost of electricity.
|
||||
$ kwh-cost [OPTIONS] price wattage interval [interval ..]
|
||||
```
|
||||
|
||||
Options are `-v, --version` to show program's version; and `-h, --help` to show usage of a program.
|
||||
Options are `-v` to show program's version; and `-h` to show usage of a program.
|
||||
|
||||
`price` is a price per kWh. `wattage` is how many Watts being consumed. `interval` is for how long.
|
||||
|
||||
`interval` is in seconds, but you can specify a unit. Units are the following:
|
||||
`m` for minutes; `h` for hours; and `d` for days. There could be as many `interval`s passed as you need.
|
||||
`wattage` is in Watts, but it can come with following units: `m`, `k|K`, `M`, `G`, `T`.
|
||||
|
||||
`interval` is in seconds, but the following units are supported: `m|M` for minutes; `h|H` for hours;
|
||||
and `d|D` for days. Also, there could be as many `interval`s passed as you need.
|
||||
|
||||
Here is an example:
|
||||
|
||||
@ -27,10 +29,12 @@ Will give 348.29.
|
||||
|
||||
### Manually
|
||||
|
||||
First run `make` to build a program, and then `make install` to copy executable in `/usr/bin/`. Later, use `make uninstall` to delete it, or just delete `/usr/bin/kwh-cost`.
|
||||
Just run `make install` to build and copy executable in `/usr/local/bin/`, set a `PREFIX=` variable to change a default `/usr/local` path. Or just run `make` to only build an executable.
|
||||
|
||||
Use `make uninstall` to delete it (don't forget about the `PREFIX=` variable if you previously set it), or just delete `${PREFIX}/bin/kwh-cost` by hand.
|
||||
|
||||
A `LICENSE` file is also being copied to `${PREFIX}/share/licenses/kwh-cost/LICENSE`.
|
||||
|
||||
### For ArchLinux
|
||||
|
||||
You can take a [PKGBUILD](/Arav/kwh-cost/raw/branch/master/PKGBUILD) file and in a directory with it run `makepkg -i`.
|
||||
|
||||
If you want to use a pre-built package for Arch from Releases, then install it using `pacman -U` command.
|
||||
|
113
kwh-cost.c
Normal file
113
kwh-cost.c
Normal file
@ -0,0 +1,113 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define VERSION "1.2.0"
|
||||
|
||||
typedef long double price_t;
|
||||
typedef long double wattage_t;
|
||||
typedef unsigned long interval_t;
|
||||
|
||||
interval_t interval_to_seconds(const char *const interval) {
|
||||
interval_t intrvl = (interval_t)atol(interval);
|
||||
|
||||
switch (interval[strlen(interval) - 1]) {
|
||||
case 'm':
|
||||
case 'M':
|
||||
return intrvl * 60;
|
||||
case 'h':
|
||||
case 'H':
|
||||
return intrvl * 60 * 60;
|
||||
case 'd':
|
||||
case 'D':
|
||||
return intrvl * 60 * 60 * 24;
|
||||
default:
|
||||
return intrvl;
|
||||
}
|
||||
}
|
||||
|
||||
wattage_t wattage_to_watts(const char *const wattage) {
|
||||
wattage_t wattg = (wattage_t)atof(wattage);
|
||||
|
||||
switch (wattage[strlen(wattage)-1]) {
|
||||
case 'm':
|
||||
return wattg / 1000.;
|
||||
case 'k':
|
||||
case 'K':
|
||||
return wattg * 1000.;
|
||||
case 'M':
|
||||
return wattg * 1000000.;
|
||||
case 'G':
|
||||
return wattg * 1000000000.;
|
||||
case 'T':
|
||||
return wattg * 1000000000000.;
|
||||
default:
|
||||
return wattg;
|
||||
}
|
||||
}
|
||||
|
||||
price_t price_kwh_to_wattsecond(const price_t price) {
|
||||
return price / 1000.f / 3600.f;
|
||||
}
|
||||
|
||||
int parse_arguments(const int argc, const char *const *const argv,
|
||||
price_t *const price, wattage_t *const wattage, interval_t *const interval) {
|
||||
if (argc < 2) return 1;
|
||||
|
||||
for (int i = 1, pos = 0; i < argc; ++i)
|
||||
if (argv[i][0] == '-') {
|
||||
if (strlen(argv[i]) < 2) return 99;
|
||||
switch (argv[i][1]) {
|
||||
case 'h': return 1;
|
||||
case 'v': return 2;
|
||||
default: return 99;
|
||||
}
|
||||
} else if (argc < 4) {
|
||||
return 1;
|
||||
} else if (pos == 0) {
|
||||
*price = price_kwh_to_wattsecond((price_t)atof(argv[i]));
|
||||
++pos;
|
||||
} else if (pos == 1) {
|
||||
*wattage = wattage_to_watts(argv[2]);
|
||||
++pos;
|
||||
} else
|
||||
*interval += interval_to_seconds(argv[i]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void print_version(void) {
|
||||
fputs("kwh-cost Ver. " VERSION "\n"
|
||||
"Copyright (C) 2021-2024 Alexander \"Arav\" Andreev <me@arav.su>. All rights reserved.\n"
|
||||
"This program is distributed under the terms of MIT+NIGGER license.\n"
|
||||
"A license for this program is placed in /usr/share/licenses/kwh-cost/LICENSE.\n", stderr);
|
||||
}
|
||||
|
||||
void print_usage(void) {
|
||||
fputs("Usage: kwh-cost [OPTIONS] price wattage interval [interval ...]\n"
|
||||
"Options:\n"
|
||||
" -h display this usage information.\n"
|
||||
" -v display program version.\n"
|
||||
"Arguments:\n"
|
||||
" price price per kWh.\n"
|
||||
" wattage power usage in Watts (m, K, M, G, T).\n"
|
||||
" interval time interval in seconds (m'inutes, h'ours, d'ays,\n"
|
||||
" like 5d 3h) during which given power is constantly used.\n"
|
||||
" All given intervals will be summed up.\n", stderr);
|
||||
}
|
||||
|
||||
int main(const int argc, const char *const *const argv) {
|
||||
price_t price = 0;
|
||||
wattage_t wattage = 0;
|
||||
interval_t interval = 0;
|
||||
|
||||
switch (parse_arguments(argc, argv, &price, &wattage, &interval)) {
|
||||
case 1: print_usage(); return 0;
|
||||
case 2: print_version(); return 0;
|
||||
case 99: fputs("Unknown flag provided.\n", stderr); print_usage(); return 1;
|
||||
}
|
||||
|
||||
printf("%.2Lf\n", (price * wattage) * interval);
|
||||
|
||||
return 0;
|
||||
}
|
91
src/main.c
91
src/main.c
@ -1,91 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "main.h"
|
||||
|
||||
int main(const int argc, const char *const *const argv) {
|
||||
price_t price;
|
||||
wattage_t wattage;
|
||||
interval_t interval = 0;
|
||||
|
||||
int res = parse_arguments(argc, argv, &price, &wattage, &interval);
|
||||
switch (res) {
|
||||
case 1: print_usage(); exit(0);
|
||||
case 2: print_version(); exit(0);
|
||||
case 99: fputs("Unknown flag provided.\n", stderr); print_usage(); exit(1);
|
||||
}
|
||||
|
||||
printf("%.2Lf\n", (price * wattage) * interval);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int parse_arguments(const int argc, const char *const *const argv,
|
||||
price_t *const price, wattage_t *const wattage, interval_t *const interval) {
|
||||
if (argc < 2) return 1;
|
||||
|
||||
for (int i = 1, pos = 0; i < argc; ++i)
|
||||
if (argv[i][0] == '-') {
|
||||
if (argv[i][1] == 'h' || argv[i][2] == 'h')
|
||||
return 1;
|
||||
else if (argv[i][1] == 'v' || argv[i][2] == 'v')
|
||||
return 2;
|
||||
else
|
||||
return 99;
|
||||
} else if (argc < 4) {
|
||||
return 1;
|
||||
} else if (pos == 0) {
|
||||
*price = price_kwh_to_wattspersecond((price_t)atof(argv[i]));
|
||||
++pos;
|
||||
} else if (pos == 1) {
|
||||
*wattage = (wattage_t)atof(argv[2]);
|
||||
++pos;
|
||||
} else
|
||||
*interval += interval_to_seconds(argv[i]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline interval_t interval_to_seconds(const char *const interval) {
|
||||
interval_t intrvl = (interval_t)atol(interval);
|
||||
|
||||
switch (interval[strlen(interval) - 1]) {
|
||||
case 'm':
|
||||
case 'M':
|
||||
return intrvl * 60;
|
||||
case 'h':
|
||||
case 'H':
|
||||
return intrvl * 60 * 60;
|
||||
case 'd':
|
||||
case 'D':
|
||||
return intrvl * 60 * 60 * 24;
|
||||
default:
|
||||
return intrvl;
|
||||
}
|
||||
}
|
||||
|
||||
inline price_t price_kwh_to_wattspersecond(const price_t price) {
|
||||
return price / 1000.f / 3600.f;
|
||||
}
|
||||
|
||||
inline void print_version() {
|
||||
fputs("kwh-cost Ver. " VERSION "\n"
|
||||
"Copyright (C) 2021-2023 Alexander \"Arav\" Andreev <me@arav.su>. All rights reserved.\n"
|
||||
"You can see a license for this program in /usr/share/licenses/kwh-cost/LICENSE.\n", stderr);
|
||||
}
|
||||
|
||||
inline void print_usage() {
|
||||
fputs("Usage: kwh-cost [OPTIONS] price wattage interval [interval ...]\n"
|
||||
"Options:\n"
|
||||
" --help, -h display this usage information.\n"
|
||||
" --version, -v display program version.\n"
|
||||
"Arguments:\n"
|
||||
" price price per kWh.\n"
|
||||
" wattage power usage in Watts.\n"
|
||||
" interval time interval in seconds during which\n"
|
||||
" this much power to be constantly used.\n"
|
||||
" You may specify other time units such as:\n"
|
||||
" m'inute, h'our, d'ay. Example: 5d 3h.\n"
|
||||
" All given intervals will be summed up.\n", stderr);
|
||||
}
|
19
src/main.h
19
src/main.h
@ -1,19 +0,0 @@
|
||||
#ifndef _MAIN_H_
|
||||
#define _MAIN_H_
|
||||
|
||||
#define VERSION "1.1.1"
|
||||
|
||||
typedef long double price_t;
|
||||
typedef long double wattage_t;
|
||||
typedef unsigned long interval_t;
|
||||
|
||||
int parse_arguments(const int argc, const char *const *const argv,
|
||||
price_t *const price, wattage_t *const wattage, interval_t *const interval);
|
||||
|
||||
inline interval_t interval_to_seconds(const char *const interval);
|
||||
inline price_t price_kwh_to_wattspersecond(const price_t price);
|
||||
|
||||
inline void print_version();
|
||||
inline void print_usage();
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user