├── README.md
├── UNLICENSE
├── bash.bash
├── crystal.cr
├── csharp.cs
├── deno.js
├── go.go
├── janet.janet
├── julia.jl
├── kotlin.kts
├── nim.nim
├── node.js
├── perl.pl
├── python.py
├── r.r
├── raku.p6
├── ruby.rb
├── rust.rs
└── sh.sh
/README.md:
--------------------------------------------------------------------------------
1 | # fetch
2 | a polyglot repo showing system status fetching in different languages. the output will look something like this:
3 |
4 | ```
5 | james@void
6 | kernel 5.4.46_1
7 | term alacritty
8 | shell /bin/sh
9 | tasks 151
10 | mem 2559m / 3974m
11 | uptime 0d 16h 39m
12 | ```
13 |
14 | | language | sloc |
15 | | - | - |
16 | | crystal | 17 |
17 | | raku | 17 |
18 | | ruby | 17 |
19 | | julia | 19 |
20 | | deno | 20 |
21 | | kotlin | 20 |
22 | | node | 20 |
23 | | python | 21 |
24 | | r | 21 |
25 | | janet | 22 |
26 | | bash | 23 |
27 | | nim | 25 |
28 | | perl | 27 |
29 | | sh | 28 |
30 | | csharp | 30 |
31 | | go | 46 |
32 | | rust | 61 |
33 |
34 | the deno script must be run with `deno run --allow-env --allow-read deno.js`
35 |
36 | for most languages the code size is determined by use of imports, and whether a `main` function is needed
37 |
38 | # spec
39 |
40 | try to follow an existing program for code organization
41 |
42 | external programs must not be used
43 |
44 | files must be closed after read (most languages do this automatically)
45 |
46 | the output must be in a single command. there must not be any code besides string interpolation / concatenation, ie define all variables beforehand.
47 |
48 | if an official style formatter exists it must be used
49 |
50 | the readme table is sorted by sloc
51 |
--------------------------------------------------------------------------------
/UNLICENSE:
--------------------------------------------------------------------------------
1 | This is free and unencumbered software released into the public domain.
2 |
3 | Anyone is free to copy, modify, publish, use, compile, sell, or
4 | distribute this software, either in source code form or as a compiled
5 | binary, for any purpose, commercial or non-commercial, and by any
6 | means.
7 |
8 | In jurisdictions that recognize copyright laws, the author or authors
9 | of this software dedicate any and all copyright interest in the
10 | software to the public domain. We make this dedication for the benefit
11 | of the public at large and to the detriment of our heirs and
12 | successors. We intend this dedication to be an overt act of
13 | relinquishment in perpetuity of all present and future rights to this
14 | software under copyright law.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | OTHER DEALINGS IN THE SOFTWARE.
23 |
24 | For more information, please refer to
25 |
--------------------------------------------------------------------------------
/bash.bash:
--------------------------------------------------------------------------------
1 | read _ _ KERNEL _
13 | int.TryParse(s.Split('/')[2], out _));
14 |
15 | var mem = File.ReadAllText("/proc/meminfo").Split('\n');
16 | Func getsize = s =>
17 | int.Parse(s.Split(' ', StringSplitOptions.RemoveEmptyEntries)[1]) / 1000;
18 | var total = getsize(mem[0]);
19 | var avail = getsize(mem[2]);
20 |
21 | var uptime = int.Parse(File.ReadAllText("/proc/uptime").Split('.')[0]);
22 | var d = uptime / 60 / 60 / 24;
23 | var h = uptime / 60 / 60 % 24;
24 | var m = uptime / 60 % 60;
25 |
26 | Console.WriteLine($"{user}@{host}
27 | kernel\t{kernel}
28 | term\t{term}
29 | shell\t{shell}
30 | tasks\t{tasks}
31 | mem\t{avail}m / {total}m
32 | uptime\t{d}d {h}h {m}m");
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/deno.js:
--------------------------------------------------------------------------------
1 | const { SHELL: shell, TERM: term, USER: user } = Deno.env.toObject();
2 | const host = Deno.readTextFileSync("/etc/hostname").trimRight();
3 | const kernel = Deno.readTextFileSync("/proc/version").split(" ")[2];
4 | const tasks =
5 | Array.from(Deno.readDirSync("/proc")).filter((x) => Number(x.name)).length;
6 |
7 | const mem = Deno.readTextFileSync("/proc/meminfo").split("\n").slice(0, 3);
8 | const [total, _, avail] = mem.map((x) =>
9 | Math.floor(Number(x.match(/\d+/))) / 1000
10 | );
11 |
12 | const uptime = Number(Deno.readTextFileSync("/proc/uptime").split(" ")[0]);
13 | const d = Math.floor(uptime / 60 / 60 / 24);
14 | const h = Math.floor(uptime / 60 / 60 % 24);
15 | const m = Math.floor(uptime / 60 % 60);
16 |
17 | console.log(`${user}@${host}
18 | kernel\t${kernel}
19 | term\t${term}
20 | shell\t${shell}
21 | tasks\t${tasks}
22 | mem\t${avail}m / ${total}m
23 | uptime\t${d}d ${h}h ${m}m`);
24 |
--------------------------------------------------------------------------------
/go.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "fmt"
5 | "io/ioutil"
6 | "os"
7 | "strconv"
8 | "strings"
9 | )
10 |
11 | func read(path string) string {
12 | buf, _ := ioutil.ReadFile(path)
13 | return string(buf)
14 | }
15 |
16 | func main() {
17 | user := os.Getenv("USER")
18 | host := read("/etc/hostname")
19 | kernel := strings.Fields(read("/proc/version"))[2]
20 | term := os.Getenv("TERM")
21 | shell := os.Getenv("SHELL")
22 |
23 | n := 0
24 | files, _ := ioutil.ReadDir("/proc")
25 | for _, file := range files {
26 | _, err := strconv.Atoi(file.Name())
27 | if err == nil {
28 | n += 1
29 | }
30 | }
31 | tasks := strconv.Itoa(n)
32 |
33 | mem := strings.Split(read("/proc/meminfo"), "\n")
34 | getSize := func(s string) string {
35 | i, _ := strconv.Atoi(strings.Fields(s)[1])
36 | return strconv.Itoa(i / 1000)
37 | }
38 | total := getSize(mem[0])
39 | avail := getSize(mem[2])
40 |
41 | uptime, _ := strconv.Atoi(strings.Split(read("/proc/uptime"), ".")[0])
42 | d := strconv.Itoa(uptime / 60 / 60 / 24)
43 | h := strconv.Itoa(uptime / 60 / 60 % 24)
44 | m := strconv.Itoa(uptime / 60 / 60)
45 |
46 | fmt.Printf(user + "@" + host +
47 | "kernel\t" + kernel +
48 | "\nterm\t" + term +
49 | "\nshell\t" + shell +
50 | "\ntasks\t" + tasks +
51 | "\nmem\t" + avail + "m / " + total + "m" +
52 | "\nuptime\t" + d + "d " + h + "h " + m + "m")
53 | }
54 |
--------------------------------------------------------------------------------
/janet.janet:
--------------------------------------------------------------------------------
1 | (let [user (os/getenv "USER")
2 | host (slurp "/etc/hostname")
3 | kernel (->> (slurp "/proc/version") (string/split " ") 2)
4 | term (os/getenv "TERM")
5 | shell (os/getenv "SHELL")
6 | tasks (length (filter scan-number (os/dir "/proc")))
7 | mem (->> (slurp "/proc/meminfo") (string/split "\n") (take 3))
8 | uptime (->> (slurp "/proc/uptime") (string/split ".") 0 int/u64)]
9 |
10 | (defn split [s] (filter (comp not empty?) (string/split " " s)))
11 | (def mem (map (comp |(/ $ 1000) int/u64 1 split) mem))
12 | (def total (mem 0))
13 | (def avail (mem 2))
14 |
15 | (def d (-> uptime (/ 60 60 24)))
16 | (def h (-> uptime (/ 60 60) (% 24)))
17 | (def m (-> uptime (/ 60) (% 60)))
18 |
19 | (print user "@" host
20 | "kernel\t" kernel
21 | "\nterm\t" term
22 | "\nshell\t" shell
23 | "\ntasks\t" tasks
24 | "\nmem\t" avail "m / " total "m"
25 | "\nuptime\t" d "d " h "h " m "m"))
26 |
--------------------------------------------------------------------------------
/julia.jl:
--------------------------------------------------------------------------------
1 | user = ENV["USER"]
2 | host = readlines("/etc/hostname")[1]
3 | kernel = split(readlines("/proc/version")[1])[3]
4 | term = ENV["TERM"]
5 | shell = ENV["SHELL"]
6 | tasks = length(filter(x -> isa(tryparse(Int, x), Int), readdir("/proc")))
7 |
8 | mem = readlines("/proc/meminfo")[1:3]
9 | total, _, avail = map(x -> fld(parse(Int, split(x)[2]), 1000), mem)
10 |
11 | uptime = parse(Int, split(readlines("/proc/uptime")[1], '.')[1])
12 | d = trunc(Int, (uptime / 60 / 60 / 24))
13 | h = trunc(Int, (uptime / 60 / 60 % 24))
14 | m = trunc(Int, (uptime / 60 % 60))
15 |
16 | print("$user@$host
17 | kernel\t$kernel
18 | term\t$term
19 | shell\t$shell
20 | tasks\t$tasks
21 | mem\t$(avail)m / $(total)m
22 | uptime\t$(d)d $(h)h $(m)m")
23 |
--------------------------------------------------------------------------------
/kotlin.kts:
--------------------------------------------------------------------------------
1 | import java.io.File
2 |
3 | val user = System.getenv("USER")
4 | val host = File("/etc/hostname").readLines()[0]
5 | val kernel = File("/proc/version").readText().split(' ')[2]
6 | val term = System.getenv("TERM")
7 | val shell = System.getenv("SHELL")
8 | val tasks = File("/proc").list().count { it.toIntOrNull() != null }
9 |
10 | val mem = File("/proc/meminfo").readLines().take(3)
11 | val (total, _, avail) = mem.map { it.split(Regex("\\s+"))[1].toInt() / 1000 }
12 |
13 | val uptime = File("/proc/uptime").readText().split('.')[0].toInt()
14 | val d = uptime / 60 / 60 / 24
15 | val h = uptime / 60 / 60 % 24
16 | val m = uptime / 60 % 60
17 |
18 | println("$user@$host" +
19 | "\nkernel\t$kernel" +
20 | "\nterm\t$term" +
21 | "\nshell\t$shell" +
22 | "\ntasks\t$tasks" +
23 | "\nmem\t${avail}m / ${total}m" +
24 | "\nuptime\t${d}d ${h}h ${m}m")
25 |
--------------------------------------------------------------------------------
/nim.nim:
--------------------------------------------------------------------------------
1 | import os, strutils
2 |
3 | let user = getEnv("USER")
4 | let host = readFile("/etc/hostname")
5 | let kernel = readFile("/proc/version").split()[2]
6 | let term = getEnv("TERM")
7 | let shell = getEnv("SHELL")
8 |
9 | var tasks = 0
10 | for _, path in walkDir("/proc", true):
11 | if not path.contains(Allchars - Digits):
12 | tasks += 1
13 |
14 | let mem = readFile("/proc/meminfo").splitLines()
15 | proc getsize(s: string): int = parseInt(s.splitWhitespace()[1]) div 1000
16 | let total = getsize(mem[0])
17 | let avail = getsize(mem[2])
18 |
19 | let uptime = parseFloat(readFile("/proc/uptime").split()[0])
20 | let d = toInt(uptime / 60 / 60 / 24)
21 | let h = toInt(uptime / 60 / 60) mod 24
22 | let m = toInt(uptime / 60) mod 60
23 |
24 | echo user, "@", host,
25 | "kernel\t", kernel,
26 | "\nterm\t", term,
27 | "\nshell\t", shell,
28 | "\ntasks\t", tasks,
29 | "\nmem\t", avail, "m / ", total, "m",
30 | "\nuptime\t", d, "d ", h, "h ", m, "m"
31 |
--------------------------------------------------------------------------------
/node.js:
--------------------------------------------------------------------------------
1 | const fs = require("fs");
2 |
3 | const { SHELL: shell, TERM: term, USER: user } = process.env;
4 | const host = require("os").hostname();
5 | const kernel = fs.readFileSync("/proc/version", "utf8").split(" ")[2];
6 | const tasks = fs.readdirSync("/proc").filter((s) => Number(s)).length;
7 |
8 | const mem = fs.readFileSync("/proc/meminfo", "utf8").split("\n").slice(0, 3);
9 | const [total, _, avail] = mem.map((x) =>
10 | Math.floor(Number(x.match(/\d+/))) / 1000
11 | );
12 |
13 | const uptime = Number(fs.readFileSync("/proc/uptime", "utf8").split(" ")[0]);
14 | const d = Math.floor(uptime / 60 / 60 / 24);
15 | const h = Math.floor(uptime / 60 / 60 % 24);
16 | const m = Math.floor(uptime / 60 % 60);
17 |
18 | console.log(`${user}@${host}
19 | kernel\t${kernel}
20 | term\t${term}
21 | shell\t${shell}
22 | tasks\t${tasks}
23 | mem\t${avail}m / ${total}m
24 | uptime\t${d}d ${h}h ${m}m`);
25 |
--------------------------------------------------------------------------------
/perl.pl:
--------------------------------------------------------------------------------
1 | sub ffetch {
2 | open("f", "<", $_[0]) or die $!;
3 | my @r = ;
4 | close "f";
5 | return @r;
6 | }
7 |
8 | my $user = $ENV{'USER'};
9 | my $hostname = (ffetch("/etc/hostname"))[0];
10 | my $kernel = (split / /, (ffetch("/proc/version"))[0])[2];
11 | my $term = $ENV{'TERM'};
12 | my $shell = $ENV{'SHELL'};
13 |
14 | opendir my($dir), "/proc" or die $!;
15 | my $tasks = scalar (grep { /\d+$/ } readdir $dir);
16 | closedir $dir;
17 |
18 | my @mem = ffetch("/proc/meminfo");
19 | my ($total, $x, $avail) = map {int(($_ =~ /(\d+)/)[0] / 1000)} @mem;
20 |
21 | my @uptime = split / /, (ffetch("/proc/uptime"))[0];
22 | my $d = int((($uptime[0] / 60) / 60) / 24);
23 | my $h = (($uptime[0] / 60) / 60) % 24;
24 | my $m = ($uptime[0] / 60) % 60;
25 |
26 | print $user . "@" . $hostname
27 | . "kernel\t" . $kernel
28 | . "\nterm\t" . $term
29 | . "\nshell\t" . $shell
30 | . "\ntasks\t" . $tasks
31 | . "\nmem\t" . $avail . "m / " . $total . "m"
32 | . "\nuptime\t" . $d . "d " . $h . "h " . $m . "m";
33 |
--------------------------------------------------------------------------------
/python.py:
--------------------------------------------------------------------------------
1 | import os
2 |
3 | def read(path):
4 | with open(path) as f:
5 | return f.read()
6 |
7 | user, term, shell = [os.environ[x] for x in ["USER", "TERM", "SHELL"]]
8 | host = read('/etc/hostname').rstrip()
9 | kernel = read('/proc/version').split()[2]
10 | tasks = len([x for x in os.listdir('/proc') if x.isdigit()])
11 |
12 | mem = read('/proc/meminfo').split('\n')[:3]
13 | (total, _, avail) = [int(x.split()[1]) // 1000 for x in mem]
14 |
15 | uptime = float(read('/proc/uptime').split()[0])
16 | d = int(uptime / 60 / 60 / 24)
17 | h = int(uptime / 60 / 60 % 24)
18 | m = int(uptime / 60 % 60)
19 |
20 | print(f"""{user}@{host}
21 | kernel\t{kernel}
22 | term\t{term}
23 | shell\t{shell}
24 | tasks\t{tasks}
25 | mem\t{avail}m / {total}m
26 | uptime\t{d}d {h}h {m}m""")
27 |
--------------------------------------------------------------------------------
/r.r:
--------------------------------------------------------------------------------
1 | user <- Sys.getenv("USER")
2 | hostname <- readLines("/etc/hostname")
3 | kernel <- strsplit(readLines("/proc/version"), " ")[[1]][3]
4 | term <- Sys.getenv("TERM")
5 | shell <- Sys.getenv("SHELL")
6 | tasks <- length(dir("/proc")[ grepl("^[0-9]{1,}$", dir("/proc")) ])
7 |
8 | mem <- readLines("/proc/meminfo")
9 | total <- as.numeric(strsplit(mem[1], "\\D+")[[1]][-1]) %/% 1000
10 | avail <- as.numeric(strsplit(mem[3], "\\D+")[[1]][-1]) %/% 1000
11 |
12 | uptime <- as.numeric(strsplit(readLines("/proc/uptime"), " ")[[1]][1])
13 | d <- trunc(((uptime / 60) / 60) / 24)
14 | h <- trunc(((uptime / 60) / 60) %% 24)
15 | m <- trunc((uptime / 60) %% 60)
16 |
17 | cat(user, "@", hostname,
18 | "\nkernel\t", kernel,
19 | "\nterm\t", term,
20 | "\nshell\t", shell,
21 | "\ntasks\t", tasks,
22 | "\nmem\t", avail, "m / ", total, "m",
23 | "\nuptime\t", d, "d ", h, "h ", m, "m", "\n",
24 | sep="")
25 |
--------------------------------------------------------------------------------
/raku.p6:
--------------------------------------------------------------------------------
1 | my ($user, $term, $shell) = %*ENV;
2 | my $host = '/etc/hostname'.IO.lines[0];
3 | my $kernel = '/proc/version'.IO.words[2];
4 | my $tasks = '/proc'.IO.dir(test => /\d+/).elems;
5 |
6 | my @mem = '/proc/meminfo'.IO.lines.head(3);
7 | my ($total, $x, $avail) = @mem.map(-> $x {Int($x.words[1]) div 1000});
8 |
9 | my $uptime = '/proc/uptime'.IO.words[0];
10 | my $d = Int($uptime / 60 / 60 / 24);
11 | my $h = Int($uptime / 60 / 60 % 24);
12 | my $m = Int($uptime / 60 % 60);
13 |
14 | say "$user@$host
15 | kernel\t$kernel
16 | term\t$term
17 | shell\t$shell
18 | tasks\t$tasks
19 | mem\t{$avail}m / {$total}m
20 | uptime\t{$d}d {$h}h {$m}m"
21 |
--------------------------------------------------------------------------------
/ruby.rb:
--------------------------------------------------------------------------------
1 | user, term, shell = ENV.values_at("USER", "TERM", "SHELL")
2 | host = File.read("/etc/hostname").strip
3 | kernel = File.read("/proc/version").split[2]
4 | tasks = Dir.entries("/proc").count { |x| x.to_i != 0 }
5 |
6 | mem = File.read("/proc/meminfo").lines.first 3
7 | total, _, avail = mem.map { |x| x.split[1].to_i / 1000 }
8 |
9 | uptime = File.read("/proc/uptime").split(".")[0].to_i
10 | d = uptime / 60 / 60 / 24
11 | h = uptime / 60 / 60 % 24
12 | m = uptime / 60 % 60
13 |
14 | print "#{user}@#{host}
15 | kernel\t#{kernel}
16 | term\t#{term}
17 | shell\t#{shell}
18 | tasks\t#{tasks}
19 | mem\t#{avail}m / #{total}m
20 | uptime\t#{d}d #{h}h #{m}m"
21 |
--------------------------------------------------------------------------------
/rust.rs:
--------------------------------------------------------------------------------
1 | use std::env::var;
2 | use std::fs::{read_dir, read_to_string};
3 |
4 | fn mem() -> (i32, i32) {
5 | let mem = read_to_string("/proc/meminfo").unwrap();
6 | let get = |i: usize| -> i32 {
7 | mem.lines()
8 | .nth(i)
9 | .unwrap()
10 | .split_ascii_whitespace()
11 | .nth(1)
12 | .unwrap()
13 | .parse::()
14 | .unwrap()
15 | / 1000
16 | };
17 | (get(0), get(2))
18 | }
19 |
20 | fn uptime() -> (i32, i32, i32) {
21 | let uptime = read_to_string("/proc/uptime")
22 | .unwrap()
23 | .split(' ')
24 | .next()
25 | .unwrap()
26 | .parse::()
27 | .unwrap() as i32;
28 | let d = uptime / 60 / 60 / 24;
29 | let h = uptime / 60 / 60 % 24;
30 | let m = uptime / 60 % 60;
31 | (d, h, m)
32 | }
33 |
34 | fn main() {
35 | let user = var("USER").unwrap();
36 | let host = read_to_string("/etc/hostname").unwrap().trim().to_string();
37 | let kernel = read_to_string("/proc/version")
38 | .unwrap()
39 | .split(' ')
40 | .nth(2)
41 | .unwrap()
42 | .to_string();
43 | let term = var("TERM").unwrap();
44 | let shell = var("SHELL").unwrap();
45 |
46 | let tasks = read_dir("/proc")
47 | .unwrap()
48 | .filter(|entry| {
49 | let name = entry.as_ref().unwrap().file_name().into_string().unwrap();
50 | name.chars().all(|c| c.is_digit(10))
51 | })
52 | .count();
53 |
54 | let (total, avail) = mem();
55 | let (d, h, m) = uptime();
56 | println!(
57 | "{}@{}
58 | kernel\t{}
59 | term\t{}
60 | shell\t{}
61 | tasks\t{}
62 | mem\t{}m / {}m
63 | uptime\t{}d {}h {}m",
64 | user, host, kernel, term, shell, tasks, avail, total, d, h, m
65 | );
66 | }
67 |
--------------------------------------------------------------------------------
/sh.sh:
--------------------------------------------------------------------------------
1 | read _ _ KERNEL _ /dev/null && TASKS=$((TASKS + 1))
8 | done
9 |
10 | while IFS=$'\n' read -r l; do
11 | set -- $l
12 | case "$1" in
13 | MemTotal:)
14 | TOTAL=$2 ;;
15 | MemAvailable:)
16 | AVAIL=$2 ;;
17 | esac
18 | done