Repository: dalance/procs (language: rust)

Issue:
`procs --sortd rss` doesn't show memory column
```
$ procs --sortd rss
 PID:▲ User            │ TTY    CPU  MEM CPU Time │ Command                                                                                                              >
                       │        [%]  [%]          │                                                                                                                      >
 1     root            │        0.0  0.1 00:00:05 │ /usr/lib/systemd/systemd rhgb --switched-root --system --deserialize 31            
```


Discussion / hints:
rss is not shown by default.
`--sortd` only sorts the specified column, do nothing if the column is not found.
If you want it, `VmRss` column can be added to your configuration file.
Please see the following file.
https://github.com/dalance/procs/blob/master/config/large.toml
I was looking for a lightweight replacement for `ps` and having to configure `procs` in addition to installation on every server I may need to inspect memory usage is too cumbersome. Unfortunately.
If you want to do without config,  you can `procs --insert rss --sortd rss`.
Why not to turn on the column automatically if specified?
There is no reason. I'll consider it, but I don't have the enough time to implement now.
Understand. Will a PR be accepted to provide that behavior?
> Understand. Will a PR be accepted to provide that behavior?

Yes

Fix patch:
diff --git a/src/main.rs b/src/main.rs
index 9c89a090d..d1bdf1cc6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -281,9 +281,9 @@ fn run() -> Result<(), Error> {
                 Some(n) => (n * 1000.0).round() as u64,
                 None => 1000,
             };
-            run_watch(&opt, &config, interval)
+            run_watch(&mut opt, &config, interval)
         } else {
-            run_default(&opt, &config)
+            run_default(&mut opt, &config)
         }
     }
 }
@@ -320,11 +320,11 @@ fn run_list() -> Result<(), Error> {
 }
 
 #[cfg_attr(tarpaulin, skip)]
-fn run_watch(opt: &Opt, config: &Config, interval: u64) -> Result<(), Error> {
+fn run_watch(opt: &mut Opt, config: &Config, interval: u64) -> Result<(), Error> {
     Watcher::start(opt, config, interval)
 }
 
-fn run_default(opt: &Opt, config: &Config) -> Result<(), Error> {
+fn run_default(opt: &mut Opt, config: &Config) -> Result<(), Error> {
     let mut time = Instant::now();
 
     let theme = get_theme(opt, config);
@@ -367,8 +367,8 @@ mod tests {
         config.display.theme = ConfigTheme::Dark;
 
         let args = vec!["procs"];
-        let opt = Opt::parse_from(args.iter());
-        let ret = run_default(&opt, &config);
+        let mut opt = Opt::parse_from(args.iter());
+        let ret = run_default(&mut opt, &config);
         assert!(ret.is_ok());
     }
 
@@ -379,40 +379,40 @@ mod tests {
         config.display.theme = ConfigTheme::Dark;
 
         let args = vec!["procs", "root"];
-        let opt = Opt::parse_from(args.iter());
-        let ret = run_default(&opt, &config);
+        let mut opt = Opt::parse_from(args.iter());
+        let ret = run_default(&mut opt, &config);
         assert!(ret.is_ok());
 
         let args = vec!["procs", "1"];
-        let opt = Opt::parse_from(args.iter());
-        let ret = run_default(&opt, &config);
+        let mut opt = Opt::parse_from(args.iter());
+        let ret = run_default(&mut opt, &config);
         assert!(ret.is_ok());
 
         let args = vec!["procs", "--or", "root", "1"];
-        let opt = Opt::parse_from(args.iter());
-        let ret = run_default(&opt, &config);
+        let mut opt = Opt::parse_from(args.iter());
+        let ret = run_default(&mut opt, &config);
         assert!(ret.is_ok());
 
         let args = vec!["procs", "--and", "root", "1"];
-        let opt = Opt::parse_from(args.iter());
-        let ret = run_default(&opt, &config);
+        let mut opt = Opt::parse_from(args.iter());
+        let ret = run_default(&mut opt, &config);
         assert!(ret.is_ok());
 
         let args = vec!["procs", "--nor", "root", "1"];
-        let opt = Opt::parse_from(args.iter());
-        let ret = run_default(&opt, &config);
+        let mut opt = Opt::parse_from(args.iter());
+        let ret = run_default(&mut opt, &config);
         assert!(ret.is_ok());
 
         let args = vec!["procs", "--nand", "root", "1"];
-        let opt = Opt::parse_from(args.iter());
-        let ret = run_default(&opt, &config);
+        let mut opt = Opt::parse_from(args.iter());
+        let ret = run_default(&mut opt, &config);
         assert!(ret.is_ok());
 
         config.search.nonnumeric_search = ConfigSearchKind::Exact;
         config.search.numeric_search = ConfigSearchKind::Partial;
         let args = vec!["procs", "root", "1"];
-        let opt = Opt::parse_from(args.iter());
-        let ret = run_default(&opt, &config);
+        let mut opt = Opt::parse_from(args.iter());
+        let ret = run_default(&mut opt, &config);
         assert!(ret.is_ok());
     }
 
@@ -435,9 +435,9 @@ mod tests {
         config.display.theme = ConfigTheme::Dark;
 
         let args = vec!["procs"];
-        let opt = Opt::parse_from(args.iter());
+        let mut opt = Opt::parse_from(args.iter());
         config.pager.mode = ConfigPagerMode::Disable;
-        let ret = run_default(&opt, &config);
+        let ret = run_default(&mut opt, &config);
         assert!(ret.is_ok());
     }
 
@@ -448,8 +448,8 @@ mod tests {
         config.display.theme = ConfigTheme::Dark;
 
         let args = vec!["procs", "--insert", "ppid"];
-        let opt = Opt::parse_from(args.iter());
-        let ret = run_default(&opt, &config);
+        let mut opt = Opt::parse_from(args.iter());
+        let ret = run_default(&mut opt, &config);
         assert!(ret.is_ok());
     }
 
@@ -460,13 +460,13 @@ mod tests {
         config.display.theme = ConfigTheme::Dark;
 
         let args = vec!["procs", "--sorta", "cpu"];
-        let opt = Opt::parse_from(args.iter());
-        let ret = run_default(&opt, &config);
+        let mut opt = Opt::parse_from(args.iter());
+        let ret = run_default(&mut opt, &config);
         assert!(ret.is_ok());
 
         let args = vec!["procs", "--sortd", "cpu"];
-        let opt = Opt::parse_from(args.iter());
-        let ret = run_default(&opt, &config);
+        let mut opt = Opt::parse_from(args.iter());
+        let ret = run_default(&mut opt, &config);
         assert!(ret.is_ok());
     }
 
@@ -477,8 +477,8 @@ mod tests {
         config.display.theme = ConfigTheme::Dark;
 
         let args = vec!["procs", "--tree"];
-        let opt = Opt::parse_from(args.iter());
-        let ret = run_default(&opt, &config);
+        let mut opt = Opt::parse_from(args.iter());
+        let ret = run_default(&mut opt, &config);
         assert!(ret.is_ok());
     }
 
@@ -492,8 +492,8 @@ mod tests {
         let _udp = std::net::UdpSocket::bind("127.0.0.1:10000");
 
         let args = vec!["procs"];
-        let opt = Opt::parse_from(args.iter());
-        let ret = run_default(&opt, &config);
+        let mut opt = Opt::parse_from(args.iter());
+        let ret = run_default(&mut opt, &config);
         assert!(ret.is_ok());
     }
 }
diff --git a/src/view.rs b/src/view.rs
index 3b3373874..285bcfcd4 100644
--- a/src/view.rs
+++ b/src/view.rs
@@ -31,7 +31,7 @@ pub struct View {
 }
 
 impl View {
-    pub fn new(opt: &Opt, config: &Config, clear_by_line: bool) -> Result<Self, Error> {
+    pub fn new(opt: &mut Opt, config: &Config, clear_by_line: bool) -> Result<Self, Error> {
         let mut slot_idx = 0;
         let mut columns = Vec::new();
         let mut only_kind_found = false;
@@ -48,6 +48,16 @@ impl View {
             header: None,
         };
 
+        // Adding the sort column to inserts if not already present
+        match (&opt.sorta, &opt.sortd) {
+            (_, Some(col)) | (Some(col), _) => {
+                if !opt.insert.contains(col) {
+                    opt.insert.push(col.clone());
+                }
+            }
+            _ => {}
+        }
+
         // Add default TreeSlot if there is not TreeSlot in config
         let config_columns = if config
             .columns
diff --git a/src/watcher.rs b/src/watcher.rs
index 7a9b2bf3b..5e58764f8 100644
--- a/src/watcher.rs
+++ b/src/watcher.rs
@@ -91,7 +91,7 @@ impl Watcher {
         Ok(())
     }
 
-    pub fn start(opt: &Opt, config: &Config, interval: u64) -> Result<(), Error> {
+    pub fn start(opt: &mut Opt, config: &Config, interval: u64) -> Result<(), Error> {
         let theme = get_theme(opt, config);
 
         let (tx_cmd, rx_cmd) = channel();


Test patch: