Repository: cloudhead/rx (language: rust)

Issue:
init.rx: fatal: no active view
When loading config options from ~/.config/rx/init.rx I'm getting the following errors:

```
[2020-01-04T23:46:09Z DEBUG rx::session] source: /home/pop/.config/rx/init.rx
[2020-01-04T23:46:09Z DEBUG rx::session] command: Set("checker", Bool(true))
[2020-01-04T23:46:09Z DEBUG rx::session] set `checker`: off -> on
[2020-01-04T23:46:09Z DEBUG rx::session] command: Set("grid/spacing", U32Tuple(4, 4))
[2020-01-04T23:46:09Z DEBUG rx::session] set `grid/spacing`: 8,8 -> 4,4
[2020-01-04T23:46:09Z DEBUG rx::session] command: ResizeFrame(64, 64)
thread 'main' panicked at 'fatal: no active view', src/session.rs:1293:9
```

Not every setting triggers this crash, but these do:

- [resize frame](https://github.com/cloudhead/rx/blob/master/src/session.rs#L2330)
- [slice](https://github.com/cloudhead/rx/blob/master/src/session.rs#L2490)
- [animation/delay](https://github.com/cloudhead/rx/blob/master/src/session.rs#L1153)

Seems like any command that calls `self.active_view_mut()` triggers the crash. Maybe the active view needs to be initialized before config files are loaded?

Without those config options set `rx` starts and runs well.

I'd love to contribute to the project. If I could get some direction on what changes should be made and where to make them I'd be more than happy to put together a pull request.

---

System details:

- Linux/Fedora 30 with Wayland
- rustc 1.42.0-nightly (3a3f4a7cb 2019-12-28)

Let me know if you need more system deets.


Discussion / hints:
Thanks for opening this!

I think loading a view before loading the config makes sense as a fix.

---

`lib.rs` is the entry point, and `session::init` is where `init.rx` gets loaded. `session::blank` is how the initial blank view is created, which is done in `lib.rs` *after* `::init`.

The desired behavior is that a blank view is created *only if* `rx` wasn't started with a file name, eg. `rx hero.png`. So the blank view creation is under the `if session.views.is_empty()` conditional.

I think the first thing to try would be to just move the blank view creation before the `::init`, and see what happens.

Let me know if you run into any trouble.

Fix patch:
diff --git a/src/lib.rs b/src/lib.rs
index a8180b57..d7ac6ef0 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -124,8 +124,13 @@ pub fn init<P: AsRef<Path>>(paths: &[P], options: Options) -> std::io::Result<()
     let resources = ResourceManager::new();
     let base_dirs = dirs::ProjectDirs::from("io", "cloudhead", "rx")
         .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "home directory not found"))?;
-    let mut session =
-        Session::new(win_w, win_h, resources.clone(), base_dirs).init(options.source.clone())?;
+    let mut session = Session::new(win_w, win_h, resources.clone(), base_dirs)
+        .with_blank(
+            FileStatus::NoFile,
+            Session::DEFAULT_VIEW_W,
+            Session::DEFAULT_VIEW_H,
+        )
+        .init(options.source.clone())?;
 
     if options.debug {
         session
@@ -184,13 +189,6 @@ pub fn init<P: AsRef<Path>>(paths: &[P], options: Options) -> std::io::Result<()
     if let Err(e) = session.edit(paths) {
         session.message(format!("Error loading path(s): {}", e), MessageType::Error);
     }
-    if session.views.is_empty() {
-        session.blank(
-            FileStatus::NoFile,
-            Session::DEFAULT_VIEW_W,
-            Session::DEFAULT_VIEW_H,
-        );
-    }
 
     renderer.init(session.effects(), &session.views);
 
diff --git a/src/session.rs b/src/session.rs
index b4f9801d..9c5171a6 100644
--- a/src/session.rs
+++ b/src/session.rs
@@ -885,6 +885,12 @@ impl Session {
         self.edit_view(id);
     }
 
+    pub fn with_blank(mut self, fs: FileStatus, w: u32, h: u32) -> Self {
+        self.blank(fs, w, h);
+
+        self
+    }
+
     /// Transition to a new state. Only allows valid state transitions.
     pub fn transition(&mut self, to: State) {
         match (&self.state, &to) {


Test patch: